group2 0.1.0
CSE 125 Group 2
Loading...
Searching...
No Matches
WorldData.hpp
Go to the documentation of this file.
1
9
10#pragma once
11
12#include "SweptCollision.hpp"
13
14#include <array>
15#include <cmath>
16#include <glm/geometric.hpp>
17
18namespace physics
19{
20
22
23namespace detail
24{
25
28{
30 bool set = false;
31};
32
35{
36 static ActiveWorldState s;
37 return s;
38}
39
40} // namespace detail
41
50inline void setActiveWorld(const WorldGeometry& geo)
51{
52 auto& s = detail::activeWorldState();
53 s.geo = geo;
54 s.set = true;
55}
56
61inline const WorldGeometry& activeWorld();
62// Defined after testWorld() below, since it depends on it.
63
64// Helper factories
65
70inline WorldBrush makeRamp(float xMin, float xMax, float zMin, float zMax, float height)
71{
72 WorldBrush b;
73
74 const float k_depth = zMax - zMin;
75 const float k_len = std::sqrt(k_depth * k_depth + height * height);
76 const glm::vec3 k_slopeNorm{0.0f, k_depth / k_len, -height / k_len};
77
78 // Slope surface distance: dot(normal, point_on_slope).
79 // The front-bottom corner (0, 0, zMin) sits on the slope.
80 const float k_slopeDist = glm::dot(k_slopeNorm, glm::vec3(0.0f, 0.0f, zMin));
81
82 b.planes[0] = {k_slopeNorm, k_slopeDist}; // slope (walkable surface)
83 b.planes[1] = {{0, -1, 0}, 0.0f}; // bottom
84 b.planes[2] = {{-1, 0, 0}, -xMin}; // left side
85 b.planes[3] = {{1, 0, 0}, xMax}; // right side
86 b.planes[4] = {{0, 0, -1}, -zMin}; // front face
87 b.planes[5] = {{0, 0, 1}, zMax}; // back face
88 b.planeCount = 6;
89
90 return b;
91}
92
100inline WorldBrush makeDiagonalWall(glm::vec3 center, float halfLen, float halfThick, float height, glm::vec3 dir)
101{
102 WorldBrush b;
103
104 // Face normal: 90-degree rotation of dir in the XZ plane.
105 const glm::vec3 k_faceN{dir.z, 0.0f, -dir.x};
106
107 b.planes[0] = {k_faceN, glm::dot(k_faceN, center + k_faceN * halfThick)}; // front face
108 b.planes[1] = {-k_faceN, glm::dot(-k_faceN, center - k_faceN * halfThick)}; // back face
109 b.planes[2] = {dir, glm::dot(dir, center + dir * halfLen)}; // right end
110 b.planes[3] = {-dir, glm::dot(-dir, center - dir * halfLen)}; // left end
111 b.planes[4] = {{0, 1, 0}, height}; // top
112 b.planes[5] = {{0, -1, 0}, 0.0f}; // bottom
113 b.planeCount = 6;
114
115 return b;
116}
117
118// Test world
119
131{
132 // Infinite planes
133 static const std::array<Plane, 1> k_planes = {{
134 {.normal = {0, 1, 0}, .distance = 0.0f}, // floor at y=0
135 }};
136
137 // Axis-aligned boxes
138 static const std::array<WorldAABB, 30> k_boxes = {{
139 // 0. Reference cube (existing visual — now also collidable)
140 {{-32, 0, 368}, {32, 64, 432}},
141
142 // 1. Small steppable box (16 units tall < stepHeight=18)
143 {{68, 0, 768}, {132, 16, 832}},
144
145 // 2. Large jumpable box (64 units tall — requires a jump)
146 {{-140, 0, 760}, {-60, 64, 840}},
147
148 // 3-7. Stairs — 5 steps, each 16u high × 48u deep, 128u wide
149 {{-64, 0, 1500}, {64, 16, 1548}},
150 {{-64, 0, 1548}, {64, 32, 1596}},
151 {{-64, 0, 1596}, {64, 48, 1644}},
152 {{-64, 0, 1644}, {64, 64, 1692}},
153 {{-64, 0, 1692}, {64, 80, 1740}},
154
155 // 8. Axis-aligned wall (256 wide × 120 tall × 16 thick)
156 {{-128, 0, 1892}, {128, 120, 1908}},
157
158 // 9. Pole (16×16 base × 200 tall)
159 {{-258, 0, 1892}, {-242, 200, 1908}},
160
161 // 10. Thin elevated walkway (32 wide × 16 tall × 400 long, at y=80)
162 {{-16, 80, 2100}, {16, 96, 2500}},
163
164 // Wallrun corridor (parallel walls, 400u long, 200u tall, 200u apart)
165 // 11. Left wallrun wall
166 {{-116, 0, 2700}, {-100, 200, 3100}},
167 // 12. Right wallrun wall
168 {{100, 0, 2700}, {116, 200, 3100}},
169
170 // Wall-to-wall jump section (offset walls for chaining wallruns)
171 // 13. Left wall (offset)
172 {{-116, 0, 3300}, {-100, 200, 3600}},
173 // 14. Right wall (offset further)
174 {{140, 0, 3400}, {156, 200, 3700}},
175
176 // Tall flat wall (300u tall)
177 // 15. Tall wall (front-facing from the corridor)
178 {{-64, 0, 3900}, {64, 300, 3916}},
179
180 // Medium wall (120u, flat top)
181 // 16. Medium flat-top wall
182 {{200, 0, 3900}, {328, 120, 3916}},
183
184 // Slide run (long flat stretch for sprint -> slide)
185 // The floor itself is the slide surface, but add side walls to guide
186 // 17. Left guide wall
187 {{-200, 0, 4100}, {-184, 40, 4600}},
188 // 18. Right guide wall
189 {{184, 0, 4100}, {200, 40, 4600}},
190
191 // Combined parkour course
192 // 19. Platform (jump up to start the course)
193 {{-48, 0, 4800}, {48, 48, 4848}},
194 // 20. Left wallrun wall (angled course)
195 {{-140, 0, 4900}, {-124, 200, 5300}},
196 // 21. Right wallrun wall (angled course)
197 {{124, 0, 5000}, {140, 200, 5400}},
198 // 22. Tall target wall (at end of wallrun)
199 {{-64, 0, 5500}, {64, 250, 5516}},
200 // 23. Raised platform (on top of a medium wall)
201 {{200, 0, 5500}, {328, 100, 5516}},
202 // 24. Landing pad (beyond the tall wall)
203 {{-80, 0, 5550}, {80, 16, 5650}},
204
205 // Grapple test: arch + high platform
206 // 25. Arch left pillar (32×500×32, very tall)
207 {{-116, 0, 6000}, {-84, 500, 6032}},
208 // 26. Arch right pillar
209 {{84, 0, 6000}, {116, 500, 6032}},
210 // 27. Arch crossbar (connects pillars at y=460, 232 wide × 40 tall × 32 deep)
211 {{-116, 460, 6000}, {116, 500, 6032}},
212 // 28. High platform (behind the arch, at y=400, 200×16×100)
213 {{-100, 400, 6200}, {100, 416, 6300}},
214 // 29. Ultra-high target platform (y=600, small, way up there)
215 {{-48, 580, 6500}, {48, 596, 6548}},
216 }};
217
218 // Convex brushes
219 static const std::array<WorldBrush, 3> k_brushes = {{
220 // Gentle ramp (15 deg): x ∈ [-214, -86], z ∈ [950, 1250], rises to 80u
221 makeRamp(-214.0f, -86.0f, 950.0f, 1250.0f, 80.0f),
222
223 // Steep ramp (40 deg): x ∈ [86, 214], z ∈ [1000, 1200], rises to 168u
224 makeRamp(86.0f, 214.0f, 1000.0f, 1200.0f, 168.0f),
225
226 // Diagonal wall (45 deg): centre (300, 0, 1900), length 200, thick 16, height 120
227 makeDiagonalWall({300.0f, 0.0f, 1900.0f}, 100.0f, 8.0f, 120.0f, glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f))),
228 }};
229
230 static const WorldGeometry k_geo{k_planes, k_boxes, k_brushes, {}, {}, {}};
231 return k_geo;
232}
233
234// activeWorld() definition (depends on testWorld)
235
237{
238 const auto& s = detail::activeWorldState();
239 if (s.set)
240 return s.geo;
241 return testWorld();
242}
243
244} // namespace physics
Swept AABB and sphere collision queries against world geometry.
Active world (runtime-switchable).
Definition WorldData.hpp:24
ActiveWorldState & activeWorldState()
Access the shared active-world state (Meyer's singleton).
Definition WorldData.hpp:34
Pure physics math — no ECS types, no registry.
Definition BroadphaseTree.cpp:11
WorldBrush makeRamp(float xMin, float xMax, float zMin, float zMax, float height)
Create a ramp brush that rises along +Z.
Definition WorldData.hpp:70
WorldBrush makeDiagonalWall(glm::vec3 center, float halfLen, float halfThick, float height, glm::vec3 dir)
Create a diagonal wall brush from a centre, direction, and dimensions.
Definition WorldData.hpp:100
const WorldGeometry & activeWorld()
Return the world geometry most recently set via setActiveWorld(), or fall back to testWorld() if none...
Definition WorldData.hpp:236
const WorldGeometry & testWorld()
The physics test playground.
Definition WorldData.hpp:130
void setActiveWorld(const WorldGeometry &geo)
Set the world geometry that activeWorld() returns.
Definition WorldData.hpp:50
A convex volume defined by bounding planes (for ramps, angled walls, etc.).
Definition SweptCollision.hpp:58
Plane planes[k_maxPlanes]
Definition SweptCollision.hpp:60
int planeCount
Definition SweptCollision.hpp:61
All world collision geometry for one tick.
Definition SweptCollision.hpp:225
Shared singleton state for the runtime-switchable world.
Definition WorldData.hpp:28
bool set
Definition WorldData.hpp:30
WorldGeometry geo
Definition WorldData.hpp:29