SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
PhysicsWorld.hpp
Go to the documentation of this file.
1#ifndef _PHYSICS_WORLD_HPP_
2#define _PHYSICS_WORLD_HPP_
3
7#include <vector>
8#include <cstdint>
9
10namespace Sleak {
11
13
14 namespace Physics {
15
16 /// Two overlapping colliders and the manifold describing how they overlap.
17 /// @ingroup physics
23
24 /// Result of a single Raycast call.
25 /// @ingroup physics
26 struct RayHit {
27 bool hit = false;
31 float distance = 0.0f;
32 };
33
34 /// Result of sweeping a moving shape (e.g. SphereSweep) against the world.
35 /// @ingroup physics
36 struct SweepResult {
37 bool hit = false;
41 float distance = 0.0f;
42 };
43
44 /// Owns the broadphase tree and every registered collider; drives collision detection and resolution each step.
45 ///
46 /// Each Scene creates one PhysicsWorld and steps it from its
47 /// update, so you normally reach it with
48 /// SceneBase::GetPhysicsWorld() rather than constructing one. You
49 /// also rarely call RegisterCollider() by hand: adding a
50 /// GameObject that carries a ColliderComponent registers it (and
51 /// its children) automatically.
52 ///
53 /// Step() integrates every dynamic RigidbodyComponent using that
54 /// body's own gravity setting, refreshes the DynamicAABBTree
55 /// broadphase, then finds and resolves overlapping pairs.
56 ///
57 /// The part you will use directly is the query API. Raycast(),
58 /// SphereSweep(), OverlapSphere(), and OverlapAABB() all take an
59 /// optional `layerMask` that is matched against
60 /// ColliderComponent::GetLayer(), so you can aim a query at exactly
61 /// the kind of object you care about.
62 ///
63 /// @code{.cpp}
64 /// auto* world = GetPhysicsWorld();
65 /// if (!world) return;
66 ///
67 /// // What is the player looking at, within 5 meters?
68 /// Sleak::Physics::RayHit hit = world->Raycast(
69 /// camera->GetPosition(), camera->GetDirection(), 5.0f);
70 /// if (hit.hit) {
71 /// SLEAK_INFO("Hit {} at {}m",
72 /// hit.collider->GetOwner()->GetName(), hit.distance);
73 /// }
74 ///
75 /// // Everything inside a blast radius
76 /// auto caught = world->OverlapSphere(
77 /// Sleak::Math::Vector3D(0.0f, 1.0f, 0.0f), 4.0f);
78 /// for (const auto& pair : caught) {
79 /// ApplyDamage(pair.b->GetOwner());
80 /// }
81 /// @endcode
82 ///
83 /// @see ColliderComponent, RigidbodyComponent, DynamicAABBTree,
84 /// RayHit, SweepResult, CollisionPair
85 /// @ingroup physics
87 public:
88 PhysicsWorld() = default;
89 ~PhysicsWorld() = default;
90
91 /// Advances the simulation by dt: integrates each dynamic rigidbody's
92 /// velocity, including its own gravity setting, into position, then
93 /// updates the broadphase and resolves collisions.
94 void Step(float dt);
95
96 void RegisterCollider(ColliderComponent* collider);
98
99 /// Query API: colliders overlapping a sphere, filtered by layerMask.
100 std::vector<CollisionPair> OverlapSphere(const Vector3D& center, float radius, uint32_t layerMask = 0xFFFFFFFF) const;
101 /// Colliders overlapping an AABB, filtered by layerMask.
102 std::vector<CollisionPair> OverlapAABB(const AABB& aabb, uint32_t layerMask = 0xFFFFFFFF) const;
103 /// Sweeps a sphere from start along direction and returns the first collider it hits within maxDist.
104 SweepResult SphereSweep(const Vector3D& start, const Vector3D& direction,
105 float radius, float maxDist, uint32_t layerMask = 0xFFFFFFFF) const;
106 /// Casts a ray and returns the closest collider hit within maxDist.
107 RayHit Raycast(const Vector3D& origin, const Vector3D& direction,
108 float maxDist, uint32_t layerMask = 0xFFFFFFFF) const;
109
110 private:
111 void UpdateBroadphase();
112 void FindPairsAndResolve();
113
114 DynamicAABBTree m_tree;
115 std::vector<ColliderComponent*> m_colliders;
116 };
117
118 } // namespace Physics
119} // namespace Sleak
120
121#endif // _PHYSICS_WORLD_HPP_
std::vector< CollisionPair > OverlapSphere(const Vector3D &center, float radius, uint32_t layerMask=0xFFFFFFFF) const
Query API: colliders overlapping a sphere, filtered by layerMask.
void RegisterCollider(ColliderComponent *collider)
void UnregisterCollider(ColliderComponent *collider)
SweepResult SphereSweep(const Vector3D &start, const Vector3D &direction, float radius, float maxDist, uint32_t layerMask=0xFFFFFFFF) const
Sweeps a sphere from start along direction and returns the first collider it hits within maxDist.
RayHit Raycast(const Vector3D &origin, const Vector3D &direction, float maxDist, uint32_t layerMask=0xFFFFFFFF) const
Casts a ray and returns the closest collider hit within maxDist.
std::vector< CollisionPair > OverlapAABB(const AABB &aabb, uint32_t layerMask=0xFFFFFFFF) const
Colliders overlapping an AABB, filtered by layerMask.
Collision shapes, the broadphase tree, and the world that steps them.
Definition SceneBase.hpp:29
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
ColliderComponent * collider
ColliderComponent * collider