Skip to main content

LevelBoundsBehaviorSystem Class

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

Declaration

class helios::engine::mechanics::bounds::systems::LevelBoundsBehaviorSystem { ... }

Public Member Typedefs Index

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

Public Member Functions Index

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

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

Private Member Functions Index

voidupdateCollisionResponse (helios::engine::ecs::GameObject go, BounceResult bounceResult, helios::engine::modules::physics::collision::types::CollisionResponse collisionResponse)

Updates the entity's state based on the configured collision response. More...

BounceResultbounce (helios::math::vec3f worldTranslation, helios::math::aabbf objectBounds, helios::math::aabbf levelBounds, const float restitution, helios::engine::modules::physics::motion::components::Move2DComponent &m2d, helios::engine::modules::physics::motion::components::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 58 of file LevelBoundsBehaviorSystem.ixx.

Public Member Typedefs

EngineRoleTag

using helios::engine::mechanics::bounds::systems::LevelBoundsBehaviorSystem::EngineRoleTag = helios::engine::common::tags::SystemRole

Public Member Functions

update()

void helios::engine::mechanics::bounds::systems::LevelBoundsBehaviorSystem::update (helios::engine::runtime::world::UpdateContext & updateContext)
inline noexcept

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 90 of file LevelBoundsBehaviorSystem.ixx.

91
93
94 for (auto [entity, m2d, ab, sc, dc, tsc, bc, bbc, active] : updateContext.view<
103 >().whereEnabled()) {
104
105
106 auto& objectBounds = bc->bounds();
107 auto levelBounds = updateContext.level()->bounds();
108
109 if (!levelBounds.contains(objectBounds)) {
110
111 helios::math::mat4f parentWorldTransform = sc->sceneNode()->parent()->worldTransform();
112 helios::math::vec4f childWorldTranslation = parentWorldTransform * tsc->translation().toVec4(1.0f);
113
114 helios::math::vec3f bouncedWorldTranslation;
115
116 BounceResult bounceResult{};
117
118 if (bbc->collisionBehavior() == CollisionBehavior::Reflect || bbc->collisionBehavior() == CollisionBehavior::Bounce) {
119
120 bounceResult = bounce(
121 childWorldTranslation.toVec3(), objectBounds, levelBounds,
122 bbc->collisionBehavior() == CollisionBehavior::Reflect ? 1.0f : bbc->restitution(),
123 *m2d, *dc
124 );
125
126 bouncedWorldTranslation = bounceResult.translation;
127 updateCollisionResponse(entity, bounceResult, bbc->collisionResponse());
128
129 } else if (bbc->collisionBehavior() == CollisionBehavior::Despawn) {
130 /**
131 * @todo optimize
132 */
134 assert(sbp && "Unexpected missing SpawnProfile");
135
137 entity.entityHandle(), sbp->spawnProfileId()
138 );
139
140 }
141
142
143 if (bounceResult.hitX || bounceResult.hitY) {
144 m2d->setVelocity(bounceResult.velocity);
145 dc->setDirection(bounceResult.direction);
146 }
147
148 auto parentTransform_inverse = parentWorldTransform.inverse();
149 auto childLocalTranslation = parentTransform_inverse * bouncedWorldTranslation.toVec4(1.0f);
150
151 tsc->setTranslation(childLocalTranslation.toVec3());
152 bc->setBounds(ab->aabb().applyTransform(
153 parentWorldTransform.withTranslation(bouncedWorldTranslation)
154 ));
155
156 }
157 }
158 }

References helios::engine::modules::physics::collision::types::Bounce, helios::engine::modules::physics::collision::types::Despawn, helios::math::mat4< T >::inverse, helios::engine::modules::physics::collision::types::Reflect, helios::math::vec4< T >::toVec3, helios::math::vec3< T >::toVec4, helios::math::mat4< T >::translation and helios::math::mat4< T >::withTranslation.

Private Member Functions

bounce()

BounceResult helios::engine::mechanics::bounds::systems::LevelBoundsBehaviorSystem::bounce (helios::math::vec3f worldTranslation, helios::math::aabbf objectBounds, helios::math::aabbf levelBounds, const float restitution, helios::engine::modules::physics::motion::components::Move2DComponent & m2d, helios::engine::modules::physics::motion::components::DirectionComponent & dc)
inline nodiscard noexcept

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.

restitution
m2d

Reference to the Move2DComponent for velocity modification.

dc

Reference to the DirectionComponent for direction update.

Returns

Corrected world position after bounce.

Definition at line 209 of file LevelBoundsBehaviorSystem.ixx.

209 [[nodiscard]] BounceResult bounce(
210 helios::math::vec3f worldTranslation,
211 helios::math::aabbf objectBounds,
212 helios::math::aabbf levelBounds,
213 const float restitution,
216 ) noexcept {
217
218
219 auto velocity = m2d.velocity();
220 bool hitX = false;
221 bool hitY = false;
222 auto translation = worldTranslation;
223
224 // Check left boundary collision
225 if (objectBounds.min()[0] < levelBounds.min()[0]) {
226 translation[0] += (levelBounds.min()[0] - objectBounds.min()[0]);
227 if (velocity[0] < 0) {
228 velocity = (velocity.withX(restitution * -velocity[0]));
229 }
230 hitX = true;
231 // Check right boundary collision
232 } else if (objectBounds.max()[0] > levelBounds.max()[0]) {
233 translation[0] -= (objectBounds.max()[0] - levelBounds.max()[0]) ;
234 if (velocity[0] > 0) {
235 velocity = (velocity.withX(restitution * -velocity[0]));
236 }
237 hitX = true;
238 }
239
240 objectBounds = objectBounds + (translation - worldTranslation);
241
242 // Check bottom boundary collision
243 if (objectBounds.min()[1] < levelBounds.min()[1]) {
244 translation[1] += (levelBounds.min()[1] - objectBounds.min()[1]);
245 if (velocity[1] < 0) {
246 velocity = velocity.withY(restitution * -velocity[1]);
247 }
248 hitY = true;
249 // Check top boundary collision
250 } else if (objectBounds.max()[1] > levelBounds.max()[1]) {
251 translation[1] -= (objectBounds.max()[1] - levelBounds.max()[1]);
252 if (velocity[1] > 0) {
253 velocity = velocity.withY(restitution * -velocity[1]);
254 }
255 hitY = true;
256 }
257
258
259 return {
260 hitX, hitY, translation, velocity, velocity.normalize()
261 };
262 }

updateCollisionResponse()

void helios::engine::mechanics::bounds::systems::LevelBoundsBehaviorSystem::updateCollisionResponse (helios::engine::ecs::GameObject go, BounceResult bounceResult, helios::engine::modules::physics::collision::types::CollisionResponse collisionResponse)
inline

Updates the entity's state based on the configured collision response.

If the response is set to AlignHeadingToDirection, this method updates the SteeringComponent to match the new bounce direction.

Parameters
go

Pointer to the GameObject to update.

bounceResult

The result data from the bounce calculation.

collisionResponse

The type of response to apply.

Definition at line 173 of file LevelBoundsBehaviorSystem.ixx.

173 void updateCollisionResponse(
175 BounceResult bounceResult,
177
180
181 const auto direction = bounceResult.direction;
182
183 if (psc) {
184 psc->setSteeringIntent(
185 bounceResult.direction, 1.0f
186 );
187 }
188 }
189 }

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.