diff --git a/LibCarla/source/carla/client/Actor.cpp b/LibCarla/source/carla/client/Actor.cpp index 476d51890..78e63c050 100644 --- a/LibCarla/source/carla/client/Actor.cpp +++ b/LibCarla/source/carla/client/Actor.cpp @@ -84,6 +84,11 @@ namespace client { GetEpisode().Lock()->SetActorSimulatePhysics(*this, enabled); } + void Actor::SetEnableGravity(const bool enabled) { + std::cout << "Actor::SetEnableGravity" << std::endl; + GetEpisode().Lock()->SetActorEnableGravity(*this, enabled); + } + bool Actor::Destroy() { if (IsAlive()) { // Let the exceptions leave the function, IsAlive() will still be true. diff --git a/LibCarla/source/carla/client/Actor.h b/LibCarla/source/carla/client/Actor.h index 3710765bb..5fdc1698c 100644 --- a/LibCarla/source/carla/client/Actor.h +++ b/LibCarla/source/carla/client/Actor.h @@ -97,6 +97,9 @@ namespace client { /// Enable or disable physics simulation on this actor. void SetSimulatePhysics(bool enabled = true); + /// Enable or disable gravity on this actor. + void SetEnableGravity(bool enabled = true); + /// @warning This method only checks whether this instance of Actor has /// called the Destroy() method, it does not check whether the actor is /// actually alive in the simulator. diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index 77add06c6..3fe744611 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -308,6 +308,11 @@ namespace detail { _pimpl->AsyncCall("set_actor_simulate_physics", actor, enabled); } + void Client::SetActorEnableGravity(rpc::ActorId actor, const bool enabled) { + std::cout << "SetActorEnableGravity" << std::endl; + _pimpl->AsyncCall("set_actor_enable_gravity", actor, enabled); + } + void Client::SetActorAutopilot(rpc::ActorId vehicle, const bool enabled) { _pimpl->AsyncCall("set_actor_autopilot", vehicle, enabled); } diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index eb4f840ec..b194ddeba 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -191,6 +191,10 @@ namespace detail { rpc::ActorId actor, bool enabled); + void SetActorEnableGravity( + rpc::ActorId actor, + bool enabled); + void SetActorAutopilot( rpc::ActorId vehicle, bool enabled); diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index 0ceacf50c..064e9ea61 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -378,6 +378,10 @@ namespace detail { _client.SetActorSimulatePhysics(actor.GetId(), enabled); } + void SetActorEnableGravity(Actor &actor, bool enabled) { + _client.SetActorEnableGravity(actor.GetId(), enabled); + } + /// @} // ========================================================================= /// @name Operations with vehicles diff --git a/LibCarla/source/carla/rpc/Command.h b/LibCarla/source/carla/rpc/Command.h index b029b4d2e..946f69a90 100644 --- a/LibCarla/source/carla/rpc/Command.h +++ b/LibCarla/source/carla/rpc/Command.h @@ -172,6 +172,16 @@ namespace rpc { MSGPACK_DEFINE_ARRAY(actor, enabled); }; + struct SetEnableGravity : CommandBase { + SetEnableGravity() = default; + SetEnableGravity(ActorId id, bool value) + : actor(id), + enabled(value) {} + ActorId actor; + bool enabled; + MSGPACK_DEFINE_ARRAY(actor, enabled); + }; + struct SetAutopilot : CommandBase { SetAutopilot() = default; SetAutopilot( @@ -213,6 +223,7 @@ namespace rpc { ApplyAngularImpulse, ApplyTorque, SetSimulatePhysics, + SetEnableGravity, SetAutopilot, SetVehicleLightState>; diff --git a/PythonAPI/carla/source/libcarla/Actor.cpp b/PythonAPI/carla/source/libcarla/Actor.cpp index efe97a0ab..9aec37bd8 100644 --- a/PythonAPI/carla/source/libcarla/Actor.cpp +++ b/PythonAPI/carla/source/libcarla/Actor.cpp @@ -104,6 +104,7 @@ void export_actor() { .def("add_angular_impulse", &cc::Actor::AddAngularImpulse, (arg("angular_impulse"))) .def("add_torque", &cc::Actor::AddTorque, (arg("torque"))) .def("set_simulate_physics", &cc::Actor::SetSimulatePhysics, (arg("enabled") = true)) + .def("set_enable_gravity", &cc::Actor::SetEnableGravity, (arg("enabled") = true)) .def("destroy", CALL_WITHOUT_GIL(cc::Actor, Destroy)) .def(self_ns::str(self_ns::self)) ; diff --git a/PythonAPI/carla/source/libcarla/Commands.cpp b/PythonAPI/carla/source/libcarla/Commands.cpp index 2edd79387..e9a44d91f 100644 --- a/PythonAPI/carla/source/libcarla/Commands.cpp +++ b/PythonAPI/carla/source/libcarla/Commands.cpp @@ -172,6 +172,13 @@ void export_commands() { .def_readwrite("enabled", &cr::Command::SetSimulatePhysics::enabled) ; + class_("SetEnableGravity") + .def("__init__", &command_impl::CustomInit, (arg("actor"), arg("enabled"))) + .def(init((arg("actor_id"), arg("enabled")))) + .def_readwrite("actor_id", &cr::Command::SetEnableGravity::actor) + .def_readwrite("enabled", &cr::Command::SetEnableGravity::enabled) + ; + class_("SetAutopilot") .def("__init__", &command_impl::CustomInit, (arg("actor"), arg("enabled"), arg("tm_port") = TM_DEFAULT_PORT )) .def(init((arg("actor_id"), arg("enabled"), arg("tm_port") = TM_DEFAULT_PORT ))) @@ -200,6 +207,7 @@ void export_commands() { implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); + implicitly_convertible(); implicitly_convertible(); implicitly_convertible(); } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index 6223d82f9..8aebe8db6 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -828,12 +828,14 @@ void FCarlaServer::FPimpl::BindActions() RESPOND_ERROR("unable to set actor simulate physics: actor not found"); } + UE_LOG(LogCarla, Warning, TEXT("CarlaServer: set_actor_simulate_physics %d!!!!"), bEnabled); auto Character = Cast(ActorView.GetActor()); // The physics in the walkers works in a different way so to disable them, // we need to do it in the UCharacterMovementComponent. if (Character != nullptr) { auto CharacterMovement = Cast(Character->GetCharacterMovement()); + UE_LOG(LogCarla, Error, TEXT("Character::SetEnableGravity %d!!!!"), bEnabled); if(bEnabled) { CharacterMovement->SetDefaultMovementMode(); @@ -847,6 +849,7 @@ void FCarlaServer::FPimpl::BindActions() else { auto RootComponent = Cast(ActorView.GetActor()->GetRootComponent()); + UE_LOG(LogCarla, Error, TEXT("Root::SetEnableGravity %d!!!!"), bEnabled); if (RootComponent == nullptr) { RESPOND_ERROR("unable to set actor simulate physics: not supported by actor"); @@ -862,6 +865,49 @@ void FCarlaServer::FPimpl::BindActions() return R::Success(); }; + BIND_SYNC(set_actor_enable_gravity) << [this]( + cr::ActorId ActorId, + bool bEnabled) -> R + { + REQUIRE_CARLA_EPISODE(); + auto ActorView = Episode->FindActor(ActorId); + if (!ActorView.IsValid()) + { + RESPOND_ERROR("unable to set actor enable gravity: actor not found"); + } + + UE_LOG(LogCarla, Error, TEXT("CarlaServer: set_actor_enable_gravity %d!!!!"), bEnabled); + auto Character = Cast(ActorView.GetActor()); + // The physics in the walkers works in a different way so to disable them, + // we need to do it in the UCharacterMovementComponent. + if (Character != nullptr) + { + auto CharacterMovement = Cast(Character->GetCharacterMovement()); + UE_LOG(LogCarla, Error, TEXT("Character::SetEnableGravity %d!!!!"), bEnabled); + + if(bEnabled) { + CharacterMovement->GravityScale = 1.0f; + } + else { + CharacterMovement->GravityScale = 0.0f; + } + } + // In the rest of actors, the physics is controlled with the UPrimitiveComponent, so we use + // that for disable it. + else + { + auto RootComponent = Cast(ActorView.GetActor()->GetRootComponent()); + if (RootComponent == nullptr) + { + RESPOND_ERROR("unable to set actor enable gravity: not supported by actor"); + } + UE_LOG(LogCarla, Error, TEXT("SetEnableGravity %d!!!!"), bEnabled); + RootComponent->SetEnableGravity(bEnabled); + } + + return R::Success(); + }; + // ~~ Apply control ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BIND_SYNC(apply_control_to_vehicle) << [this]( @@ -1284,6 +1330,7 @@ void FCarlaServer::FPimpl::BindActions() [=](auto, const C::ApplyAngularImpulse &c) { MAKE_RESULT(add_actor_angular_impulse(c.actor, c.impulse)); }, [=](auto, const C::ApplyTorque &c) { MAKE_RESULT(add_actor_torque(c.actor, c.torque)); }, [=](auto, const C::SetSimulatePhysics &c) { MAKE_RESULT(set_actor_simulate_physics(c.actor, c.enabled)); }, + [=](auto, const C::SetEnableGravity &c) { MAKE_RESULT(set_actor_enable_gravity(c.actor, c.enabled)); }, // TODO: SetAutopilot should be removed. This is the old way to control the vehicles [=](auto, const C::SetAutopilot &c) { MAKE_RESULT(set_actor_autopilot(c.actor, c.enabled)); }, [=](auto, const C::SetVehicleLightState &c) { MAKE_RESULT(set_vehicle_light_state(c.actor, c.light_state)); },