12#include <glm/geometric.hpp>
13#include <glm/vec3.hpp>
163 const std::function<
bool(uint32_t meshIndex)>& visit);
167 return a.max.x >= b.min.x && a.min.x <= b.max.x && a.max.y >= b.min.y && a.min.y <= b.max.y && a.max.z >= b.min.z &&
171template <
class Visit>
179 constexpr int kStackCapacity = 128;
180 int stack[kStackCapacity];
181 std::vector<int> overflowStack;
185 auto pushNode = [&](
int nodeIndex) {
186 if (stackPtr + 1 < kStackCapacity) {
187 stack[++stackPtr] = nodeIndex;
189 overflowStack.push_back(nodeIndex);
193 while (stackPtr >= 0 || !overflowStack.empty()) {
196 nodeIdx = stack[stackPtr--];
198 nodeIdx = overflowStack.back();
199 overflowStack.pop_back();
206 if (node.
count > 0) {
208 const uint32_t meshIndex =
broadphase.meshIndices[
static_cast<std::size_t
>(i)];
209 if (meshIndex >=
broadphase.meshBounds.size() ||
213 if (!visit(meshIndex))
250 glm::vec3
up{0.0f, 1.0f, 0.0f};
253 [[nodiscard]] glm::vec3
segA(glm::vec3 center)
const noexcept {
return center +
up *
halfHeight; }
256 [[nodiscard]] glm::vec3
segB(glm::vec3 center)
const noexcept {
return center -
up *
halfHeight; }
287 const float maxAllowed = std::max(
halfHeight - 0.5f, 0.0f);
288 const float clamped = std::min(std::max(requestedStepHeight, 0.0f), maxAllowed);
335HitResult
sweepAABB(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, std::span<const Plane> planes);
342HitResult
sweepAABBvsBox(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end,
const WorldAABB& box);
348HitResult
sweepAABBvsBrush(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end,
const WorldBrush& brush);
355HitResult
sweepAABBvsCylinder(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end,
const WorldCylinder& cyl);
362HitResult
sweepAABBvsSphere(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end,
const WorldSphere& sph);
365HitResult
sweepAll(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end,
const WorldGeometry& world);
373HitResult
sweepCapsuleVsPlanes(CapsuleShape capsule, glm::vec3 start, glm::vec3 end, std::span<const Plane> planes);
376HitResult
sweepCapsuleVsBox(CapsuleShape capsule, glm::vec3 start, glm::vec3 end,
const WorldAABB& box);
379HitResult
sweepCapsuleVsBrush(CapsuleShape capsule, glm::vec3 start, glm::vec3 end,
const WorldBrush& brush);
382HitResult
sweepCapsuleVsCylinder(CapsuleShape capsule, glm::vec3 start, glm::vec3 end,
const WorldCylinder& cyl);
385HitResult
sweepCapsuleVsSphere(CapsuleShape capsule, glm::vec3 start, glm::vec3 end,
const WorldSphere& sph);
388HitResult
sweepAll(CapsuleShape capsule, glm::vec3 start, glm::vec3 end,
const WorldGeometry& world);
442 const WorldGeometry& world,
443 float maxMeshSearchRadius = 1024.0f);
480GroundProbeResult
probeGround(CapsuleShape capsule, glm::vec3 pos,
float maxDistance,
const WorldGeometry& world);
517DepenContact
deepestCapsuleContact(CapsuleShape capsule, glm::vec3 pos, glm::vec3 vel,
const WorldGeometry& world);
540 CapsuleShape capsule,
541 const WorldGeometry& world,
542 DepenetrationOptions options = {});
578SphereHitResult
sphereCast(
float radius, glm::vec3 start, glm::vec3 end,
const WorldGeometry& world);
Optional per-frame physics/KCC counters for client performance captures.
Surface-material classification shared across collision, raycasts, VFX, SFX, and damage falloff.
SurfaceType
Surface material hit by a projectile / hitscan / contact.
Definition SurfaceType.hpp:28
@ Concrete
Definition SurfaceType.hpp:30
Definition BroadphaseTree.cpp:11
void add(std::uint32_t FrameStats::*field, std::uint32_t amount=1) noexcept
Definition PhysicsPerfStats.hpp:107
Pure physics math — no ECS types, no registry.
Definition BroadphaseTree.cpp:11
bool emergencyUnstick(glm::vec3 &pos, glm::vec3 &vel, CapsuleShape capsule, const WorldGeometry &world)
Last-resort recovery for a capsule embedded in geometry with no clear depen direction.
Definition SweptCollision.cpp:1432
ClearanceResult clearanceCapsuleVsWorld(CapsuleShape capsule, glm::vec3 pos, const WorldGeometry &world, float maxMeshSearchRadius)
Scene-wide minimum clearance.
Definition SweptCollision.cpp:1096
HitResult sweepAll(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, const WorldGeometry &world)
Sweep an AABB against all world geometry, returning the earliest hit.
Definition SweptCollision.cpp:856
bool broadphaseAabbOverlap(const WorldAABB &a, const WorldAABB &b) noexcept
Definition SweptCollision.hpp:165
HitResult sweepCapsuleVsCylinder(CapsuleShape capsule, glm::vec3 start, glm::vec3 end, const WorldCylinder &cyl)
Sweep a capsule against a vertical cylinder. Conservative.
Definition SweptCollision.cpp:714
HitResult sweepAABBvsBox(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, const WorldAABB &box)
Sweep an AABB against a static axis-aligned box.
Definition SweptCollision.cpp:270
ClearanceResult clearanceCapsuleVsSphere(CapsuleShape capsule, glm::vec3 pos, const WorldSphere &sph)
Definition SweptCollision.cpp:1071
SphereHitResult sphereCast(float radius, glm::vec3 start, glm::vec3 end, const WorldGeometry &world)
Cast a sphere along the path [start, end] against all world geometry.
Definition SweptCollision.cpp:1477
void buildStaticWorldBroadphase(StaticWorldBroadphase &broadphase, std::span< const WorldTriMesh > triMeshes)
Rebuild the immutable broadphase over all static triangle meshes.
Definition SweptCollision.cpp:189
HitResult sweepCapsuleVsPlanes(CapsuleShape capsule, glm::vec3 start, glm::vec3 end, std::span< const Plane > planes)
Sweep a capsule along [start, end] against a list of infinite planes.
Definition SweptCollision.cpp:577
HitResult sweepAABBvsSphere(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, const WorldSphere &sph)
Sweep an AABB against a sphere.
Definition SweptCollision.cpp:519
HitResult sweepAABBvsCylinder(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, const WorldCylinder &cyl)
Sweep an AABB against a vertical cylinder.
Definition SweptCollision.cpp:411
HitResult sweepCapsuleVsBox(CapsuleShape capsule, glm::vec3 start, glm::vec3 end, const WorldAABB &box)
Sweep a capsule against a static axis-aligned box. Conservative.
Definition SweptCollision.cpp:603
HitResult sweepCapsuleVsBrush(CapsuleShape capsule, glm::vec3 start, glm::vec3 end, const WorldBrush &brush)
Sweep a capsule against a convex brush. Exact (per-plane Minkowski extent).
Definition SweptCollision.cpp:665
HitResult sweepAABB(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, std::span< const Plane > planes)
Sweep an AABB along the path [start, end] against a list of infinite planes.
Definition SweptCollision.cpp:224
DepenetrationResult depenetrateCapsuleVsWorldDetailed(glm::vec3 &pos, glm::vec3 &vel, CapsuleShape capsule, const WorldGeometry &world, DepenetrationOptions options)
Definition SweptCollision.cpp:1357
ClearanceResult clearanceCapsuleVsCylinder(CapsuleShape capsule, glm::vec3 pos, const WorldCylinder &cyl)
Definition SweptCollision.cpp:1058
ClearanceResult clearanceCapsuleVsPlanes(CapsuleShape capsule, glm::vec3 pos, std::span< const Plane > planes)
Definition SweptCollision.cpp:936
HitResult sweepAABBvsBrush(glm::vec3 halfExtents, glm::vec3 start, glm::vec3 end, const WorldBrush &brush)
Sweep an AABB against a convex brush (set of bounding planes).
Definition SweptCollision.cpp:346
DepenContact deepestCapsuleContact(CapsuleShape capsule, glm::vec3 pos, glm::vec3 vel, const WorldGeometry &world)
Scene-wide deepest single contact (per-primitive, per-feature).
Definition SweptCollision.cpp:1332
void depenetrateCapsuleVsWorld(glm::vec3 &pos, glm::vec3 &vel, CapsuleShape capsule, const WorldGeometry &world)
Per-pass-deepest-first capsule depenetration against the whole world.
Definition SweptCollision.cpp:1427
ClearanceResult clearanceCapsuleVsBrush(CapsuleShape capsule, glm::vec3 pos, const WorldBrush &brush)
Definition SweptCollision.cpp:1015
void queryStaticWorldBroadphase(const StaticWorldBroadphase &broadphase, const WorldAABB &query, const std::function< bool(uint32_t meshIndex)> &visit)
Visit triangle-mesh indices whose mesh bounds overlap query.
Definition SweptCollision.cpp:217
HitResult sweepCapsuleVsSphere(CapsuleShape capsule, glm::vec3 start, glm::vec3 end, const WorldSphere &sph)
Sweep a capsule against a sphere. Conservative.
Definition SweptCollision.cpp:810
ClearanceResult clearanceCapsuleVsBox(CapsuleShape capsule, glm::vec3 pos, const WorldAABB &box)
Definition SweptCollision.cpp:954
GroundProbeResult probeGround(CapsuleShape capsule, glm::vec3 pos, float maxDistance, const WorldGeometry &world)
Downward sweep that classifies the ground under a capsule.
Definition SweptCollision.cpp:1133
void queryStaticWorldBroadphaseFast(const StaticWorldBroadphase &broadphase, const WorldAABB &query, Visit &&visit)
Definition SweptCollision.hpp:173
A single BVH node for spatial acceleration of triangle meshes.
Definition SweptCollision.hpp:87
int leftFirst
If leaf: index into triIndices[]. If interior: left child index.
Definition SweptCollision.hpp:90
glm::vec3 boundsMax
AABB maximum corner.
Definition SweptCollision.hpp:89
int count
>0 → leaf with count triangles. 0 → interior node.
Definition SweptCollision.hpp:91
glm::vec3 boundsMin
AABB minimum corner.
Definition SweptCollision.hpp:88
Capsule shape input for swept-collision queries.
Definition SweptCollision.hpp:247
glm::vec3 enclosingHalfExtents() const noexcept
Tight half-extents of the AABB that encloses the capsule at center.
Definition SweptCollision.hpp:262
float halfHeight
Half the segment length (excludes spherical caps).
Definition SweptCollision.hpp:249
glm::vec3 up
Unit-length axis direction.
Definition SweptCollision.hpp:250
CapsuleShape walkShape(float requestedStepHeight) const noexcept
"Walk" capsule — a shorter capsule whose foot is lifted by stepHeight (using effectiveStepHeight) and...
Definition SweptCollision.hpp:299
float radius
Cylinder cross-section radius.
Definition SweptCollision.hpp:248
float effectiveStepHeight(float requestedStepHeight) const noexcept
Clamp a requested step height to a value the capsule can safely support — never larger than the segme...
Definition SweptCollision.hpp:285
glm::vec3 segA(glm::vec3 center) const noexcept
Top endpoint of the inner segment at center.
Definition SweptCollision.hpp:253
glm::vec3 segB(glm::vec3 center) const noexcept
Bottom endpoint of the inner segment at center.
Definition SweptCollision.hpp:256
float minkowskiExtent(glm::vec3 n) const noexcept
Minkowski half-extent of the capsule along a unit direction n.
Definition SweptCollision.hpp:275
glm::vec3 walkCenterOffset(float requestedStepHeight) const noexcept
Centre-position offset that takes a full-capsule centre to the matching walk-capsule centre (head ali...
Definition SweptCollision.hpp:308
Result of a shape-vs-geometry closest-point clearance query.
Definition SweptCollision.hpp:407
float distance
Signed distance shape-surface → geometry-surface.
Definition SweptCollision.hpp:409
glm::vec3 normal
Unit-length, points from geometry into free space.
Definition SweptCollision.hpp:411
bool contact
True when distance <= contactEpsilon.
Definition SweptCollision.hpp:408
glm::vec3 pointOnGeometry
World-space closest point on the geometry side.
Definition SweptCollision.hpp:410
SurfaceType surfaceType
Material tag at the closest feature.
Definition SweptCollision.hpp:412
Definition SweptCollision.hpp:500
bool allowEmergencyUnstick
Definition SweptCollision.hpp:502
float maxPushDistance
Definition SweptCollision.hpp:501
Definition SweptCollision.hpp:506
bool unresolvedOverlap
Definition SweptCollision.hpp:511
int passes
Definition SweptCollision.hpp:508
bool emergencyUnstuck
Definition SweptCollision.hpp:509
float pushDistance
Definition SweptCollision.hpp:507
bool exceededMaxPush
Definition SweptCollision.hpp:510
Result of a downward ground probe — what's directly under a character's feet, classified for walkabil...
Definition SweptCollision.hpp:455
glm::vec3 point
World-space contact point on the surface.
Definition SweptCollision.hpp:459
float distance
Foot-to-surface signed distance along the probe axis.
Definition SweptCollision.hpp:458
bool walkable
True if the surface normal makes it standable per k_floorAngleCos.
Definition SweptCollision.hpp:457
SurfaceType surfaceType
Material at the hit surface.
Definition SweptCollision.hpp:461
glm::vec3 normal
Outward-pointing surface normal (toward free space).
Definition SweptCollision.hpp:460
bool hit
True if any surface was found within the probe range.
Definition SweptCollision.hpp:456
Result of a swept AABB collision query.
Definition SweptCollision.hpp:316
SurfaceType surfaceType
Material at the hit surface (Phase 3).
Definition SweptCollision.hpp:320
glm::vec3 normal
Surface normal at the contact point.
Definition SweptCollision.hpp:319
float tFirst
Fraction along the movement path [0..1] where the first hit occurs.
Definition SweptCollision.hpp:318
bool hit
True if the sweep intersected a plane.
Definition SweptCollision.hpp:317
An infinite plane dividing free space from solid geometry.
Definition SweptCollision.hpp:31
SurfaceType surfaceType
Material tag for impact VFX / SFX (Phase 3).
Definition SweptCollision.hpp:34
glm::vec3 normal
Unit vector pointing into free (non-solid) space.
Definition SweptCollision.hpp:32
float distance
Signed offset: dot(normal, p) == distance for points on the plane.
Definition SweptCollision.hpp:33
Result of a sphere-cast query (includes world-space hit point).
Definition SweptCollision.hpp:560
glm::vec3 point
World-space contact point on the surface.
Definition SweptCollision.hpp:564
bool hit
Definition SweptCollision.hpp:561
float t
Fraction along path [0..1].
Definition SweptCollision.hpp:562
glm::vec3 normal
Surface normal at contact.
Definition SweptCollision.hpp:563
SurfaceType surfaceType
Material at the hit surface (Phase 3).
Definition SweptCollision.hpp:565
Immutable world-level BVH over WorldTriMesh bounds.
Definition SweptCollision.hpp:149
std::vector< uint32_t > meshIndices
Definition SweptCollision.hpp:151
std::vector< WorldAABB > meshBounds
Definition SweptCollision.hpp:152
std::vector< BVHNode > nodes
Definition SweptCollision.hpp:150
An axis-aligned box in world space, used as static collision geometry.
Definition SweptCollision.hpp:39
SurfaceType surfaceType
Material tag (Phase 3).
Definition SweptCollision.hpp:42
glm::vec3 max
Maximum corner (highest x, y, z).
Definition SweptCollision.hpp:41
glm::vec3 min
Minimum corner (lowest x, y, z).
Definition SweptCollision.hpp:40
A convex volume defined by bounding planes (for ramps, angled walls, etc.).
Definition SweptCollision.hpp:58
Plane planes[k_maxPlanes]
Definition SweptCollision.hpp:60
static constexpr int k_maxPlanes
Definition SweptCollision.hpp:59
SurfaceType surfaceType
Material tag (Phase 3).
Definition SweptCollision.hpp:62
int planeCount
Definition SweptCollision.hpp:61
A vertical (Y-axis) cylinder in world space.
Definition SweptCollision.hpp:70
float radius
Horizontal radius.
Definition SweptCollision.hpp:72
float height
Extent along +Y from base.
Definition SweptCollision.hpp:73
glm::vec3 base
Centre of the bottom cap.
Definition SweptCollision.hpp:71
SurfaceType surfaceType
Material tag (Phase 3).
Definition SweptCollision.hpp:74
All world collision geometry for one tick.
Definition SweptCollision.hpp:225
std::span< const WorldCylinder > cylinders
Definition SweptCollision.hpp:229
std::span< const WorldTriMesh > triMeshes
Definition SweptCollision.hpp:231
const StaticWorldBroadphase * staticBroadphase
Definition SweptCollision.hpp:232
std::span< const Plane > planes
Definition SweptCollision.hpp:226
std::span< const WorldAABB > boxes
Definition SweptCollision.hpp:227
std::span< const WorldBrush > brushes
Definition SweptCollision.hpp:228
std::span< const WorldSphere > spheres
Definition SweptCollision.hpp:230
A sphere in world space.
Definition SweptCollision.hpp:79
SurfaceType surfaceType
Material tag (Phase 3).
Definition SweptCollision.hpp:82
glm::vec3 center
Centre point.
Definition SweptCollision.hpp:80
float radius
Radius.
Definition SweptCollision.hpp:81
A triangle mesh with BVH acceleration for collision queries.
Definition SweptCollision.hpp:108
std::vector< uint8_t > vertActive
Bit i set ⇔ vertex i of this triangle is touched by an active edge.
Definition SweptCollision.hpp:119
std::vector< uint32_t > indices
Triangle indices (3 per triangle).
Definition SweptCollision.hpp:110
std::vector< uint8_t > triangleMaterials
One SurfaceType index per triangle (empty = use default).
Definition SweptCollision.hpp:139
std::vector< BVHNode > bvhNodes
Flat BVH node array.
Definition SweptCollision.hpp:111
std::vector< glm::vec3 > faceNormals
CCW face normal (unit length) per triangle.
Definition SweptCollision.hpp:117
std::vector< uint8_t > edgeActive
Bit i set ⇔ edge i of this triangle is an active (boundary or convex) edge.
Definition SweptCollision.hpp:118
SurfaceType defaultSurface
Fallback for triangles without per-face material data.
Definition SweptCollision.hpp:140
std::vector< glm::vec3 > vertices
All vertex positions (world space, scaled).
Definition SweptCollision.hpp:109
std::vector< uint32_t > edgeNeighbor
Phase B — edge → neighbour triangle adjacency.
Definition SweptCollision.hpp:134
glm::vec3 boundsMin
Whole-mesh AABB min.
Definition SweptCollision.hpp:113
glm::vec3 boundsMax
Whole-mesh AABB max.
Definition SweptCollision.hpp:114
std::vector< uint32_t > triIndices
Permutation: BVH leaf ranges → triangle indices.
Definition SweptCollision.hpp:112
std::uint32_t staticBroadphaseQueries
Definition PhysicsPerfStats.hpp:34
std::uint32_t staticBroadphaseMeshes
Definition PhysicsPerfStats.hpp:35