Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PhysicsQueries.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
5
6#include <cfloat>
7#include <cstring>
8
9static float raycastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void* context) {
10 if (g_raycastBuffer.size() / RAYCAST_STRIDE >= MAX_RAYCAST_HITS) return 0.0f;
11
12 uint32_t entityId = entityFromShape(shapeId);
13 if (entityId == 0xFFFFFFFF) return 1.0f;
14
16 g_raycastBuffer.push_back(point.x);
17 g_raycastBuffer.push_back(point.y);
18 g_raycastBuffer.push_back(normal.x);
19 g_raycastBuffer.push_back(normal.y);
20 g_raycastBuffer.push_back(fraction);
21
22 return 1.0f;
23}
24
25static bool overlapCallback(b2ShapeId shapeId, void* context) {
26 if (g_overlapBuffer.size() >= MAX_OVERLAP_HITS) return false;
27
28 uint32_t entityId = entityFromShape(shapeId);
29 if (entityId == 0xFFFFFFFF) return true;
30
32 return true;
33}
34
35static float shapeCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void* context) {
36 if (g_shapeCastBuffer.size() / SHAPECAST_STRIDE >= MAX_RAYCAST_HITS) return 0.0f;
37
38 uint32_t entityId = entityFromShape(shapeId);
39 if (entityId == 0xFFFFFFFF) return 1.0f;
40
42 g_shapeCastBuffer.push_back(point.x);
43 g_shapeCastBuffer.push_back(point.y);
44 g_shapeCastBuffer.push_back(normal.x);
45 g_shapeCastBuffer.push_back(normal.y);
46 g_shapeCastBuffer.push_back(fraction);
47
48 return 1.0f;
49}
50
51// —— Kinematic character mover (Box2D v3 native: CollideMover + SolvePlanes) ——
52// The generic shape cast reports a resting/touching contact as fraction 0 with a
53// zero normal, which stalls a hand-rolled move-and-slide. The mover collides the
54// capsule against the world into *planes* (each with a valid normal) and solves a
55// depenetrating slide — the intended path for characters standing on ground.
56static constexpr int MAX_MOVER_PLANES = 32;
57static constexpr int MAX_MOVER_HITS = 4;
58// How far a one-way surface's normal must lean to the solid side for a contact to count. Above zero, so the side of a platform walked
59// past is not a wall.
60static constexpr float MOVER_ONE_WAY_ALIGN = 0.5f;
61// Metres a mover may be sunk into a one-way surface and still stand on it: what the skin and the solver leave behind.
62static constexpr float MOVER_ONE_WAY_SLOP = 0.02f;
63
64static constexpr uint32_t MOVER_DROP_THROUGH = 1u;
65static constexpr uint32_t MOVER_IGNORE_DYNAMIC = 2u;
66static constexpr uint32_t MOVER_BLOCK_STEEP = 4u;
67static constexpr uint32_t MOVER_WAS_ON_FLOOR = 8u;
68
69static b2CollisionPlane g_moverPlanes[MAX_MOVER_PLANES];
72static int g_moverPlaneCount = 0;
73static uint32_t g_moverSelfEntity = 0xFFFFFFFF;
74static uint32_t g_moverFlags = 0;
75// The move's own velocity and how far it could fall, against which a one-way surface is judged.
76static b2Vec2 g_moverVelocity = {0.0f, 0.0f};
77static float g_moverDt = 0.0f;
78// The record physics_moveCharacter writes: see physics_moveCharacterBytes.
79static float g_moverBuffer[11 + MAX_MOVER_HITS * 3] = {};
82static int g_moverHitCount = 0;
83
85static bool oneWayAdmits(b2ShapeId shapeId, uint32_t entity, b2Vec2 normal, float depth) {
86 const auto it = g_ctx.oneWayNormals.find(entity);
87 if (it == g_ctx.oneWayNormals.end()) return true;
88 if (g_moverFlags & MOVER_DROP_THROUGH) return false;
89 const b2Vec2 solid = it->second.localSpace
90 ? b2RotateVector(b2Body_GetRotation(b2Shape_GetBody(shapeId)), it->second.normal)
91 : it->second.normal;
92 if (b2Dot(normal, solid) <= MOVER_ONE_WAY_ALIGN) return false;
93 const float along = b2Dot(g_moverVelocity, solid);
94 // A fall lands inside a thin surface before the mover looks at it, so a step's worth of fall still counts as landing on it.
95 return depth <= MOVER_ONE_WAY_SLOP - along * g_moverDt;
96}
97
102static bool normalIsSurface(b2ShapeId shapeId, const b2PlaneResult* pr) {
103 b2RayCastInput ray;
104 ray.origin = b2MulAdd(pr->point, 0.05f, pr->plane.normal);
105 ray.translation = b2MulSV(-0.1f, pr->plane.normal);
106 ray.maxFraction = 1.0f;
107 const b2CastOutput hit = b2Shape_RayCast(shapeId, &ray);
108 return hit.hit && b2Dot(hit.normal, pr->plane.normal) > 0.999f;
109}
110
111static bool moverPlaneCallback(b2ShapeId shapeId, const b2PlaneResult* pr, void* context) {
112 (void)context;
113 if (!pr->hit) return true;
114 const uint32_t entity = entityFromShape(shapeId);
115 // Never collide the mover with its own body (entity filter — robust regardless
116 // of the collision-category setup).
117 if (entity == g_moverSelfEntity) return true;
118 // A trigger is walked through: it reports an overlap, and blocks nothing.
119 if (b2Shape_IsSensor(shapeId)) return true;
120 const bool dynamic = b2Body_GetType(b2Shape_GetBody(shapeId)) == b2_dynamicBody;
121 if (dynamic && (g_moverFlags & MOVER_IGNORE_DYNAMIC)) return true;
122 if (!oneWayAdmits(shapeId, entity, pr->plane.normal, pr->plane.offset)) return true;
123 if (dynamic && g_moverHitCount < MAX_MOVER_HITS) {
124 bool known = false;
125 for (int i = 0; i < g_moverHitCount; ++i) known = known || g_moverHitEntity[i] == entity;
126 if (!known) {
128 g_moverHitNormal[g_moverHitCount++] = pr->plane.normal;
129 }
130 }
134 g_moverPlanes[g_moverPlaneCount].plane = pr->plane;
135 g_moverPlanes[g_moverPlaneCount].pushLimit = FLT_MAX;
136 g_moverPlanes[g_moverPlaneCount].push = 0.0f;
137 g_moverPlanes[g_moverPlaneCount].clipVelocity = true;
139 }
140 return true;
141}
142
143// Floor-snap down-probe: tracks the closest floor-classified hit (excluding self).
144// Returns 1.0 for every hit so the cast evaluates all shapes; the closest floor is
145// kept manually rather than via cast clipping (a nearer wall must not hide a floor).
146static float g_snapClosest = 1.0f;
147static b2Vec2 g_snapNormal = {0.0f, 0.0f};
148static uint32_t g_snapEntity = 0xFFFFFFFF;
149static bool g_snapHit = false;
150static b2Vec2 g_snapUp = {0.0f, 1.0f};
151static float g_snapFloorCos = 0.0f;
152
153static float snapCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void* context) {
154 (void)point;
155 (void)context;
156 const uint32_t entity = entityFromShape(shapeId);
157 if (entity == g_moverSelfEntity || b2Shape_IsSensor(shapeId)) return 1.0f;
158 if ((g_moverFlags & MOVER_IGNORE_DYNAMIC) && b2Body_GetType(b2Shape_GetBody(shapeId)) == b2_dynamicBody) return 1.0f;
159 if (!oneWayAdmits(shapeId, entity, normal, 0.0f)) return 1.0f;
160 float d = normal.x * g_snapUp.x + normal.y * g_snapUp.y;
161 if (d >= g_snapFloorCos && fraction < g_snapClosest) {
162 g_snapClosest = fraction;
163 g_snapNormal = normal;
164 g_snapEntity = entity;
165 g_snapHit = true;
166 }
167 return 1.0f;
168}
169
170// How far a cast up gets before something overhead stops it, for a mover stepping over a ledge.
171static float g_headroom = 1.0f;
172
173static float headroomCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void* context) {
174 (void)point;
175 (void)context;
176 const uint32_t entity = entityFromShape(shapeId);
177 if (entity == g_moverSelfEntity || b2Shape_IsSensor(shapeId)) return 1.0f;
178 if (g_ctx.oneWayNormals.count(entity) != 0) return 1.0f;
179 // It is already touching the ledge and the floor, and a cast starting in touch reports them with no normal. Only what faces down is overhead.
180 if (b2Dot(normal, g_snapUp) > -0.1f) return 1.0f;
181 if ((g_moverFlags & MOVER_IGNORE_DYNAMIC) && b2Body_GetType(b2Shape_GetBody(shapeId)) == b2_dynamicBody) return 1.0f;
182 if (fraction < g_headroom) g_headroom = fraction;
183 return 1.0f;
184}
185
186namespace {
187
189struct MoverShape {
190 b2Vec2 c1, c2;
191 float radius;
192 b2QueryFilter filter;
193 int slides;
194};
195
196b2Capsule capsuleAt(const MoverShape& shape, b2Vec2 pos) {
197 b2Capsule capsule;
198 capsule.center1 = b2Add(pos, shape.c1);
199 capsule.center2 = b2Add(pos, shape.c2);
200 capsule.radius = shape.radius;
201 return capsule;
202}
203
204b2ShapeProxy proxyAt(const MoverShape& shape, b2Vec2 pos, b2Vec2 points[2]) {
205 points[0] = b2Add(pos, shape.c1);
206 points[1] = b2Add(pos, shape.c2);
207 return b2MakeProxy(points, 2, shape.radius);
208}
209
211b2Vec2 withoutClimbing(b2Vec2 delta, b2Vec2 up, float floorCos) {
212 for (int i = 0; i < g_moverPlaneCount; ++i) {
213 const b2Vec2 n = g_moverPlanes[i].plane.normal;
214 const float d = b2Dot(n, up);
215 if (d <= 0.0f || d >= floorCos || !g_moverPlaneSurface[i]) continue;
216 const b2Vec2 across = b2Normalize(b2Sub(n, b2MulSV(d, up)));
217 const float into = b2Dot(delta, across);
218 if (into < 0.0f) delta = b2Sub(delta, b2MulSV(into, across));
219 }
220 return delta;
221}
222
224b2Vec2 solveMove(const MoverShape& shape, b2Vec2 pos, b2Vec2 target, b2Vec2 up, float floorCos) {
225 const float tolerance = 0.001f;
226 for (int iteration = 0; iteration < shape.slides; ++iteration) {
228 b2Capsule mover = capsuleAt(shape, pos);
229 b2World_CollideMover(g_ctx.worldId, &mover, shape.filter, moverPlaneCallback, nullptr);
230 if (g_moverFlags & MOVER_BLOCK_STEEP) target = b2Add(pos, withoutClimbing(b2Sub(target, pos), up, floorCos));
231 b2PlaneSolverResult result = b2SolvePlanes(b2Sub(target, pos), g_moverPlanes, g_moverPlaneCount);
232 pos = b2Add(pos, result.translation);
233 if (b2LengthSquared(result.translation) < tolerance * tolerance) break;
234 }
235 return pos;
236}
237
238struct StepResult { bool stepped; b2Vec2 pos; b2Vec2 normal; uint32_t floor; };
239
241StepResult stepOver(const MoverShape& shape, b2Vec2 pos, b2Vec2 left, b2Vec2 up, float floorCos, float height) {
242 StepResult out{false, pos, {0.0f, 0.0f}, 0xFFFFFFFF};
243 b2Vec2 points[2];
244 g_headroom = 1.0f;
245 b2ShapeProxy proxy = proxyAt(shape, pos, points);
246 b2World_CastShape(g_ctx.worldId, &proxy, b2MulSV(height, up), shape.filter, headroomCastCallback, nullptr);
247 const float rise = height * g_headroom;
248 if (rise <= 0.001f) return out;
249
250 const b2Vec2 raised = b2Add(pos, b2MulSV(rise, up));
251 const b2Vec2 across = solveMove(shape, raised, b2Add(raised, left), up, floorCos);
252 const float leftLength = b2Length(left);
253 if (b2Dot(b2Sub(across, raised), left) <= 0.25f * leftLength * leftLength) return out;
254
255 // A foot only just over the edge lands on the corner, whose normal leans: anything that holds it up from below will do.
256 g_snapClosest = 1.0f;
257 g_snapHit = false;
258 g_snapFloorCos = 0.01f;
259 proxy = proxyAt(shape, across, points);
260 const b2Vec2 down = b2MulSV(-rise, up);
261 b2World_CastShape(g_ctx.worldId, &proxy, down, shape.filter, snapCastCallback, nullptr);
262 g_snapFloorCos = floorCos;
263 if (!g_snapHit) return out;
264 out.stepped = true;
265 out.pos = b2Add(across, b2MulSV(g_snapClosest, down));
266 out.normal = up;
267 out.floor = g_snapEntity;
268 return out;
269}
270
271} // namespace
272
273extern "C" {
274
276int physics_raycast(float originX, float originY, float dirX, float dirY,
277 float maxDistance, uint32_t maskBits) {
278 if (!b2World_IsValid(g_ctx.worldId)) return 0;
279
280 g_raycastBuffer.clear();
281
282 b2Vec2 origin = {originX, originY};
283 b2Vec2 translation = {dirX * maxDistance, dirY * maxDistance};
284
285 b2QueryFilter filter = b2DefaultQueryFilter();
286 filter.maskBits = static_cast<uint64_t>(maskBits);
287
288 b2World_CastRay(g_ctx.worldId, origin, translation, filter, raycastCallback, nullptr);
289
290 return static_cast<int>(g_raycastBuffer.size() / RAYCAST_STRIDE);
291}
292
295 return reinterpret_cast<uintptr_t>(g_raycastBuffer.data());
296}
297
299int physics_overlapCircle(float centerX, float centerY, float radius, uint32_t maskBits) {
300 if (!b2World_IsValid(g_ctx.worldId)) return 0;
301
302 g_overlapBuffer.clear();
303
304 b2Vec2 center = {centerX, centerY};
305 b2ShapeProxy proxy = b2MakeProxy(&center, 1, radius);
306
307 b2QueryFilter filter = b2DefaultQueryFilter();
308 filter.maskBits = static_cast<uint64_t>(maskBits);
309
310 b2World_OverlapShape(g_ctx.worldId, &proxy, filter, overlapCallback, nullptr);
311
312 return static_cast<int>(g_overlapBuffer.size());
313}
314
317 return reinterpret_cast<uintptr_t>(g_overlapBuffer.data());
318}
319
321int physics_shapeCastCircle(float centerX, float centerY, float radius,
322 float translationX, float translationY, uint32_t maskBits) {
323 if (!b2World_IsValid(g_ctx.worldId)) return 0;
324
325 g_shapeCastBuffer.clear();
326
327 b2Vec2 center = {centerX, centerY};
328 b2ShapeProxy proxy = b2MakeProxy(&center, 1, radius);
329 b2Vec2 translation = {translationX, translationY};
330
331 b2QueryFilter filter = b2DefaultQueryFilter();
332 filter.maskBits = static_cast<uint64_t>(maskBits);
333
334 b2World_CastShape(g_ctx.worldId, &proxy, translation, filter, shapeCastCallback, nullptr);
335
336 return static_cast<int>(g_shapeCastBuffer.size() / SHAPECAST_STRIDE);
337}
338
340int physics_shapeCastBox(float centerX, float centerY, float halfW, float halfH, float angle,
341 float translationX, float translationY, uint32_t maskBits) {
342 if (!b2World_IsValid(g_ctx.worldId)) return 0;
343
344 g_shapeCastBuffer.clear();
345
346 b2Polygon box = b2MakeOffsetBox(halfW, halfH, {centerX, centerY}, b2MakeRot(angle));
347 b2ShapeProxy proxy = b2MakeProxy(box.vertices, box.count, box.radius);
348 b2Vec2 translation = {translationX, translationY};
349
350 b2QueryFilter filter = b2DefaultQueryFilter();
351 filter.maskBits = static_cast<uint64_t>(maskBits);
352
353 b2World_CastShape(g_ctx.worldId, &proxy, translation, filter, shapeCastCallback, nullptr);
354
355 return static_cast<int>(g_shapeCastBuffer.size() / SHAPECAST_STRIDE);
356}
357
359int physics_shapeCastCapsule(float center1X, float center1Y, float center2X, float center2Y,
360 float radius, float translationX, float translationY, uint32_t maskBits) {
361 if (!b2World_IsValid(g_ctx.worldId)) return 0;
362
363 g_shapeCastBuffer.clear();
364
365 b2Vec2 centers[2] = {{center1X, center1Y}, {center2X, center2Y}};
366 b2ShapeProxy proxy = b2MakeProxy(centers, 2, radius);
367 b2Vec2 translation = {translationX, translationY};
368
369 b2QueryFilter filter = b2DefaultQueryFilter();
370 filter.maskBits = static_cast<uint64_t>(maskBits);
371
372 b2World_CastShape(g_ctx.worldId, &proxy, translation, filter, shapeCastCallback, nullptr);
373
374 return static_cast<int>(g_shapeCastBuffer.size() / SHAPECAST_STRIDE);
375}
376
379 return reinterpret_cast<uintptr_t>(g_shapeCastBuffer.data());
380}
381
383int physics_overlapAABB(float minX, float minY, float maxX, float maxY, uint32_t maskBits) {
384 if (!b2World_IsValid(g_ctx.worldId)) return 0;
385
386 g_overlapBuffer.clear();
387
388 b2AABB aabb = {{minX, minY}, {maxX, maxY}};
389 b2QueryFilter filter = b2DefaultQueryFilter();
390 filter.maskBits = static_cast<uint64_t>(maskBits);
391
392 b2World_OverlapAABB(g_ctx.worldId, aabb, filter, overlapCallback, nullptr);
393
394 return static_cast<int>(g_overlapBuffer.size());
395}
396
397// Move a capsule character one step, in meters. `carryX/Y` moves it with what it stands on, apart from its own velocity.
398// Writes [dx, dy, velX, velY, onFloor, onWall, onCeiling, floorNx, floorNy, floorEntityBits, hitCount, (entityBits, nx, ny) * 4]
399// and returns 1, or 0 if the world is gone.
401int physics_moveCharacter(float px, float py, float c1x, float c1y, float c2x, float c2y, float radius,
402 float velX, float velY, float dt, float upX, float upY, float floorCos,
403 uint32_t maskBits, uint32_t selfEntity,
404 float skinWidth, int maxSlides, float snapLength, int slideOnCeiling,
405 float carryX, float carryY, uint32_t flags, float stepHeight) {
406 if (!b2World_IsValid(g_ctx.worldId)) return 0;
407 g_moverSelfEntity = selfEntity;
408 g_moverFlags = flags;
409 g_moverVelocity = {velX, velY};
410 g_moverDt = dt;
411 g_moverHitCount = 0;
412
413 b2Vec2 pos = {px, py};
414 b2Vec2 up = {upX, upY};
415 b2Vec2 start = pos;
416 b2Vec2 target = {px + (velX + carryX) * dt, py + (velY + carryY) * dt};
417
418 b2QueryFilter filter = b2DefaultQueryFilter();
419 filter.maskBits = static_cast<uint64_t>(maskBits);
420
421 // skinWidth keeps the capsule a small margin off surfaces (less jitter, no
422 // visual overlap) — a slightly larger collision radius does exactly that.
423 MoverShape shape{{c1x, c1y}, {c2x, c2y}, radius + (skinWidth > 0.0f ? skinWidth : 0.0f), filter, maxSlides > 0 ? maxSlides : 1};
424
425 pos = solveMove(shape, pos, target, up, floorCos);
426
427 b2Vec2 vel = {velX, velY};
428 // Clipped as it is, the speed walked into a steep slope turns into climbing it on the next move.
429 if (flags & MOVER_BLOCK_STEEP) vel = withoutClimbing(vel, up, floorCos);
430 b2Vec2 newVel = b2ClipVector(vel, g_moverPlanes, g_moverPlaneCount);
431
432 // Classify the final contact planes against the up axis.
433 bool onFloor = false, onWall = false, onCeiling = false;
434 float fnx = 0.0f, fny = 0.0f;
435 uint32_t floorEntity = 0xFFFFFFFF;
436 for (int i = 0; i < g_moverPlaneCount; ++i) {
437 b2Vec2 n = g_moverPlanes[i].plane.normal;
438 float d = n.x * upX + n.y * upY;
439 if (d >= floorCos) { onFloor = true; fnx = n.x; fny = n.y; floorEntity = g_moverPlaneEntity[i]; }
440 else if (d <= -floorCos) { onCeiling = true; }
441 else { onWall = true; }
442 }
443
444 // slideOnCeiling=false: a ceiling hit cancels the upward velocity instead of
445 // sliding along it, so the character drops straight back down.
446 if (onCeiling && slideOnCeiling == 0) {
447 float vUp = newVel.x * upX + newVel.y * upY;
448 if (vUp > 0.0f) { newVel.x -= vUp * upX; newVel.y -= vUp * upY; }
449 }
450
451 g_snapUp = up;
452 g_snapFloorCos = floorCos;
453
454 // A ledge low enough to step over stops a walking character only for this contact: its own walking
455 // goes on, and neither a jump nor a fall steps up onto anything.
456 if (stepHeight > 0.0f && onWall && (flags & MOVER_WAS_ON_FLOOR) != 0 && b2Dot(vel, up) <= 0.01f) {
457 const b2Vec2 wanted = b2Sub(target, start);
458 const b2Vec2 along = b2Sub(wanted, b2MulSV(b2Dot(wanted, up), up));
459 const float length = b2Length(along);
460 if (length > 0.0001f) {
461 const b2Vec2 direction = b2MulSV(1.0f / length, along);
462 const float remaining = length - b2Dot(b2Sub(pos, start), direction);
463 if (remaining > 0.0001f) {
464 const StepResult step = stepOver(shape, pos, b2MulSV(remaining, direction), up, floorCos, stepHeight);
465 if (step.stepped) {
466 pos = step.pos;
467 onFloor = true;
468 onWall = false;
469 fnx = step.normal.x;
470 fny = step.normal.y;
471 floorEntity = step.floor;
472 newVel = vel;
473 const float vn = b2Dot(newVel, step.normal);
474 if (vn < 0.0f) newVel = b2Sub(newVel, b2MulSV(vn, step.normal));
475 }
476 }
477 }
478 }
479
480 // Floor snap: when not rising and not already grounded, probe down up to
481 // snapLength and stick to a floor within reach, so the character stays glued to
482 // descending stairs/slopes instead of launching off each ledge.
483 if (snapLength > 0.0f && !onFloor) {
484 float vUp = newVel.x * upX + newVel.y * upY;
485 if (vUp <= 0.01f) {
486 g_snapClosest = 1.0f;
487 g_snapHit = false;
488 b2Vec2 pts[2];
489 b2ShapeProxy proxy = proxyAt(shape, pos, pts);
490 b2Vec2 down = {-upX * snapLength, -upY * snapLength};
491 b2World_CastShape(g_ctx.worldId, &proxy, down, filter, snapCastCallback, nullptr);
492 if (g_snapHit) {
493 pos = b2Add(pos, {down.x * g_snapClosest, down.y * g_snapClosest});
494 onFloor = true;
495 fnx = g_snapNormal.x;
496 fny = g_snapNormal.y;
497 floorEntity = g_snapEntity;
498 float vn = newVel.x * g_snapNormal.x + newVel.y * g_snapNormal.y;
499 if (vn < 0.0f) { newVel.x -= vn * g_snapNormal.x; newVel.y -= vn * g_snapNormal.y; }
500 }
501 }
502 }
503
504 g_moverBuffer[0] = pos.x - px;
505 g_moverBuffer[1] = pos.y - py;
506 g_moverBuffer[2] = newVel.x;
507 g_moverBuffer[3] = newVel.y;
508 g_moverBuffer[4] = onFloor ? 1.0f : 0.0f;
509 g_moverBuffer[5] = onWall ? 1.0f : 0.0f;
510 g_moverBuffer[6] = onCeiling ? 1.0f : 0.0f;
511 g_moverBuffer[7] = fnx;
512 g_moverBuffer[8] = fny;
513 std::memcpy(&g_moverBuffer[9], &floorEntity, sizeof(uint32_t));
514 g_moverBuffer[10] = static_cast<float>(g_moverHitCount);
515 for (int i = 0; i < g_moverHitCount; ++i) {
516 std::memcpy(&g_moverBuffer[11 + i * 3], &g_moverHitEntity[i], sizeof(uint32_t));
517 g_moverBuffer[12 + i * 3] = g_moverHitNormal[i].x;
518 g_moverBuffer[13 + i * 3] = g_moverHitNormal[i].y;
519 }
520 return 1;
521}
522
525 return reinterpret_cast<uintptr_t>(g_moverBuffer);
526}
527
528} // extern "C"
#define EMSCRIPTEN_KEEPALIVE
The Box2D module's entry points, declared once for both platforms.
PhysicsContext g_ctx
std::vector< float > g_shapeCastBuffer
uint32_t entityFromShape(b2ShapeId shapeId)
void pushEntityBits(std::vector< float > &buf, uint32_t entityId)
std::vector< float > g_raycastBuffer
std::vector< float > g_overlapBuffer
static constexpr int MAX_RAYCAST_HITS
static constexpr int SHAPECAST_STRIDE
static constexpr int RAYCAST_STRIDE
static constexpr int MAX_OVERLAP_HITS
uintptr_t physics_getShapeCastBuffer()
uintptr_t physics_getOverlapBuffer()
static bool moverPlaneCallback(b2ShapeId shapeId, const b2PlaneResult *pr, void *context)
static float g_moverBuffer[11+MAX_MOVER_HITS *3]
static b2Vec2 g_moverHitNormal[MAX_MOVER_HITS]
static constexpr float MOVER_ONE_WAY_SLOP
static float shapeCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void *context)
static int g_moverPlaneCount
static b2Vec2 g_moverVelocity
static constexpr uint32_t MOVER_WAS_ON_FLOOR
int physics_shapeCastCircle(float centerX, float centerY, float radius, float translationX, float translationY, uint32_t maskBits)
static constexpr uint32_t MOVER_DROP_THROUGH
static uint32_t g_moverHitEntity[MAX_MOVER_HITS]
static bool overlapCallback(b2ShapeId shapeId, void *context)
static bool oneWayAdmits(b2ShapeId shapeId, uint32_t entity, b2Vec2 normal, float depth)
static constexpr int MAX_MOVER_PLANES
static uint32_t g_snapEntity
int physics_raycast(float originX, float originY, float dirX, float dirY, float maxDistance, uint32_t maskBits)
static uint32_t g_moverSelfEntity
int physics_overlapAABB(float minX, float minY, float maxX, float maxY, uint32_t maskBits)
uintptr_t physics_getMoveCharacterBuffer()
static constexpr float MOVER_ONE_WAY_ALIGN
int physics_moveCharacter(float px, float py, float c1x, float c1y, float c2x, float c2y, float radius, float velX, float velY, float dt, float upX, float upY, float floorCos, uint32_t maskBits, uint32_t selfEntity, float skinWidth, int maxSlides, float snapLength, int slideOnCeiling, float carryX, float carryY, uint32_t flags, float stepHeight)
uintptr_t physics_getRaycastBuffer()
int physics_shapeCastBox(float centerX, float centerY, float halfW, float halfH, float angle, float translationX, float translationY, uint32_t maskBits)
static int g_moverHitCount
static bool normalIsSurface(b2ShapeId shapeId, const b2PlaneResult *pr)
static uint32_t g_moverPlaneEntity[MAX_MOVER_PLANES]
int physics_shapeCastCapsule(float center1X, float center1Y, float center2X, float center2Y, float radius, float translationX, float translationY, uint32_t maskBits)
static bool g_snapHit
static constexpr int MAX_MOVER_HITS
static b2Vec2 g_snapNormal
static constexpr uint32_t MOVER_BLOCK_STEEP
static bool g_moverPlaneSurface[MAX_MOVER_PLANES]
static b2Vec2 g_snapUp
static float raycastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void *context)
static float g_headroom
static b2CollisionPlane g_moverPlanes[MAX_MOVER_PLANES]
static float g_moverDt
static uint32_t g_moverFlags
static constexpr uint32_t MOVER_IGNORE_DYNAMIC
static float snapCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void *context)
static float g_snapClosest
static float g_snapFloorCos
static float headroomCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void *context)
int physics_overlapCircle(float centerX, float centerY, float radius, uint32_t maskBits)