Skip to content
Snippets Groups Projects
Commit 00e8a63f authored by Andreas Rauschert's avatar Andreas Rauschert
Browse files

Merge branch 'bugfix/set_speed' into 'master'

Helper function SetSpeed does not change the movement direction

See merge request eclipse/simopenpass/scenario_api!31
parents 4f665cc7 e6150846
No related branches found
No related tags found
No related merge requests found
...@@ -15,23 +15,48 @@ ...@@ -15,23 +15,48 @@
#ifndef MANTLEAPI_TRAFFIC_ENTITY_HELPER_H #ifndef MANTLEAPI_TRAFFIC_ENTITY_HELPER_H
#define MANTLEAPI_TRAFFIC_ENTITY_HELPER_H #define MANTLEAPI_TRAFFIC_ENTITY_HELPER_H
#include <MantleAPI/Common/floating_point_helper.h>
#include <MantleAPI/Traffic/i_entity.h> #include <MantleAPI/Traffic/i_entity.h>
#include <cmath> #include <cmath>
namespace mantle_api namespace mantle_api
{ {
/// Set the speed (i.e. the length of the velocity vector) of an entity. The direction of the velocity vector does not
inline void SetSpeed(mantle_api::IEntity* entity, const units::velocity::meters_per_second_t &velocity) /// change. If the entity's speed is zero, then the function uses the orientation of the entity to derive the direction
/// of the velocity vector.
///
/// @param entity pointer to the entity
/// @param velocity the new speed to set
inline void SetSpeed(mantle_api::IEntity* entity, const units::velocity::meters_per_second_t& velocity)
{ {
auto orientation = entity->GetOrientation(); using namespace units::literals;
if (entity == nullptr)
{
throw std::runtime_error("entity is null");
}
auto current_velocity = entity->GetVelocity();
if (IsEqual(current_velocity.Length(), 0_mps))
{
auto orientation = entity->GetOrientation();
auto cos_elevation = units::math::cos(orientation.pitch); auto cos_elevation = units::math::cos(orientation.pitch);
mantle_api::Vec3<units::velocity::meters_per_second_t> velocity_vector{velocity * units::math::cos(orientation.yaw) * cos_elevation, mantle_api::Vec3<units::velocity::meters_per_second_t> velocity_vector{
velocity * units::math::sin(orientation.yaw) * cos_elevation, velocity * units::math::cos(orientation.yaw) * cos_elevation,
velocity * -units::math::sin(orientation.pitch)}; velocity * units::math::sin(orientation.yaw) * cos_elevation,
velocity * -units::math::sin(orientation.pitch)};
entity->SetVelocity(velocity_vector); entity->SetVelocity(velocity_vector);
}
else
{
mantle_api::Vec3<units::velocity::meters_per_second_t> velocity_vector =
(current_velocity / current_velocity.Length()()) * velocity();
entity->SetVelocity(velocity_vector);
}
} }
} // namespace mantle_api } // namespace mantle_api
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment