Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
PhysicsContext.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5// The module's entry points are marked KEEPALIVE so the emscripten link keeps them
6// exported. A native build compiles the same TUs into the host binary, where the
7// attribute has no meaning and the header does not exist.
8#ifdef __EMSCRIPTEN__
9#include <emscripten.h>
10#else
11#define EMSCRIPTEN_KEEPALIVE
12#endif
13
14#include <box2d/box2d.h>
15
16#include <unordered_map>
17#include <vector>
18#include <cstdint>
19#include <cstring>
20
21static constexpr int MAX_PHYSICS_STEPS_PER_FRAME = 8;
22static constexpr int RAYCAST_STRIDE = 6;
23static constexpr int MAX_RAYCAST_HITS = 64;
24static constexpr int MAX_OVERLAP_HITS = 64;
25static constexpr int SHAPECAST_STRIDE = 6;
26
28 b2WorldId worldId = b2_nullWorldId;
29 float fixedTimestep = 1.0f / 60.0f;
30 int subStepCount = 4;
31 float accumulator = 0.0f;
32
33 std::unordered_map<uint32_t, b2BodyId> entityToBody;
34 std::unordered_map<uint32_t, std::vector<b2ShapeId>> entityToShapes;
35 std::unordered_map<uint32_t, b2JointId> entityToJoint;
36 std::vector<uint32_t> dynamicBodyEntities;
37
38 // Box2D discards solver velocity when a body moves to the disabled set.
39 struct PausedMotion {
42 };
43 std::unordered_map<uint32_t, PausedMotion> pausedMotion;
44
45 // One-way platforms: entity -> normalized solid-side normal and coordinate frame.
46 // Read by the pre-solve callback to cancel contacts that approach
47 // from the pass-through side. Empty map = zero pre-solve overhead.
48 struct OneWayNormal {
49 b2Vec2 normal;
50 bool localSpace = false;
51 };
52 std::unordered_map<uint32_t, OneWayNormal> oneWayNormals;
53
54 // Interactive drag: a single kinematic "mouse body" + motor joint pulling one
55 // target body toward the cursor (Box2D v3 has no dedicated mouse joint).
56 b2BodyId mouseBodyId = b2_nullBodyId;
57 b2JointId mouseJointId = b2_nullJointId;
58
59 std::vector<float> dynamicTransformBuffer;
60
61 // Render interpolation, kept here rather than in JS: the two most recent
62 // fixed-step poses and the lerped result, all in the same flat
63 // [entityBits, x, y, angle] layout the readback hands out. A per-body object
64 // graph on the JS side would put a 5000-iteration hot loop on the every-frame
65 // path, which the no-JIT budget forbids (docs/REARCH_NATIVE.md ยง3.2).
66 std::vector<float> posePrev;
67 std::vector<float> poseCur;
68 std::vector<float> poseInterpolated;
69 // Maps entity -> index into posePrev, built lazily and only when the body set
70 // changed between the two snapshots; steady state matches by position instead.
71 std::unordered_map<uint32_t, uint32_t> posePrevIndex;
72
73 std::vector<float> collisionEnterBuffer;
74 std::vector<float> collisionExitBuffer;
75 std::vector<float> sensorEnterBuffer;
76 std::vector<float> sensorExitBuffer;
77 std::vector<float> hitEventBuffer;
78
79 float velocityBuffer[2] = {};
80 float gravityBuffer[2] = {};
81
82 void reset() {
83 entityToBody.clear();
84 entityToShapes.clear();
85 entityToJoint.clear();
86 pausedMotion.clear();
87 oneWayNormals.clear();
88 mouseBodyId = b2_nullBodyId;
89 mouseJointId = b2_nullJointId;
90 dynamicBodyEntities.clear();
92 posePrev.clear();
93 poseCur.clear();
94 poseInterpolated.clear();
95 posePrevIndex.clear();
97 collisionExitBuffer.clear();
98 sensorEnterBuffer.clear();
99 sensorExitBuffer.clear();
100 hitEventBuffer.clear();
101 accumulator = 0.0f;
102
103 if (b2World_IsValid(worldId)) {
104 b2DestroyWorld(worldId);
105 }
106 worldId = b2_nullWorldId;
107 }
108};
109
110extern PhysicsContext g_ctx;
111
112extern std::vector<float> g_raycastBuffer;
113extern std::vector<float> g_overlapBuffer;
114extern std::vector<float> g_shapeCastBuffer;
115extern float g_massDataBuffer[2];
116
117b2BodyId findValidBody(uint32_t entityId);
118b2JointId findValidJoint(uint32_t entityId);
119uint32_t entityFromBody(b2BodyId bodyId);
120uint32_t entityFromShape(b2ShapeId shapeId);
121void pushEntityBits(std::vector<float>& buf, uint32_t entityId);
float g_massDataBuffer[2]
PhysicsContext g_ctx
std::vector< float > g_shapeCastBuffer
std::vector< float > g_raycastBuffer
std::vector< float > g_overlapBuffer
b2JointId findValidJoint(uint32_t entityId)
static constexpr int MAX_RAYCAST_HITS
uint32_t entityFromBody(b2BodyId bodyId)
static constexpr int SHAPECAST_STRIDE
static constexpr int MAX_PHYSICS_STEPS_PER_FRAME
static constexpr int RAYCAST_STRIDE
static constexpr int MAX_OVERLAP_HITS
uint32_t entityFromShape(b2ShapeId shapeId)
void pushEntityBits(std::vector< float > &buf, uint32_t entityId)
b2BodyId findValidBody(uint32_t entityId)
std::vector< float > hitEventBuffer
std::unordered_map< uint32_t, std::vector< b2ShapeId > > entityToShapes
std::vector< float > posePrev
float velocityBuffer[2]
std::vector< float > dynamicTransformBuffer
std::vector< float > poseInterpolated
std::unordered_map< uint32_t, PausedMotion > pausedMotion
std::unordered_map< uint32_t, OneWayNormal > oneWayNormals
std::vector< float > collisionEnterBuffer
std::vector< float > poseCur
std::unordered_map< uint32_t, uint32_t > posePrevIndex
std::vector< float > sensorEnterBuffer
b2JointId mouseJointId
std::unordered_map< uint32_t, b2JointId > entityToJoint
std::unordered_map< uint32_t, b2BodyId > entityToBody
std::vector< float > collisionExitBuffer
std::vector< float > sensorExitBuffer
std::vector< uint32_t > dynamicBodyEntities