Skip to main content

SteeringSystem Class

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

Declaration

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

Base class

classSystem

Abstract base class for game systems. More...

Public Member Functions Index

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

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 38 of file SteeringSystem.ixx.

Public Member Functions

update()

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

Updates the heading of entities.

Parameters
updateContext

Context containing frame timing and game state.

Definition at line 120 of file SteeringSystem.ixx.

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

References helios::math::degrees, helios::engine::ecs::System::gameWorld_ 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 51 of file SteeringSystem.ixx.

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

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.