Skip to main content

Move2DSystem.ixx File

System for processing 2D movement physics. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacemodules

Domain-specific components and systems. More...

namespacephysics

Physics simulation and collision detection subsystem for the game engine. More...

namespacemotion

Motion physics components and systems. More...

namespacesystems

Classes Index

classMove2DSystem

System that processes 2D movement for entities. More...

Description

System for processing 2D movement physics.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file Move2DSystem.ixx
3 * @brief System for processing 2D movement physics.
4 */
5module;
6
7
8#include <algorithm>
9#include <cassert>
10#include <cmath>
11
12export module helios.engine.modules.physics.motion.systems.Move2DSystem;
13
14
15
16import helios.math;
17
18import helios.engine.runtime.world.GameWorld;
19import helios.engine.modules.physics.motion.components.Move2DComponent;
20import helios.engine.modules.spatial.transform.components.TranslationStateComponent;
21import helios.engine.modules.physics.motion.components.DirectionComponent;
22
23import helios.engine.runtime.world.UpdateContext;
24
25import helios.engine.mechanics.lifecycle.components.Active;
26
27import helios.engine.common.tags.SystemRole;
28
30
31 /**
32 * @brief System that processes 2D movement for entities.
33 *
34 * @details This system reads from Move2DComponent and applies physics simulation
35 * including velocity integration and dampening when input is inactive.
36 *
37 * The system updates TranslationStateComponent with the computed translation
38 * changes each frame.
39 *
40 * Required components:
41 * - Move2DComponent (physics configuration and state)
42 * - DirectionComponent (current movement direction)
43 * - TranslationStateComponent (receives translation updates)
44 */
46
47 private:
48
49
50
51 /**
52 * @brief Computes translation delta for an entity based on its Move2DComponent state.
53 *
54 * @details Calculates velocity changes based on input state. When input is active,
55 * accelerates in the facing direction. When inactive, applies exponential
56 * drag to slow down. Velocity is clamped to maximum speed.
57 *
58 * @param cmp Pointer to the Move2DComponent.
59 * @param currentDirection The current direction vector from DirectionComponent.
60 * @param deltaTime Frame delta time in seconds.
61 *
62 * @return Translation delta to apply to the entity this frame.
63 */
64 [[nodiscard]] static helios::math::vec3f moveGameObject(
66 helios::math::vec3f currentDirection,
67 float deltaTime
68 ) noexcept {
69
70 assert(cmp != nullptr && "Unexpected invalid Move2DComponent passed");
71 assert(deltaTime >= 0 && "Unexpected negative value for deltaTime");
72
73 if (deltaTime == 0) {
74 return helios::math::vec3f{0.0f};
75 }
76
77 bool movementStateChanged = cmp->throttle() > helios::math::EPSILON_LENGTH;
78
79
80 auto velocity = cmp->velocity();
81
82 if (!movementStateChanged) {
83 float movementDampening = cmp->movementDampening();
84
85 // Apply exponential drag when no input is active.
86 // This creates a smooth deceleration effect (velocity approaches zero over time).
87 const float drag = std::pow(movementDampening, deltaTime);
88 velocity = velocity * drag;
89
90 if (velocity.length() <= helios::math::EPSILON_LENGTH) {
91 velocity = {0.0f, 0.0f, 0.0f};
92 }
93
94 } else {
95
96 if (cmp->useInstantAcceleration()) {
97 velocity = currentDirection * cmp->movementSpeed() * cmp->throttle();
98 } else {
99 // Accelerate in the current facing direction.
100 // Uses throttle (input intensity) to scale acceleration.
101 velocity = velocity + (currentDirection * (cmp->movementAcceleration() * cmp->throttle() * deltaTime));
102 }
103
104 }
105
106 // Clamp velocity to maximum speed to prevent unlimited acceleration.
107 if (velocity.length() > cmp->movementSpeed()) {
108 velocity = velocity.normalize() * cmp->movementSpeed();
109 }
110
111 cmp->setVelocity(velocity);
112
113 return (velocity + cmp->inheritedVelocity()) * deltaTime;
114 }
115
116
117 public:
118
120 /**
121 * @brief Updates movement for all applicable entities.
122 *
123 * @details For each entity with Move2DComponent, computes translation
124 * changes and applies them to the TranslationStateComponent.
125 *
126 * @param updateContext Context containing deltaTime and other frame data.
127 */
129
130 for (auto [entity, m2d, dc, tsc, active] : updateContext.view<
135 >().whereEnabled()) {
136
137 helios::math::vec3f translationDelta;
138
139 translationDelta = moveGameObject(m2d, dc->direction(), updateContext.deltaTime());
140
141 tsc->translateBy(translationDelta);
142 }
143
144 }
145
146
147 };
148
149};

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.