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
15import helios.engine.ecs.System;
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
28
29 /**
30 * @brief System that processes 2D movement for entities.
31 *
32 * @details This system reads from Move2DComponent and applies physics simulation
33 * including velocity integration and dampening when input is inactive.
34 *
35 * The system updates TranslationStateComponent with the computed translation
36 * changes each frame.
37 *
38 * Required components:
39 * - Move2DComponent (physics configuration and state)
40 * - DirectionComponent (current movement direction)
41 * - TranslationStateComponent (receives translation updates)
42 */
44
45 private:
46
47
48
49 /**
50 * @brief Computes translation delta for an entity based on its Move2DComponent state.
51 *
52 * @details Calculates velocity changes based on input state. When input is active,
53 * accelerates in the facing direction. When inactive, applies exponential
54 * drag to slow down. Velocity is clamped to maximum speed.
55 *
56 * @param cmp Pointer to the Move2DComponent.
57 * @param currentDirection The current direction vector from DirectionComponent.
58 * @param deltaTime Frame delta time in seconds.
59 *
60 * @return Translation delta to apply to the entity this frame.
61 */
62 [[nodiscard]] static helios::math::vec3f moveGameObject(
64 helios::math::vec3f currentDirection,
65 float deltaTime
66 ) noexcept {
67
68 assert(cmp != nullptr && "Unexpected invalid Move2DComponent passed");
69 assert(deltaTime >= 0 && "Unexpected negative value for deltaTime");
70
71 if (deltaTime == 0) {
72 return helios::math::vec3f{0.0f};
73 }
74
75 bool movementStateChanged = cmp->throttle() > helios::math::EPSILON_LENGTH;
76
77
78 auto velocity = cmp->velocity();
79
80 if (!movementStateChanged) {
81 float movementDampening = cmp->movementDampening();
82
83 // Apply exponential drag when no input is active.
84 // This creates a smooth deceleration effect (velocity approaches zero over time).
85 const float drag = std::pow(movementDampening, deltaTime);
86 velocity = velocity * drag;
87
88 if (velocity.length() <= helios::math::EPSILON_LENGTH) {
89 velocity = {0.0f, 0.0f, 0.0f};
90 }
91
92 } else {
93
94 if (cmp->useInstantAcceleration()) {
95 velocity = currentDirection * cmp->movementSpeed() * cmp->throttle();
96 } else {
97 // Accelerate in the current facing direction.
98 // Uses throttle (input intensity) to scale acceleration.
99 velocity = velocity + (currentDirection * (cmp->movementAcceleration() * cmp->throttle() * deltaTime));
100 }
101
102 }
103
104 // Clamp velocity to maximum speed to prevent unlimited acceleration.
105 if (velocity.length() > cmp->movementSpeed()) {
106 velocity = velocity.normalize() * cmp->movementSpeed();
107 }
108
109 cmp->setVelocity(velocity);
110
111 return (velocity + cmp->inheritedVelocity()) * deltaTime;
112 }
113
114 public:
115
116 /**
117 * @brief Called when the system is added to a GameWorld.
118 *
119 * @param gameWorld Pointer to the GameWorld this system belongs to.
120 */
121 void init(helios::engine::runtime::world::GameWorld& gameWorld) noexcept override {
122 System::init(gameWorld);
123 }
124
125 /**
126 * @brief Updates movement for all applicable entities.
127 *
128 * @details For each entity with Move2DComponent, computes translation
129 * changes and applies them to the TranslationStateComponent.
130 *
131 * @param updateContext Context containing deltaTime and other frame data.
132 */
133 void update(helios::engine::runtime::world::UpdateContext& updateContext) noexcept override {
134
135 for (auto [entity, m2d, dc, tsc, active] : gameWorld_->view<
140 >().whereEnabled()) {
141
142 helios::math::vec3f translationDelta;
143
144 translationDelta = moveGameObject(m2d, dc->direction(), updateContext.deltaTime());
145
146 tsc->translateBy(translationDelta);
147 }
148
149 }
150
151
152 };
153
154};

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.