Skip to main content

SteeringSystem Class

System that processes heading and rotation physics. More...

Declaration

class helios::engine::modules::physics::motion::systems::SteeringSystem { ... }

Public Member Typedefs Index

usingEngineRoleTag = helios::engine::common::tags::SystemRole

Public Member Functions Index

voidupdate (helios::engine::runtime::world::UpdateContext &updateContext) noexcept

Updates the heading of entities. More...

Private Member Functions Index

voidupdateHeading (helios::engine::modules::physics::motion::components::SteeringComponent *cmp, float deltaTime) noexcept

Performs the rotation physics update for a SteeringComponent. More...

Description

System that processes heading and rotation physics.

This system updates the rotation state of entities based on their SteeringComponent. It calculates the target rotation from input, applies rotation speed and dampening, and updates the RotationStateComponent and DirectionComponent.

Definition at line 40 of file SteeringSystem.ixx.

Public Member Typedefs

EngineRoleTag

using helios::engine::modules::physics::motion::systems::SteeringSystem::EngineRoleTag = helios::engine::common::tags::SystemRole

Public Member Functions

update()

void helios::engine::modules::physics::motion::systems::SteeringSystem::update (helios::engine::runtime::world::UpdateContext & updateContext)
inline noexcept

Updates the heading of entities.

Parameters
updateContext

Context containing frame timing and game state.

Definition at line 124 of file SteeringSystem.ixx.

125
126
127 for (auto [entity, hc, rsc, dc, active] : updateContext.view<
132 >().whereEnabled()) {
133
134
135 if (hc->stateChanged()) {
136 auto direction = hc->steeringInput();
137 auto targetRotationAngle = helios::math::degrees(std::atan2(direction[1], direction[0]));
138 auto rotationAngleDelta = std::fmod((targetRotationAngle - hc->currentRotationAngle()) + 540.0f, 360.0f) - 180.0f;
139
140 float turnBoostFactor = 0.5f;
141 float turnBoost = 1.0f + turnBoostFactor * std::clamp((abs(rotationAngleDelta))/180.f, 0.0f, 1.0f);
142 float currentRotationSpeed = turnBoost * hc->rotationSpeed() * hc->turnIntensity();
143
144 hc->setTargetRotationAngle(targetRotationAngle);
145 hc->setRotationAngleDelta(rotationAngleDelta);
146 hc->setCurrentRotationSpeed(currentRotationSpeed);
147
148 }
149
150 updateHeading(hc, updateContext.deltaTime());
151
152 float rotationAngle = hc->currentRotationAngle();
153 rsc->setHeadingRotationAngle(rotationAngle);
154 rsc->setHeadingRotationAxis(hc->rotationAxis());
155
156 float rad = helios::math::radians(rotationAngle);
157
158 if (hc->directionFromSteering()) {
159 dc->setDirection(helios::math::vec3f{cos(rad), sin(rad), 0.0f});
160 }
161
162
163 }
164
165 }

References helios::math::degrees and helios::math::radians.

Private Member Functions

updateHeading()

void helios::engine::modules::physics::motion::systems::SteeringSystem::updateHeading (helios::engine::modules::physics::motion::components::SteeringComponent * cmp, float deltaTime)
inline noexcept

Performs the rotation physics update for a SteeringComponent.

Applies dampening when input is inactive, calculates rotation steps, and snaps to target when within threshold.

Parameters
cmp

The SteeringComponent to update. Must not be nullptr.

deltaTime

Time elapsed since last frame in seconds.

Definition at line 53 of file SteeringSystem.ixx.

53 void updateHeading(
55 float deltaTime
56 ) noexcept {
57
58 assert(cmp != nullptr && "Unexpected invalid SteeringComponent passed");
59 assert(deltaTime >= 0 && "Unexpected negative value for deltaTime");
60
61 if (deltaTime == 0) {
62 return;
63 }
64
65 float currentRotationSpeed = cmp->currentRotationSpeed();
66 float currentRotationAngle = cmp->currentRotationAngle();
67 float rotationAngleDelta = cmp->rotationAngleDelta();
68 bool headingStateChanged = cmp->stateChanged();
69
70 // Apply rotation dampening when input is inactive
71 if (!headingStateChanged) {
72 currentRotationSpeed *= std::pow(cmp->rotationDampening(), deltaTime);
73
74 if (currentRotationSpeed < cmp->rotationSpeedThreshold()) {
75 currentRotationSpeed = 0.0f;
76 }
77
78 cmp->setCurrentRotationSpeed(currentRotationSpeed);
79 }
80
81 // Apply rotation step towards target angle
82 if (currentRotationSpeed > 0.0f) {
83
84 if (cmp->useInstantRotation()) {
85 currentRotationAngle = cmp->targetRotationAngle();
86
87 } else {
88 float rotationStep = std::copysign(
89 std::min(
90 std::abs(rotationAngleDelta),
91 currentRotationSpeed * deltaTime
92 ),
93 rotationAngleDelta
94 );
95 currentRotationAngle = currentRotationAngle + rotationStep;
96 }
97
98 rotationAngleDelta = std::fmod((cmp->targetRotationAngle() - currentRotationAngle) + 540.0f, 360.0f) - 180.0f;
99
100 // Snap to target when close enough
101 if (std::abs(rotationAngleDelta) <= 0.5f) {
102 currentRotationAngle = cmp->targetRotationAngle();
103 rotationAngleDelta = 0.0f;
104 currentRotationSpeed = 0.0f;
105 }
106
107 cmp->setCurrentRotationAngle(currentRotationAngle);
108 cmp->setRotationAngleDelta(rotationAngleDelta);
109 cmp->setCurrentRotationSpeed(currentRotationSpeed);
110 }
111
112 }

The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.