Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PhysicsShapes.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
6
7extern "C" {
8
10void physics_addBoxShape(uint32_t entityId, float halfW, float halfH,
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;
16
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; // Box2D v3: a sensor only detects a visitor whose shape also enables sensor events.
25 shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
26 shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
27
28 b2Polygon polygon;
29 if (radius > 0.0f) {
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);
33 } else {
34 polygon = b2MakeOffsetBox(halfW, halfH, {offX, offY}, b2MakeRot(0.0f));
35 }
36 b2ShapeId shapeId = b2CreatePolygonShape(it->second, &shapeDef, &polygon);
37 g_ctx.entityToShapes[entityId].push_back(shapeId);
38}
39
41void physics_addCircleShape(uint32_t entityId, float radius,
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;
47
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; // Box2D v3: a sensor only detects a visitor whose shape also enables sensor events.
56 shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
57 shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
58
59 b2Circle circle;
60 circle.center = {offX, offY};
61 circle.radius = radius;
62
63 b2ShapeId shapeId = b2CreateCircleShape(it->second, &shapeDef, &circle);
64 g_ctx.entityToShapes[entityId].push_back(shapeId);
65}
66
68void physics_addCapsuleShape(uint32_t entityId, float radius, float halfHeight,
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;
74
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; // Box2D v3: a sensor only detects a visitor whose shape also enables sensor events.
83 shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
84 shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
85
86 b2Capsule capsule;
87 capsule.center1 = {offX, offY + halfHeight};
88 capsule.center2 = {offX, offY - halfHeight};
89 capsule.radius = radius;
90
91 b2ShapeId shapeId = b2CreateCapsuleShape(it->second, &shapeDef, &capsule);
92 g_ctx.entityToShapes[entityId].push_back(shapeId);
93}
94
96void physics_addSegmentShape(uint32_t entityId, float x1, float y1, float x2, float y2,
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;
101
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; // Box2D v3: a sensor only detects a visitor whose shape also enables sensor events.
110 shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
111 shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
112
113 b2Segment segment;
114 segment.point1 = {x1, y1};
115 segment.point2 = {x2, y2};
116
117 b2ShapeId shapeId = b2CreateSegmentShape(it->second, &shapeDef, &segment);
118 g_ctx.entityToShapes[entityId].push_back(shapeId);
119}
120
122void physics_addPolygonShape(uint32_t entityId, uintptr_t verticesPtr, int vertexCount, float radius,
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;
128
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; // Box2D v3: a sensor only detects a visitor whose shape also enables sensor events.
137 shapeDef.filter.categoryBits = static_cast<uint64_t>(categoryBits);
138 shapeDef.filter.maskBits = static_cast<uint64_t>(maskBits);
139
140 auto* floats = esengine::boundarySpan<float>(verticesPtr, static_cast<esengine::u64>(vertexCount) * 2, "physics_addPolygonShape");
141 if (!floats) return;
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]};
145 }
146
147 b2Hull hull = b2ComputeHull(points, vertexCount);
148 if (hull.count == 0) return;
149
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);
155}
156
158void physics_addChainShape(uint32_t entityId, uintptr_t pointsPtr, int pointCount, int isLoop,
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;
163 // Chain points are unbounded by Box2D itself; cap at a sane ceiling so a wild
164 // count can't allocate gigabytes or walk the heap.
165 if (pointCount < 4 || pointCount > 65536) return;
166
167 auto* floats = esengine::boundarySpan<float>(pointsPtr, static_cast<esengine::u64>(pointCount) * 2, "physics_addChainShape");
168 if (!floats) 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]};
172 }
173
174 b2SurfaceMaterial material = b2DefaultSurfaceMaterial();
175 material.friction = friction;
176 material.restitution = restitution;
177
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);
186
187 b2CreateChain(it->second, &chainDef);
188}
189
190// Destroy all of an entity's shapes while KEEPING the body, so the reconciler can
191// rebuild colliders in place (velocity, pose, contacts-elsewhere, and joints are
192// preserved — far cheaper and less disruptive than destroying the whole body).
193// Chain shapes (b2CreateChain) aren't tracked in entityToShapes and so aren't
194// cleared here; chains are static level geometry and aren't runtime-rebuilt.
196void physics_clearShapes(uint32_t entityId) {
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); // mass is recomputed when shapes are re-added
202 }
203 }
204 it->second.clear();
205}
206
207} // extern "C"
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.
PhysicsContext g_ctx
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.
Definition Types.hpp:47