Skip to main content

CollisionStateComponent.ixx File

Per-frame collision state component written by detection systems. 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...

namespacecollision
namespacecomponents

Collider components for collision detection. More...

Classes Index

classCollisionStateComponent

Stores per-frame collision state for a GameObject. More...

Description

Per-frame collision state component written by detection systems.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file CollisionStateComponent.ixx
3 * @brief Per-frame collision state component written by detection systems.
4 */
5module;
6
7#include <format>
8#include <iostream>
9#include <optional>
10
11export module helios.engine.modules.physics.collision.components.CollisionStateComponent;
12
13import helios.engine.modules.physics.collision.types.CollisionBehavior;
14import helios.engine.modules.physics.collision.types.CollisionContext;
15
16import helios.engine.ecs.EntityHandle;
17
18import helios.engine.ecs.GameObject;
19import helios.util.Guid;
20import helios.math;
21
23
24 /**
25 * @class CollisionStateComponent
26 * @brief Stores per-frame collision state for a GameObject.
27 *
28 * @details This component is written by the collision detection system each frame
29 * to record whether a collision occurred and provide details about the collision.
30 * Game logic can query this component to react to collisions.
31 *
32 * The state should be reset at the start of each frame by the collision system or
33 * when the owning GameObject is acquired from a pool.
34 */
36 /**
37 * @brief World-space contact point of the collision.
38 */
39 helios::math::vec3f contact_;
40
41 /**
42 * @brief Whether this collision is a solid (physical) collision.
43 */
44 bool isSolid_ = false;
45
46 /**
47 * @brief Whether this collision is a trigger (non-physical) collision.
48 */
49 bool isTrigger_ = false;
50
51 /**
52 * @brief Whether this entity is the collision reporter.
53 */
54 bool isCollisionReporter_ = false;
55
56 /**
57 * @brief Handle of the other entity involved in the collision.
58 */
59 std::optional<helios::engine::ecs::EntityHandle> other_;
60
61 /**
62 * @brief The collision behavior to apply.
63 */
65
66 /**
67 * @brief Flag indicating whether a collision was detected this frame.
68 */
69 bool hasCollision_ = false;
70
71 /**
72 * @brief Full collision context data for event publishing.
73 */
75
76 /**
77 * @brief Collision layer ID of this entity.
78 */
79 uint32_t collisionLayer_ = 0;
80
81 /**
82 * @brief Collision layer ID of the other entity.
83 */
84 uint32_t otherCollisionLayer_ = 0;
85
86 /**
87 * @brief Whether this component is enabled.
88 */
89 bool isEnabled_ = true;
90
91 public:
92
93 /**
94 * @brief Checks whether this component is enabled.
95 *
96 * @return True if enabled, false otherwise.
97 */
98 [[nodiscard]] bool isEnabled() const noexcept {
99 return isEnabled_;
100 }
101
102 /**
103 * @brief Enables this component.
104 */
105 void enable() noexcept {
106 isEnabled_ = true;
107 }
108
109 /**
110 * @brief Disables this component.
111 */
112 void disable() noexcept {
113 isEnabled_ = false;
114 }
115
116 /**
117 * @brief Default constructor.
118 */
120
121 /**
122 * @brief Copy constructor (creates empty state).
123 *
124 * @param other The component to copy from (state is not copied).
125 */
127
130 CollisionStateComponent& operator=(CollisionStateComponent&&) noexcept = default;
131
132 /**
133 * @brief Sets the collision state for this frame.
134 *
135 * @details This method is called by the collision detection system when a
136 * collision is detected. Only the first collision per frame is recorded.
137 *
138 * @param contact World-space contact point.
139 * @param isSolid Whether this is a solid collision.
140 * @param isTrigger Whether this is a trigger collision.
141 * @param collisionBehavior The behavior to apply for this collision.
142 * @param isCollisionReporter Whether this entity reports the collision.
143 * @param other GUID of the other entity (optional).
144 * @param collisionLayer Collision layer ID of this entity.
145 * @param otherCollisionLayer Collision layer ID of the other entity.
146 *
147 * @return True if the state was set, false if a collision was already recorded.
148 */
150 helios::engine::ecs::GameObject gameObject,
151 helios::math::vec3f contact,
152 const bool isSolid,
153 const bool isTrigger,
154 const helios::engine::modules::physics::collision::types::CollisionBehavior collisionBehavior,
155 const bool isCollisionReporter,
156 std::optional<helios::engine::ecs::EntityHandle> other = std::nullopt,
157 const uint32_t collisionLayer = 0,
158 const uint32_t otherCollisionLayer = 0) {
159
160 if (hasCollision_) {
161 return false;
162 }
163
164 hasCollision_ = true;
165 isSolid_ = isSolid;
166 isTrigger_ = isTrigger;
167
168 collisionBehavior_ = collisionBehavior;
169
170 isCollisionReporter_ = isCollisionReporter;
171
172 other_ = other;
173 contact_ = contact;
174
175 collisionLayer_ = collisionLayer;
176 otherCollisionLayer_ = otherCollisionLayer;
177
178 collisionContext_ = types::CollisionContext{
179 .source = gameObject.entityHandle(),
180 .contact = contact,
181 .isSolid = isSolid,
182 .isTrigger = isTrigger,
183 .isCollisionReporter = isCollisionReporter,
184 .other = other,
185 .collisionLayerId = collisionLayer,
186 .otherCollisionLayerId = otherCollisionLayer,
187 };
188
189 return true;
190 }
191
192 /**
193 * @brief Resets the collision state for a new frame.
194 *
195 * @details Clears all collision flags and data. Called at the start of
196 * each frame or when the owning GameObject is acquired from a pool.
197 */
198 void reset() noexcept {
199 hasCollision_ = false;
200 isSolid_ = false;
201 isTrigger_ = false;
202
203 collisionBehavior_ = types::CollisionBehavior::None;
204
205 isCollisionReporter_ = false;
206
207 other_ = std::nullopt;
208 contact_ = helios::math::vec3f{0.0f, 0.0f, 0.0f};
209
210 collisionContext_ = types::CollisionContext{};
211
212 collisionLayer_ = 0;
213 otherCollisionLayer_ = 0;
214 }
215
216 /**
217 * @brief Called when the owning GameObject is acquired from a pool.
218 *
219 * @details Resets all collision state to prepare for a new lifecycle.
220 */
221 void onAcquire() noexcept {
222 reset();
223 }
224
225 /**
226 * @brief Returns the full collision context data.
227 *
228 * @return Reference to the CollisionContext struct containing all collision details.
229 */
230 [[nodiscard]] const types::CollisionContext& collisionContext() const noexcept {
231 return collisionContext_;
232 }
233
234
235 /**
236 * @brief Checks whether a collision was detected this frame.
237 *
238 * @return True if a collision occurred, false otherwise.
239 */
240 [[nodiscard]] bool hasCollision() const noexcept {
241 return hasCollision_;
242 }
243
244 /**
245 * @brief Returns the handle of the other entity involved in the collision.
246 *
247 * @return Optional handle of the other entity, or nullopt if not set.
248 */
249 [[nodiscard]] std::optional<helios::engine::ecs::EntityHandle> other() const noexcept {
250 return other_;
251 }
252
253 /**
254 * @brief Checks whether this entity is the collision reporter.
255 *
256 * @return True if this entity reported the collision.
257 */
258 [[nodiscard]] bool isCollisionReporter() const noexcept {
259 return isCollisionReporter_;
260 }
261
262 /**
263 * @brief Checks whether this is a solid (physical) collision.
264 *
265 * @return True if the collision is solid.
266 */
267 [[nodiscard]] bool isSolid() const noexcept {
268 return isSolid_;
269 }
270
271 /**
272 * @brief Checks whether this is a trigger (non-physical) collision.
273 *
274 * @return True if the collision is a trigger.
275 */
276 [[nodiscard]] bool isTrigger() const noexcept {
277 return isTrigger_;
278 }
279
280 /**
281 * @brief Returns the collision behavior for this collision.
282 *
283 * @return The CollisionBehavior enum value.
284 */
286 return collisionBehavior_;
287 }
288
289 /**
290 * @brief Returns the world-space contact point of the collision.
291 *
292 * @return The contact point as a 3D vector.
293 */
294 [[nodiscard]] helios::math::vec3f contact() const noexcept {
295 return contact_;
296 }
297
298 /**
299 * @brief Returns the collision layer ID of this entity.
300 *
301 * @return The collision layer ID.
302 */
303 [[nodiscard]] uint32_t collisionLayer() const noexcept {
304 return collisionLayer_;
305 }
306
307 /**
308 * @brief Returns the collision layer ID of the other entity.
309 *
310 * @return The other entity's collision layer ID.
311 */
312 [[nodiscard]] uint32_t otherCollisionLayer() const noexcept {
313 return otherCollisionLayer_;
314 }
315
316 };
317
318
319}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.