Skip to content

Commit

Permalink
Allow for enabling physics when spawning a new object (#2902)
Browse files Browse the repository at this point in the history
* Allow for enabling physics when spawning a new object
  • Loading branch information
Sai Vemprala committed Jul 31, 2020
1 parent fd4a8fe commit 84f314d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion AirLib/include/api/WorldSimApiBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class WorldSimApiBase {

// ------ Level setting apis ----- //
virtual bool loadLevel(const std::string& level_name) = 0;
virtual string spawnObject(string& object_name, const string& load_component, const Pose& pose, const Vector3r& scale) = 0;
virtual string spawnObject(string& object_name, const string& load_component, const Pose& pose, const Vector3r& scale, bool physics_enabled) = 0;
virtual bool destroyObject(const string& object_name) = 0;

virtual bool isPaused() const = 0;
Expand Down
4 changes: 2 additions & 2 deletions AirLib/src/api/RpcLibServerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ RpcLibServerBase::RpcLibServerBase(ApiProvider* api_provider, const std::string&
return getWorldSimApi()->loadLevel(level_name);
});

pimpl_->server.bind("simSpawnObject", [&](string& object_name, const string& load_component, const RpcLibAdapatorsBase::Pose& pose, const RpcLibAdapatorsBase::Vector3r& scale) -> string {
return getWorldSimApi()->spawnObject(object_name, load_component, pose.to(), scale.to());
pimpl_->server.bind("simSpawnObject", [&](string& object_name, const string& load_component, const RpcLibAdapatorsBase::Pose& pose, const RpcLibAdapatorsBase::Vector3r& scale, bool physics_enabled) -> string {
return getWorldSimApi()->spawnObject(object_name, load_component, pose.to(), scale.to(), physics_enabled);
});

pimpl_->server.bind("simDestroyObject", [&](const string& object_name) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions PythonClient/airsim/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def simListSceneObjects(self, name_regex = '.*'):
"""
return self.client.call('simListSceneObjects', name_regex)

def simSpawnObject(self, object_name, asset_name, pose, scale):
def simSpawnObject(self, object_name, asset_name, pose, scale, physics_enabled=False):
"""Spawned selected object in the world
Args:
Expand All @@ -395,7 +395,7 @@ def simSpawnObject(self, object_name, asset_name, pose, scale):
Returns:
str: Name of spawned object, in case it had to be modified
"""
return self.client.call('simSpawnObject', object_name, asset_name, pose, scale)
return self.client.call('simSpawnObject', object_name, asset_name, pose, scale, physics_enabled)

def simDestroyObject(self, object_name):
"""Removes selected object from the world
Expand Down
2 changes: 1 addition & 1 deletion Unity/AirLibWrapper/AirsimWrapper/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase

// ------ Level setting apis ----- //
virtual bool loadLevel(const std::string& level_name) { return false; };
virtual std::string spawnObject(std::string& object_name, const std::string& load_component, const Pose& pose, const Vector3r& scale) { return ""; };
virtual std::string spawnObject(std::string& object_name, const std::string& load_component, const Pose& pose, const Vector3r& scale, bool physics_enabled) { return ""; };
virtual bool destroyObject(const std::string& object_name) { return false; };

virtual bool isPaused() const override;
Expand Down
12 changes: 8 additions & 4 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ bool WorldSimApi::destroyObject(const std::string& object_name)
return result;
}

std::string WorldSimApi::spawnObject(std::string& object_name, const std::string& load_object, const WorldSimApi::Pose& pose, const WorldSimApi::Vector3r& scale)
std::string WorldSimApi::spawnObject(std::string& object_name, const std::string& load_object, const WorldSimApi::Pose& pose, const WorldSimApi::Vector3r& scale, bool physics_enabled)
{
// Create struct for Location and Rotation of actor in Unreal
FTransform actor_transform = simmode_->getGlobalNedTransform().fromGlobalNed(pose);
bool found_object;
UAirBlueprintLib::RunCommandOnGameThread([this, load_object, &object_name, &actor_transform, &found_object, &scale]() {
UAirBlueprintLib::RunCommandOnGameThread([this, load_object, &object_name, &actor_transform, &found_object, &scale, &physics_enabled]() {
// Find mesh in /Game and /AirSim asset registry. When more plugins are added this function will have to change
UStaticMesh* LoadObject = dynamic_cast<UStaticMesh*>(UAirBlueprintLib::GetMeshFromRegistry(load_object));
if (LoadObject)
Expand All @@ -104,8 +104,10 @@ std::string WorldSimApi::spawnObject(std::string& object_name, const std::string
}
FActorSpawnParameters new_actor_spawn_params;
new_actor_spawn_params.Name = FName(object_name.c_str());
this->createNewActor(new_actor_spawn_params, actor_transform, scale, LoadObject);
AActor* NewActor = this->createNewActor(new_actor_spawn_params, actor_transform, scale, LoadObject);
found_object = true;

UAirBlueprintLib::setSimulatePhysics(NewActor, physics_enabled);
}
else
{
Expand All @@ -121,7 +123,7 @@ std::string WorldSimApi::spawnObject(std::string& object_name, const std::string
return object_name;
}

void WorldSimApi::createNewActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh)
AActor* WorldSimApi::createNewActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh)
{
AActor* NewActor = simmode_->GetWorld()->SpawnActor<AActor>(AActor::StaticClass(), FVector::ZeroVector, FRotator::ZeroRotator, spawn_params); // new
UStaticMeshComponent* ObjectComponent = NewObject<UStaticMeshComponent>(NewActor);
Expand All @@ -132,6 +134,8 @@ void WorldSimApi::createNewActor(const FActorSpawnParameters& spawn_params, cons
ObjectComponent->RegisterComponent();
NewActor->SetRootComponent(ObjectComponent);
NewActor->SetActorLocationAndRotation(actor_transform.GetLocation(), actor_transform.GetRotation(), false, nullptr, ETeleportType::TeleportPhysics);

return NewActor;
}

bool WorldSimApi::isPaused() const
Expand Down
4 changes: 2 additions & 2 deletions Unreal/Plugins/AirSim/Source/WorldSimApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase {

virtual bool loadLevel(const std::string& level_name) override;

virtual std::string spawnObject(std::string& object_name, const std::string& load_name, const WorldSimApi::Pose& pose, const WorldSimApi::Vector3r& scale) override;
virtual std::string spawnObject(std::string& object_name, const std::string& load_name, const WorldSimApi::Pose& pose, const WorldSimApi::Vector3r& scale, bool physics_enabled) override;
virtual bool destroyObject(const std::string& object_name) override;

virtual bool isPaused() const override;
Expand Down Expand Up @@ -64,7 +64,7 @@ class WorldSimApi : public msr::airlib::WorldSimApiBase {
virtual bool isRecording() const override;

private:
void createNewActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh);
AActor* createNewActor(const FActorSpawnParameters& spawn_params, const FTransform& actor_transform, const Vector3r& scale, UStaticMesh* static_mesh);
void spawnPlayer();

private:
Expand Down

0 comments on commit 84f314d

Please sign in to comment.