New API function to add angular impulse to an actor

This commit is contained in:
bernat 2020-05-04 10:37:09 +02:00 committed by Marc Garcia Puig
parent bcf542f579
commit 4dc6eba755
12 changed files with 106 additions and 7 deletions

View File

@ -1,3 +1,7 @@
## Latest
* Added API function **add_angular_impulse** to add angular impulse to any actor
## CARLA 0.9.9 ## CARLA 0.9.9
* Introduced hybrid mode for Traffic Manager * Introduced hybrid mode for Traffic Manager

View File

@ -28,10 +28,6 @@ namespace client {
return GetEpisode().Lock()->GetActorAngularVelocity(*this); return GetEpisode().Lock()->GetActorAngularVelocity(*this);
} }
void Actor::SetAngularVelocity(const geom::Vector3D &vector) {
GetEpisode().Lock()->SetActorAngularVelocity(*this, vector);
}
geom::Vector3D Actor::GetAcceleration() const { geom::Vector3D Actor::GetAcceleration() const {
return GetEpisode().Lock()->GetActorAcceleration(*this); return GetEpisode().Lock()->GetActorAcceleration(*this);
} }
@ -48,10 +44,18 @@ namespace client {
GetEpisode().Lock()->SetActorVelocity(*this, vector); GetEpisode().Lock()->SetActorVelocity(*this, vector);
} }
void Actor::SetAngularVelocity(const geom::Vector3D &vector) {
GetEpisode().Lock()->SetActorAngularVelocity(*this, vector);
}
void Actor::AddImpulse(const geom::Vector3D &vector) { void Actor::AddImpulse(const geom::Vector3D &vector) {
GetEpisode().Lock()->AddActorImpulse(*this, vector); GetEpisode().Lock()->AddActorImpulse(*this, vector);
} }
void Actor::AddAngularImpulse(const geom::Vector3D &vector) {
GetEpisode().Lock()->AddActorAngularImpulse(*this, vector);
}
void Actor::SetSimulatePhysics(const bool enabled) { void Actor::SetSimulatePhysics(const bool enabled) {
GetEpisode().Lock()->SetActorSimulatePhysics(*this, enabled); GetEpisode().Lock()->SetActorSimulatePhysics(*this, enabled);
} }

View File

@ -67,15 +67,18 @@ namespace client {
/// Set the actor velocity. /// Set the actor velocity.
void SetVelocity(const geom::Vector3D &vector); void SetVelocity(const geom::Vector3D &vector);
/// Set the angular velocity of the actor
void SetAngularVelocity(const geom::Vector3D &vector);
/// Add impulse to the actor. /// Add impulse to the actor.
void AddImpulse(const geom::Vector3D &vector); void AddImpulse(const geom::Vector3D &vector);
/// Add angular impulse to the actor.
void AddAngularImpulse(const geom::Vector3D &vector);
/// Enable or disable physics simulation on this actor. /// Enable or disable physics simulation on this actor.
void SetSimulatePhysics(bool enabled = true); void SetSimulatePhysics(bool enabled = true);
/// Set the angular velocity of the actor
void SetAngularVelocity(const geom::Vector3D &vector);
/// @warning This method only checks whether this instance of Actor has /// @warning This method only checks whether this instance of Actor has
/// called the Destroy() method, it does not check whether the actor is /// called the Destroy() method, it does not check whether the actor is
/// actually alive in the simulator. /// actually alive in the simulator.

View File

@ -269,6 +269,10 @@ namespace detail {
_pimpl->AsyncCall("add_actor_impulse", actor, vector); _pimpl->AsyncCall("add_actor_impulse", actor, vector);
} }
void Client::AddActorAngularImpulse(rpc::ActorId actor, const geom::Vector3D &vector) {
_pimpl->AsyncCall("add_actor_angular_impulse", actor, vector);
}
void Client::SetActorSimulatePhysics(rpc::ActorId actor, const bool enabled) { void Client::SetActorSimulatePhysics(rpc::ActorId actor, const bool enabled) {
_pimpl->AsyncCall("set_actor_simulate_physics", actor, enabled); _pimpl->AsyncCall("set_actor_simulate_physics", actor, enabled);
} }

View File

@ -159,6 +159,10 @@ namespace detail {
rpc::ActorId actor, rpc::ActorId actor,
const geom::Vector3D &vector); const geom::Vector3D &vector);
void AddActorAngularImpulse(
rpc::ActorId actor,
const geom::Vector3D &vector);
void SetActorSimulatePhysics( void SetActorSimulatePhysics(
rpc::ActorId actor, rpc::ActorId actor,
bool enabled); bool enabled);

View File

@ -325,6 +325,10 @@ namespace detail {
_client.AddActorImpulse(actor.GetId(), vector); _client.AddActorImpulse(actor.GetId(), vector);
} }
void AddActorAngularImpulse(const Actor &actor, const geom::Vector3D &vector) {
_client.AddActorAngularImpulse(actor.GetId(), vector);
}
geom::Vector3D GetActorAcceleration(const Actor &actor) const { geom::Vector3D GetActorAcceleration(const Actor &actor) const {
return GetActorSnapshot(actor).acceleration; return GetActorSnapshot(actor).acceleration;
} }

View File

@ -131,6 +131,16 @@ namespace rpc {
MSGPACK_DEFINE_ARRAY(actor, impulse); MSGPACK_DEFINE_ARRAY(actor, impulse);
}; };
struct ApplyAngularImpulse : CommandBase<ApplyAngularImpulse> {
ApplyAngularImpulse() = default;
ApplyAngularImpulse(ActorId id, const geom::Vector3D &value)
: actor(id),
impulse(value) {}
ActorId actor;
geom::Vector3D impulse;
MSGPACK_DEFINE_ARRAY(actor, impulse);
};
struct SetSimulatePhysics : CommandBase<SetSimulatePhysics> { struct SetSimulatePhysics : CommandBase<SetSimulatePhysics> {
SetSimulatePhysics() = default; SetSimulatePhysics() = default;
SetSimulatePhysics(ActorId id, bool value) SetSimulatePhysics(ActorId id, bool value)
@ -168,6 +178,7 @@ namespace rpc {
ApplyVelocity, ApplyVelocity,
ApplyAngularVelocity, ApplyAngularVelocity,
ApplyImpulse, ApplyImpulse,
ApplyAngularImpulse,
SetSimulatePhysics, SetSimulatePhysics,
SetAutopilot>; SetAutopilot>;

View File

@ -86,6 +86,7 @@ void export_actor() {
.def("set_velocity", &cc::Actor::SetVelocity, (arg("vector"))) .def("set_velocity", &cc::Actor::SetVelocity, (arg("vector")))
.def("set_angular_velocity", &cc::Actor::SetAngularVelocity, (arg("vector"))) .def("set_angular_velocity", &cc::Actor::SetAngularVelocity, (arg("vector")))
.def("add_impulse", &cc::Actor::AddImpulse, (arg("vector"))) .def("add_impulse", &cc::Actor::AddImpulse, (arg("vector")))
.def("add_angular_impulse", &cc::Actor::AddAngularImpulse, (arg("vector")))
.def("set_simulate_physics", &cc::Actor::SetSimulatePhysics, (arg("enabled") = true)) .def("set_simulate_physics", &cc::Actor::SetSimulatePhysics, (arg("enabled") = true))
.def("destroy", CALL_WITHOUT_GIL(cc::Actor, Destroy)) .def("destroy", CALL_WITHOUT_GIL(cc::Actor, Destroy))
.def(self_ns::str(self_ns::self)) .def(self_ns::str(self_ns::self))

View File

@ -144,6 +144,13 @@ void export_commands() {
.def_readwrite("impulse", &cr::Command::ApplyImpulse::impulse) .def_readwrite("impulse", &cr::Command::ApplyImpulse::impulse)
; ;
class_<cr::Command::ApplyAngularImpulse>("ApplyAngularImpulse")
.def("__init__", &command_impl::CustomInit<ActorPtr, cg::Vector3D>, (arg("actor"), arg("impulse")))
.def(init<cr::ActorId, cg::Vector3D>((arg("actor_id"), arg("impulse"))))
.def_readwrite("actor_id", &cr::Command::ApplyAngularImpulse::actor)
.def_readwrite("impulse", &cr::Command::ApplyAngularImpulse::impulse)
;
class_<cr::Command::SetSimulatePhysics>("SetSimulatePhysics") class_<cr::Command::SetSimulatePhysics>("SetSimulatePhysics")
.def("__init__", &command_impl::CustomInit<ActorPtr, bool>, (arg("actor"), arg("enabled"))) .def("__init__", &command_impl::CustomInit<ActorPtr, bool>, (arg("actor"), arg("enabled")))
.def(init<cr::ActorId, bool>((arg("actor_id"), arg("enabled")))) .def(init<cr::ActorId, bool>((arg("actor_id"), arg("enabled"))))
@ -168,6 +175,7 @@ void export_commands() {
implicitly_convertible<cr::Command::ApplyVelocity, cr::Command>(); implicitly_convertible<cr::Command::ApplyVelocity, cr::Command>();
implicitly_convertible<cr::Command::ApplyAngularVelocity, cr::Command>(); implicitly_convertible<cr::Command::ApplyAngularVelocity, cr::Command>();
implicitly_convertible<cr::Command::ApplyImpulse, cr::Command>(); implicitly_convertible<cr::Command::ApplyImpulse, cr::Command>();
implicitly_convertible<cr::Command::ApplyAngularImpulse, cr::Command>();
implicitly_convertible<cr::Command::SetSimulatePhysics, cr::Command>(); implicitly_convertible<cr::Command::SetSimulatePhysics, cr::Command>();
implicitly_convertible<cr::Command::SetAutopilot, cr::Command>(); implicitly_convertible<cr::Command::SetAutopilot, cr::Command>();
} }

View File

@ -38,6 +38,13 @@
doc: > doc: >
Adds an impulse to the actor. Adds an impulse to the actor.
# -------------------------------------- # --------------------------------------
- def_name: add_angular_impulse
params:
- param_name: impulse
type: carla.Vector3D
doc: >
Adds an angular impulse to the actor (in degrees).
# --------------------------------------
- def_name: destroy - def_name: destroy
return: bool return: bool
doc: > doc: >

View File

@ -278,6 +278,32 @@
type: carla.Vector3D type: carla.Vector3D
# -------------------------------------- # --------------------------------------
- class_name: ApplyAngularImpulse
# - DESCRIPTION ------------------------
doc: >
Command adaptation of **<font color="#7fb800">add_angular_impulse()</font>** in carla.Actor. Adds angular impulse to an actor.
# - PROPERTIES -------------------------
instance_variables:
- var_name: actor_id
type: int
doc: >
Actor affected by the command.
- var_name: impulse
type: carla.Vector3D
doc: >
Angular impulse applied to the actor.
# - METHODS ----------------------------
methods:
- def_name: __init__
params:
- param_name: actor
type: carla.Actor or int
doc: >
Actor or its ID to whom the command will be applied to.
- param_name: impulse
type: carla.Vector3D
# --------------------------------------
- class_name: SetSimulatePhysics - class_name: SetSimulatePhysics
# - DESCRIPTION ------------------------ # - DESCRIPTION ------------------------
doc: > doc: >

View File

@ -586,6 +586,28 @@ void FCarlaServer::FPimpl::BindActions()
return R<void>::Success(); return R<void>::Success();
}; };
BIND_SYNC(add_actor_angular_impulse) << [this](
cr::ActorId ActorId,
cr::Vector3D vector) -> R<void>
{
REQUIRE_CARLA_EPISODE();
auto ActorView = Episode->FindActor(ActorId);
if (!ActorView.IsValid())
{
RESPOND_ERROR("unable to add actor angular impulse: actor not found");
}
auto RootComponent = Cast<UPrimitiveComponent>(ActorView.GetActor()->GetRootComponent());
if (RootComponent == nullptr)
{
RESPOND_ERROR("unable to add actor angular impulse: not supported by actor");
}
RootComponent->AddAngularImpulseInDegrees(
vector.ToFVector(),
"None",
false);
return R<void>::Success();
};
BIND_SYNC(get_physics_control) << [this]( BIND_SYNC(get_physics_control) << [this](
cr::ActorId ActorId) -> R<cr::VehiclePhysicsControl> cr::ActorId ActorId) -> R<cr::VehiclePhysicsControl>
{ {
@ -1031,6 +1053,7 @@ void FCarlaServer::FPimpl::BindActions()
[=](auto, const C::ApplyVelocity &c) { MAKE_RESULT(set_actor_velocity(c.actor, c.velocity)); }, [=](auto, const C::ApplyVelocity &c) { MAKE_RESULT(set_actor_velocity(c.actor, c.velocity)); },
[=](auto, const C::ApplyAngularVelocity &c) { MAKE_RESULT(set_actor_angular_velocity(c.actor, c.angular_velocity)); }, [=](auto, const C::ApplyAngularVelocity &c) { MAKE_RESULT(set_actor_angular_velocity(c.actor, c.angular_velocity)); },
[=](auto, const C::ApplyImpulse &c) { MAKE_RESULT(add_actor_impulse(c.actor, c.impulse)); }, [=](auto, const C::ApplyImpulse &c) { MAKE_RESULT(add_actor_impulse(c.actor, c.impulse)); },
[=](auto, const C::ApplyAngularImpulse &c) { MAKE_RESULT(add_actor_angular_impulse(c.actor, c.impulse)); },
[=](auto, const C::SetSimulatePhysics &c) { MAKE_RESULT(set_actor_simulate_physics(c.actor, c.enabled)); }, [=](auto, const C::SetSimulatePhysics &c) { MAKE_RESULT(set_actor_simulate_physics(c.actor, c.enabled)); },
// TODO: SetAutopilot should be removed. This is the old way to control the vehicles // 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::SetAutopilot &c) { MAKE_RESULT(set_actor_autopilot(c.actor, c.enabled)); },