11 float offX,
float offY,
float radius,
12 float density,
float friction,
float restitution,
int isSensor,
13 uint32_t categoryBits, uint32_t maskBits) {
14 auto it =
g_ctx.entityToBody.find(entityId);
15 if (it ==
g_ctx.entityToBody.end())
return;
17 b2ShapeDef shapeDef = b2DefaultShapeDef();
18 shapeDef.density = density;
19 shapeDef.material.friction = friction;
20 shapeDef.material.restitution = restitution;
21 shapeDef.isSensor = isSensor != 0;
22 shapeDef.enableContactEvents =
true;
23 shapeDef.enableHitEvents =
true;
24 shapeDef.enableSensorEvents =
true;
25 shapeDef.filter.categoryBits =
static_cast<uint64_t
>(categoryBits);
26 shapeDef.filter.maskBits =
static_cast<uint64_t
>(maskBits);
30 float innerHalfW = halfW > radius ? halfW - radius : 0.0f;
31 float innerHalfH = halfH > radius ? halfH - radius : 0.0f;
32 polygon = b2MakeOffsetRoundedBox(innerHalfW, innerHalfH, {offX, offY}, b2MakeRot(0.0f), radius);
34 polygon = b2MakeOffsetBox(halfW, halfH, {offX, offY}, b2MakeRot(0.0f));
36 b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);
37 g_ctx.entityToShapes[entityId].push_back(shapeId);
42 float offX,
float offY,
43 float density,
float friction,
float restitution,
int isSensor,
44 uint32_t categoryBits, uint32_t maskBits) {
45 auto it =
g_ctx.entityToBody.find(entityId);
46 if (it ==
g_ctx.entityToBody.end())
return;
48 b2ShapeDef shapeDef = b2DefaultShapeDef();
49 shapeDef.density = density;
50 shapeDef.material.friction = friction;
51 shapeDef.material.restitution = restitution;
52 shapeDef.isSensor = isSensor != 0;
53 shapeDef.enableContactEvents =
true;
54 shapeDef.enableHitEvents =
true;
55 shapeDef.enableSensorEvents =
true;
56 shapeDef.filter.categoryBits =
static_cast<uint64_t
>(categoryBits);
57 shapeDef.filter.maskBits =
static_cast<uint64_t
>(maskBits);
60 circle.center = {offX, offY};
61 circle.radius = radius;
63 b2ShapeId shapeId = b2CreateCircleShape(it->second, &shapeDef, &circle);
64 g_ctx.entityToShapes[entityId].push_back(shapeId);
69 float offX,
float offY,
70 float density,
float friction,
float restitution,
int isSensor,
71 uint32_t categoryBits, uint32_t maskBits) {
72 auto it =
g_ctx.entityToBody.find(entityId);
73 if (it ==
g_ctx.entityToBody.end())
return;
75 b2ShapeDef shapeDef = b2DefaultShapeDef();
76 shapeDef.density = density;
77 shapeDef.material.friction = friction;
78 shapeDef.material.restitution = restitution;
79 shapeDef.isSensor = isSensor != 0;
80 shapeDef.enableContactEvents =
true;
81 shapeDef.enableHitEvents =
true;
82 shapeDef.enableSensorEvents =
true;
83 shapeDef.filter.categoryBits =
static_cast<uint64_t
>(categoryBits);
84 shapeDef.filter.maskBits =
static_cast<uint64_t
>(maskBits);
87 capsule.center1 = {offX, offY + halfHeight};
88 capsule.center2 = {offX, offY - halfHeight};
89 capsule.radius = radius;
91 b2ShapeId shapeId = b2CreateCapsuleShape(it->second, &shapeDef, &capsule);
92 g_ctx.entityToShapes[entityId].push_back(shapeId);
97 float density,
float friction,
float restitution,
int isSensor,
98 uint32_t categoryBits, uint32_t maskBits) {
99 auto it =
g_ctx.entityToBody.find(entityId);
100 if (it ==
g_ctx.entityToBody.end())
return;
102 b2ShapeDef shapeDef = b2DefaultShapeDef();
103 shapeDef.density = density;
104 shapeDef.material.friction = friction;
105 shapeDef.material.restitution = restitution;
106 shapeDef.isSensor = isSensor != 0;
107 shapeDef.enableContactEvents =
true;
108 shapeDef.enableHitEvents =
true;
109 shapeDef.enableSensorEvents =
true;
110 shapeDef.filter.categoryBits =
static_cast<uint64_t
>(categoryBits);
111 shapeDef.filter.maskBits =
static_cast<uint64_t
>(maskBits);
114 segment.point1 = {x1, y1};
115 segment.point2 = {x2, y2};
117 b2ShapeId shapeId = b2CreateSegmentShape(it->second, &shapeDef, &segment);
118 g_ctx.entityToShapes[entityId].push_back(shapeId);
123 float density,
float friction,
float restitution,
int isSensor,
124 uint32_t categoryBits, uint32_t maskBits) {
125 auto it =
g_ctx.entityToBody.find(entityId);
126 if (it ==
g_ctx.entityToBody.end())
return;
127 if (vertexCount < 3 || vertexCount > B2_MAX_POLYGON_VERTICES)
return;
129 b2ShapeDef shapeDef = b2DefaultShapeDef();
130 shapeDef.density = density;
131 shapeDef.material.friction = friction;
132 shapeDef.material.restitution = restitution;
133 shapeDef.isSensor = isSensor != 0;
134 shapeDef.enableContactEvents =
true;
135 shapeDef.enableHitEvents =
true;
136 shapeDef.enableSensorEvents =
true;
137 shapeDef.filter.categoryBits =
static_cast<uint64_t
>(categoryBits);
138 shapeDef.filter.maskBits =
static_cast<uint64_t
>(maskBits);
142 b2Vec2 points[B2_MAX_POLYGON_VERTICES];
143 for (
int i = 0; i < vertexCount; i++) {
144 points[i] = {floats[i * 2], floats[i * 2 + 1]};
147 b2Hull hull = b2ComputeHull(points, vertexCount);
148 if (hull.count == 0)
return;
150 b2Polygon polygon = (radius > 0.0f)
151 ? b2MakePolygon(&hull, radius)
152 : b2MakePolygon(&hull, 0.0f);
153 b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);
154 g_ctx.entityToShapes[entityId].push_back(shapeId);
159 float friction,
float restitution,
160 uint32_t categoryBits, uint32_t maskBits) {
161 auto it =
g_ctx.entityToBody.find(entityId);
162 if (it ==
g_ctx.entityToBody.end())
return;
165 if (pointCount < 4 || pointCount > 65536)
return;
169 std::vector<b2Vec2> points(pointCount);
170 for (
int i = 0; i < pointCount; i++) {
171 points[i] = {floats[i * 2], floats[i * 2 + 1]};
174 b2SurfaceMaterial material = b2DefaultSurfaceMaterial();
175 material.friction = friction;
176 material.restitution = restitution;
178 b2ChainDef chainDef = b2DefaultChainDef();
179 chainDef.points = points.data();
180 chainDef.count = pointCount;
181 chainDef.isLoop = isLoop != 0;
182 chainDef.materials = &material;
183 chainDef.materialCount = 1;
184 chainDef.filter.categoryBits =
static_cast<uint64_t
>(categoryBits);
185 chainDef.filter.maskBits =
static_cast<uint64_t
>(maskBits);
187 b2CreateChain(it->second, &chainDef);
197 auto it =
g_ctx.entityToShapes.find(entityId);
198 if (it ==
g_ctx.entityToShapes.end())
return;
199 for (b2ShapeId shapeId : it->second) {
200 if (b2Shape_IsValid(shapeId)) {
201 b2DestroyShape(shapeId,
false);
Always-on validation for JS-supplied pointer+length pairs at the WASM boundary.
#define EMSCRIPTEN_KEEPALIVE
The Box2D module's entry points, declared once for both platforms.
void physics_addSegmentShape(uint32_t entityId, float x1, float y1, float x2, float y2, float density, float friction, float restitution, int isSensor, uint32_t categoryBits, uint32_t maskBits)
void physics_addCapsuleShape(uint32_t entityId, float radius, float halfHeight, float offX, float offY, float density, float friction, float restitution, int isSensor, uint32_t categoryBits, uint32_t maskBits)
void physics_addCircleShape(uint32_t entityId, float radius, float offX, float offY, float density, float friction, float restitution, int isSensor, uint32_t categoryBits, uint32_t maskBits)
void physics_addBoxShape(uint32_t entityId, float halfW, float halfH, float offX, float offY, float radius, float density, float friction, float restitution, int isSensor, uint32_t categoryBits, uint32_t maskBits)
void physics_addChainShape(uint32_t entityId, uintptr_t pointsPtr, int pointCount, int isLoop, float friction, float restitution, uint32_t categoryBits, uint32_t maskBits)
void physics_clearShapes(uint32_t entityId)
void physics_addPolygonShape(uint32_t entityId, uintptr_t verticesPtr, int vertexCount, float radius, float density, float friction, float restitution, int isSensor, uint32_t categoryBits, uint32_t maskBits)
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
std::uint64_t u64
Unsigned 64-bit integer.