9static float raycastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal,
float fraction,
void* context) {
13 if (entityId == 0xFFFFFFFF)
return 1.0f;
29 if (entityId == 0xFFFFFFFF)
return true;
35static float shapeCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal,
float fraction,
void* context) {
39 if (entityId == 0xFFFFFFFF)
return 1.0f;
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;
89 const b2Vec2 solid = it->second.localSpace
90 ? b2RotateVector(b2Body_GetRotation(b2Shape_GetBody(shapeId)), it->second.normal)
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;
113 if (!pr->hit)
return true;
119 if (b2Shape_IsSensor(shapeId))
return true;
120 const bool dynamic = b2Body_GetType(b2Shape_GetBody(shapeId)) == b2_dynamicBody;
122 if (!
oneWayAdmits(shapeId, entity, pr->plane.normal, pr->plane.offset))
return true;
153static float snapCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal,
float fraction,
void* context) {
159 if (!
oneWayAdmits(shapeId, entity, normal, 0.0f))
return 1.0f;
173static float headroomCastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal,
float fraction,
void* context) {
178 if (
g_ctx.oneWayNormals.count(entity) != 0)
return 1.0f;
180 if (b2Dot(normal,
g_snapUp) > -0.1f)
return 1.0f;
192 b2QueryFilter filter;
196b2Capsule capsuleAt(
const MoverShape& shape, b2Vec2 pos) {
198 capsule.center1 = b2Add(pos, shape.c1);
199 capsule.center2 = b2Add(pos, shape.c2);
200 capsule.radius = shape.radius;
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);
211b2Vec2 withoutClimbing(b2Vec2 delta, b2Vec2 up,
float floorCos) {
214 const float d = b2Dot(n, up);
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));
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);
232 pos = b2Add(pos, result.translation);
233 if (b2LengthSquared(result.translation) < tolerance * tolerance)
break;
238struct StepResult {
bool stepped; b2Vec2 pos; b2Vec2 normal; uint32_t floor; };
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};
245 b2ShapeProxy proxy = proxyAt(shape, pos, points);
248 if (rise <= 0.001f)
return out;
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;
259 proxy = proxyAt(shape, across, points);
260 const b2Vec2 down = b2MulSV(-rise, up);
277 float maxDistance, uint32_t maskBits) {
278 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
282 b2Vec2 origin = {originX, originY};
283 b2Vec2 translation = {dirX * maxDistance, dirY * maxDistance};
285 b2QueryFilter filter = b2DefaultQueryFilter();
286 filter.maskBits =
static_cast<uint64_t
>(maskBits);
300 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
304 b2Vec2 center = {centerX, centerY};
305 b2ShapeProxy proxy = b2MakeProxy(¢er, 1, radius);
307 b2QueryFilter filter = b2DefaultQueryFilter();
308 filter.maskBits =
static_cast<uint64_t
>(maskBits);
322 float translationX,
float translationY, uint32_t maskBits) {
323 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
327 b2Vec2 center = {centerX, centerY};
328 b2ShapeProxy proxy = b2MakeProxy(¢er, 1, radius);
329 b2Vec2 translation = {translationX, translationY};
331 b2QueryFilter filter = b2DefaultQueryFilter();
332 filter.maskBits =
static_cast<uint64_t
>(maskBits);
341 float translationX,
float translationY, uint32_t maskBits) {
342 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
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};
350 b2QueryFilter filter = b2DefaultQueryFilter();
351 filter.maskBits =
static_cast<uint64_t
>(maskBits);
360 float radius,
float translationX,
float translationY, uint32_t maskBits) {
361 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
365 b2Vec2 centers[2] = {{center1X, center1Y}, {center2X, center2Y}};
366 b2ShapeProxy proxy = b2MakeProxy(centers, 2, radius);
367 b2Vec2 translation = {translationX, translationY};
369 b2QueryFilter filter = b2DefaultQueryFilter();
370 filter.maskBits =
static_cast<uint64_t
>(maskBits);
384 if (!b2World_IsValid(
g_ctx.worldId))
return 0;
388 b2AABB aabb = {{minX, minY}, {maxX, maxY}};
389 b2QueryFilter filter = b2DefaultQueryFilter();
390 filter.maskBits =
static_cast<uint64_t
>(maskBits);
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;
413 b2Vec2 pos = {px, py};
414 b2Vec2 up = {upX, upY};
416 b2Vec2 target = {px + (velX + carryX) * dt, py + (velY + carryY) * dt};
418 b2QueryFilter filter = b2DefaultQueryFilter();
419 filter.maskBits =
static_cast<uint64_t
>(maskBits);
423 MoverShape shape{{c1x, c1y}, {c2x, c2y}, radius + (skinWidth > 0.0f ? skinWidth : 0.0f), filter, maxSlides > 0 ? maxSlides : 1};
425 pos = solveMove(shape, pos, target, up, floorCos);
427 b2Vec2 vel = {velX, velY};
433 bool onFloor =
false, onWall =
false, onCeiling =
false;
434 float fnx = 0.0f, fny = 0.0f;
435 uint32_t floorEntity = 0xFFFFFFFF;
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; }
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; }
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);
471 floorEntity = step.floor;
473 const float vn = b2Dot(newVel, step.normal);
474 if (vn < 0.0f) newVel = b2Sub(newVel, b2MulSV(vn, step.normal));
483 if (snapLength > 0.0f && !onFloor) {
484 float vUp = newVel.x * upX + newVel.y * upY;
489 b2ShapeProxy proxy = proxyAt(shape, pos, pts);
490 b2Vec2 down = {-upX * snapLength, -upY * snapLength};
513 std::memcpy(&
g_moverBuffer[9], &floorEntity,
sizeof(uint32_t));
#define EMSCRIPTEN_KEEPALIVE
The Box2D module's entry points, declared once for both platforms.
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 constexpr int MAX_MOVER_HITS
static b2Vec2 g_snapNormal
static constexpr uint32_t MOVER_BLOCK_STEEP
static bool g_moverPlaneSurface[MAX_MOVER_PLANES]
static float raycastCallback(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void *context)
static b2CollisionPlane g_moverPlanes[MAX_MOVER_PLANES]
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)