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