Skip to content

Commit f573de8

Browse files
committed
Add simFindLookAtRotation
1 parent 6bbd11b commit f573de8

File tree

9 files changed

+51
-0
lines changed

9 files changed

+51
-0
lines changed

AirLib/include/api/RpcLibClientBase.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ namespace airlib
165165
void simSetWind(const Vector3r& wind) const;
166166
void simSetExtForce(const Vector3r& ext_force) const;
167167

168+
Vector3r simFindLookAtRotation(const std::string& vehicle_name, const std::string& object_name) const;
169+
168170
vector<string> listVehicles();
169171

170172
std::string getSettingsString() const;

AirLib/include/api/WorldSimApiBase.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ namespace airlib
8181
virtual vector<MeshPositionVertexBuffersResponse> getMeshPositionVertexBuffers() const = 0;
8282

8383
virtual bool createVoxelGrid(const Vector3r& position, const int& x_size, const int& y_size, const int& z_size, const float& res, const std::string& output_file) = 0;
84+
virtual Vector3r findLookAtRotation(const std::string& vehicle_name, const std::string& object_name) = 0;
8485

8586
// Recording APIs
8687
virtual void startRecording() = 0;

AirLib/src/api/RpcLibClientBase.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,11 @@ __pragma(warning(disable : 4239))
583583
return pimpl_->client.call("simCreateVoxelGrid", RpcLibAdaptorsBase::Vector3r(position), x, y, z, res, output_file).as<bool>();
584584
}
585585

586+
msr::airlib::Vector3r RpcLibClientBase::simFindLookAtRotation(const std::string& vehicle_name, const std::string& object_name) const
587+
{
588+
return pimpl_->client.call("simFindLookAtRotation", vehicle_name, object_name).as<RpcLibAdaptorsBase::Vector3r>().to();
589+
}
590+
586591
void RpcLibClientBase::cancelLastTask(const std::string& vehicle_name)
587592
{
588593
pimpl_->client.call("cancelLastTask", vehicle_name);

AirLib/src/api/RpcLibServerBase.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ namespace airlib
516516
return getWorldSimApi()->getSettingsString();
517517
});
518518

519+
pimpl_->server.bind("simFindLookAtRotation", [&](const std::string& vehicle_name, const std::string& object_name) -> RpcLibAdaptorsBase::Vector3r {
520+
const auto& rot = getWorldSimApi()->findLookAtRotation(vehicle_name, object_name);
521+
return RpcLibAdaptorsBase::Vector3r(rot);
522+
});
523+
519524
//if we don't suppress then server will bomb out for exceptions raised by any method
520525
pimpl_->server.suppress_exceptions(true);
521526
}

PythonClient/airsim/client.py

+3
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ def simSetExtForce(self, ext_force):
11371137
"""
11381138
self.client.call('simSetExtForce', ext_force)
11391139

1140+
def simFindLookAtRotation(self, object_name, vehicle_name = ''):
1141+
return self.client.call('simFindLookAtRotation', vehicle_name, object_name)
1142+
11401143
# ----------------------------------- Multirotor APIs ---------------------------------------------
11411144
class MultirotorClient(VehicleClient, object):
11421145
def __init__(self, ip = "", port = 41451, timeout_value = 3600):

Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,13 @@ bool UAirBlueprintLib::CompressUsingImageWrapper(const TArray<uint8>& uncompress
828828

829829
return bSucceeded;
830830
}
831+
832+
void UAirBlueprintLib::FindAllActorByTag(const UObject* context, FName tag, TArray<AActor*>& foundActors)
833+
{
834+
UGameplayStatics::GetAllActorsWithTag(context, tag, foundActors);
835+
}
836+
837+
FRotator UAirBlueprintLib::FindLookAtRotation(AActor* source, AActor* target)
838+
{
839+
return UKismetMathLibrary::FindLookAtRotation(source->GetActorLocation(), target->GetActorLocation());
840+
}

Unreal/Plugins/AirSim/Source/AirBlueprintLib.h

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
7171
UGameplayStatics::GetAllActorsOfClass(context, T::StaticClass(), foundActors);
7272
}
7373

74+
static void FindAllActorByTag(const UObject* context, FName tag, TArray<AActor*>& foundActors);
75+
static FRotator FindLookAtRotation(AActor* source, AActor* target);
76+
7477
static std::vector<std::string> ListMatchingActors(const UObject* context, const std::string& name_regex);
7578
static std::vector<std::string> ListMatchingActorsByTag(const UObject* context, const std::string& tag_regex);
7679

Unreal/Plugins/AirSim/Source/WorldSimApi.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -1073,3 +1073,23 @@ std::vector<msr::airlib::DetectionInfo> WorldSimApi::getDetections(ImageCaptureB
10731073

10741074
return result;
10751075
}
1076+
1077+
Vector3r WorldSimApi::findLookAtRotation(const std::string& vehicle_name, const std::string& object_name)
1078+
{
1079+
Vector3r result = Vector3r::Zero();
1080+
PawnSimApi* pawn = simmode_->getVehicleSimApi(vehicle_name);
1081+
1082+
if (pawn) {
1083+
AActor* source = pawn->getPawn();
1084+
UAirBlueprintLib::RunCommandOnGameThread([this, object_name, &source, &result]() {
1085+
AActor* target = UAirBlueprintLib::FindActor<AActor>(simmode_, FString(object_name.c_str()));
1086+
if (target) {
1087+
FRotator rot = UAirBlueprintLib::FindLookAtRotation(source, target);
1088+
result = Vector3r(rot.Pitch, rot.Roll, rot.Yaw);
1089+
}
1090+
},
1091+
true);
1092+
}
1093+
1094+
return result;
1095+
}

Unreal/Plugins/AirSim/Source/WorldSimApi.h

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase
121121
virtual void clearDetectionMeshNames(ImageCaptureBase::ImageType image_type, const CameraDetails& camera_details) override;
122122
virtual std::vector<msr::airlib::DetectionInfo> getDetections(ImageCaptureBase::ImageType image_type, const CameraDetails& camera_details) override;
123123

124+
virtual Vector3r findLookAtRotation(const std::string& vehicle_name, const std::string& object_name);
125+
124126
private:
125127
AActor* createNewStaticMeshActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh);
126128
AActor* createNewBPActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UBlueprint* blueprint);

0 commit comments

Comments
 (0)