Skip to main content

LevelBoundsBehaviorSystem Class

System that handles entity behavior when colliding with level boundaries. More...

Declaration

class helios::engine::game::systems::physics::LevelBoundsBehaviorSystem { ... }

Base class

classSystem

Abstract base class for game systems. More...

Public Member Functions Index

voidupdate (helios::engine::game::UpdateContext &updateContext) noexcept override

Updates all entities that may have left level bounds. More...

Private Static Functions Index

static helios::math::vec3fbounce (helios::math::vec3f worldTranslation, helios::math::aabbf objectBounds, helios::math::aabbf levelBounds, helios::engine::game::components::physics::Move2DComponent &m2d, helios::engine::game::components::physics::LevelBoundsBehaviorComponent &bbc, helios::engine::game::components::physics::DirectionComponent &dc) noexcept

Applies bounce behavior to an entity that has left level bounds. More...

Description

System that handles entity behavior when colliding with level boundaries.

This system checks if entities with movement components have exceeded the level bounds. When an entity leaves the bounds, it applies bounce behavior based on the LevelBoundsBehaviorComponent's restitution coefficient.

Definition at line 41 of file LevelBoundsBehaviorSystem.ixx.

Public Member Functions

update()

void helios::engine::game::systems::physics::LevelBoundsBehaviorSystem::update (helios::engine::game::UpdateContext & updateContext)
inline noexcept virtual

Updates all entities that may have left level bounds.

For each entity with the required components, checks if its world-space AABB is within level bounds. If not, applies bounce behavior.

Parameters
updateContext

Context containing deltaTime and other frame data.

Definition at line 54 of file LevelBoundsBehaviorSystem.ixx.

54 void update(helios::engine::game::UpdateContext& updateContext) noexcept override {
55
56 for (auto [entity, m2d, ab, sc, dc, tsc, bc, bbc] : gameWorld_->find<
64 >().each()) {
65
66
67 auto& objectBounds = bc->bounds();
68 auto levelBounds = gameWorld_->level().bounds();
69
70 if (!levelBounds.contains(objectBounds)) {
71
72 helios::math::mat4f parentWorldTransform = sc->sceneNode()->parent()->worldTransform();
73 helios::math::vec4f childWorldTranslation = parentWorldTransform * tsc->translation().toVec4(1.0f);
74
75 auto bouncedWorldTranslation = bounce(
76 childWorldTranslation.toVec3(), objectBounds, levelBounds, *m2d, *bbc, *dc
77 );
78
79 auto parentTransform_inverse = parentWorldTransform.inverse();
80
81 auto childLocalTranslation = parentTransform_inverse * bouncedWorldTranslation.toVec4(1.0f);
82
83 tsc->setTranslation(childLocalTranslation.toVec3());
84 bc->setBounds(ab->aabb().applyTransform(
85 parentWorldTransform.withTranslation(bouncedWorldTranslation)
86 ));
87 }
88 }
89 }

References helios::engine::game::System::gameWorld_, helios::math::mat4< T >::inverse, helios::math::vec4< T >::toVec3, helios::math::mat4< T >::translation and helios::math::mat4< T >::withTranslation.

Private Static Functions

bounce()

helios::math::vec3f helios::engine::game::systems::physics::LevelBoundsBehaviorSystem::bounce (helios::math::vec3f worldTranslation, helios::math::aabbf objectBounds, helios::math::aabbf levelBounds, helios::engine::game::components::physics::Move2DComponent & m2d, helios::engine::game::components::physics::LevelBoundsBehaviorComponent & bbc, helios::engine::game::components::physics::DirectionComponent & dc)
inline nodiscard noexcept static

Applies bounce behavior to an entity that has left level bounds.

Calculates the corrected position and reflected velocity when an entity collides with level boundaries. The restitution coefficient determines how much velocity is preserved after the bounce. The DirectionComponent will be updated with the new direction.

Parameters
worldTranslation

Current world position of the entity.

objectBounds

World-space AABB of the entity.

levelBounds

World-space AABB of the level.

m2d

Reference to the Move2DComponent for velocity modification.

bbc

Reference to the LevelBoundsBehaviorComponent for restitution.

dc

Reference to the DirectionComponent for direction update.

Returns

Corrected world position after bounce.

Definition at line 111 of file LevelBoundsBehaviorSystem.ixx.

111 [[nodiscard]] static helios::math::vec3f bounce(
112 helios::math::vec3f worldTranslation,
113 helios::math::aabbf objectBounds,
114 helios::math::aabbf levelBounds,
118 ) noexcept {
119
120
121 auto velocity = m2d.velocity();
122 bool hitX = false;
123 bool hitY = false;
124 auto translation = worldTranslation;
125
126 // Check left boundary collision
127 if (objectBounds.min()[0] < levelBounds.min()[0]) {
128 translation[0] += (levelBounds.min()[0] - objectBounds.min()[0]);
129 if (velocity[0] < 0) {
130 velocity = (velocity.withX(bbc.restitution() * -velocity[0]));
131 }
132 hitX = true;
133 // Check right boundary collision
134 } else if (objectBounds.max()[0] > levelBounds.max()[0]) {
135 translation[0] -= (objectBounds.max()[0] - levelBounds.max()[0]) ;
136 if (velocity[0] > 0) {
137 velocity = (velocity.withX(bbc.restitution() * -velocity[0]));
138 }
139 hitX = true;
140 }
141
142 objectBounds = objectBounds + (translation - worldTranslation);
143
144 // Check bottom boundary collision
145 if (objectBounds.min()[1] < levelBounds.min()[1]) {
146 translation[1] += (levelBounds.min()[1] - objectBounds.min()[1]);
147 if (velocity[1] < 0) {
148 velocity = velocity.withY(bbc.restitution() * -velocity[1]);
149 }
150 hitY = true;
151 // Check top boundary collision
152 } else if (objectBounds.max()[1] > levelBounds.max()[1]) {
153 translation[1] -= (objectBounds.max()[1] - levelBounds.max()[1]);
154 if (velocity[1] > 0) {
155 velocity = velocity.withY(bbc.restitution() * -velocity[1]);
156 }
157 hitY = true;
158 }
159
160 if (hitX || hitY) {
161 m2d.setVelocity(velocity);
162 dc.setDirection(velocity.normalize());
163 }
164
165 return translation;
166 }

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.