Added EnableGravity and better SetSimulatePhysics

This commit is contained in:
Daniel Santos-Olivan 2020-09-30 13:11:54 +02:00 committed by Marc Garcia Puig
parent 83d752ef94
commit 95abf25a10
9 changed files with 88 additions and 0 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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);
}

View File

@ -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);

View File

@ -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

View File

@ -172,6 +172,16 @@ namespace rpc {
MSGPACK_DEFINE_ARRAY(actor, enabled);
};
struct SetEnableGravity : CommandBase<SetEnableGravity> {
SetEnableGravity() = default;
SetEnableGravity(ActorId id, bool value)
: actor(id),
enabled(value) {}
ActorId actor;
bool enabled;
MSGPACK_DEFINE_ARRAY(actor, enabled);
};
struct SetAutopilot : CommandBase<SetAutopilot> {
SetAutopilot() = default;
SetAutopilot(
@ -213,6 +223,7 @@ namespace rpc {
ApplyAngularImpulse,
ApplyTorque,
SetSimulatePhysics,
SetEnableGravity,
SetAutopilot,
SetVehicleLightState>;

View File

@ -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))
;

View File

@ -172,6 +172,13 @@ void export_commands() {
.def_readwrite("enabled", &cr::Command::SetSimulatePhysics::enabled)
;
class_<cr::Command::SetEnableGravity>("SetEnableGravity")
.def("__init__", &command_impl::CustomInit<ActorPtr, bool>, (arg("actor"), arg("enabled")))
.def(init<cr::ActorId, bool>((arg("actor_id"), arg("enabled"))))
.def_readwrite("actor_id", &cr::Command::SetEnableGravity::actor)
.def_readwrite("enabled", &cr::Command::SetEnableGravity::enabled)
;
class_<cr::Command::SetAutopilot>("SetAutopilot")
.def("__init__", &command_impl::CustomInit<ActorPtr, bool, uint16_t>, (arg("actor"), arg("enabled"), arg("tm_port") = TM_DEFAULT_PORT ))
.def(init<cr::ActorId, bool, uint16_t>((arg("actor_id"), arg("enabled"), arg("tm_port") = TM_DEFAULT_PORT )))
@ -200,6 +207,7 @@ void export_commands() {
implicitly_convertible<cr::Command::ApplyAngularImpulse, cr::Command>();
implicitly_convertible<cr::Command::ApplyTorque, cr::Command>();
implicitly_convertible<cr::Command::SetSimulatePhysics, cr::Command>();
implicitly_convertible<cr::Command::SetEnableGravity, cr::Command>();
implicitly_convertible<cr::Command::SetAutopilot, cr::Command>();
implicitly_convertible<cr::Command::SetVehicleLightState, cr::Command>();
}

View File

@ -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<ACharacter>(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<UCharacterMovementComponent>(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<UPrimitiveComponent>(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<void>::Success();
};
BIND_SYNC(set_actor_enable_gravity) << [this](
cr::ActorId ActorId,
bool bEnabled) -> R<void>
{
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<ACharacter>(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<UCharacterMovementComponent>(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<UPrimitiveComponent>(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<void>::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)); },