react-three-game 0.0.105 → 0.0.106
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,8 +8,9 @@ import { gameEvents } from "../../tools/prefabeditor/GameEvents";
|
|
|
8
8
|
import { PrefabEditorMode, useScene } from "../../tools/prefabeditor/PrefabRoot";
|
|
9
9
|
const SLEEP_TIME_BEFORE_REST = 0.1;
|
|
10
10
|
const SLEEP_POINT_VELOCITY_THRESHOLD = 0.06;
|
|
11
|
-
const
|
|
11
|
+
const FIXED_PHYSICS_STEP_DELTA = 1 / 60;
|
|
12
12
|
const MAX_PHYSICS_CATCH_UP_DELTA = 1 / 10;
|
|
13
|
+
const MAX_PHYSICS_STEPS_PER_FRAME = 4;
|
|
13
14
|
let didRegisterCrashcat = false;
|
|
14
15
|
function ensureCrashcatRegistered() {
|
|
15
16
|
if (didRegisterCrashcat)
|
|
@@ -68,6 +69,7 @@ export function CrashcatRuntime({ debug = false, children }) {
|
|
|
68
69
|
const bodiesRef = useRef(new Map());
|
|
69
70
|
const bodyByIdRef = useRef(new Map());
|
|
70
71
|
const apiRef = useRef(null);
|
|
72
|
+
const accumulatedDeltaRef = useRef(0);
|
|
71
73
|
const [debugState] = useState(() => debug ? createDebugState() : null);
|
|
72
74
|
const listener = useMemo(() => ({
|
|
73
75
|
onContactAdded: (bodyA, bodyB, manifold) => {
|
|
@@ -131,8 +133,10 @@ export function CrashcatRuntime({ debug = false, children }) {
|
|
|
131
133
|
getBody: (nodeId) => { var _a, _b; return (_b = (_a = bodies.get(nodeId)) === null || _a === void 0 ? void 0 : _a.body) !== null && _b !== void 0 ? _b : null; },
|
|
132
134
|
};
|
|
133
135
|
apiRef.current = runtimeApi;
|
|
136
|
+
accumulatedDeltaRef.current = 0;
|
|
134
137
|
setCrashcatApi(runtimeApi);
|
|
135
138
|
return () => {
|
|
139
|
+
accumulatedDeltaRef.current = 0;
|
|
136
140
|
for (const entry of bodies.values()) {
|
|
137
141
|
rigidBody.remove(world, entry.body);
|
|
138
142
|
}
|
|
@@ -152,11 +156,20 @@ export function CrashcatRuntime({ debug = false, children }) {
|
|
|
152
156
|
const { world } = runtimeApi;
|
|
153
157
|
const frameDelta = Math.min(delta, MAX_PHYSICS_CATCH_UP_DELTA);
|
|
154
158
|
if (mode === PrefabEditorMode.Play) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
+
accumulatedDeltaRef.current += frameDelta;
|
|
160
|
+
let stepCount = 0;
|
|
161
|
+
while (accumulatedDeltaRef.current >= FIXED_PHYSICS_STEP_DELTA &&
|
|
162
|
+
stepCount < MAX_PHYSICS_STEPS_PER_FRAME) {
|
|
163
|
+
updateWorld(world, listener, FIXED_PHYSICS_STEP_DELTA);
|
|
164
|
+
accumulatedDeltaRef.current -= FIXED_PHYSICS_STEP_DELTA;
|
|
165
|
+
stepCount += 1;
|
|
159
166
|
}
|
|
167
|
+
if (stepCount === MAX_PHYSICS_STEPS_PER_FRAME) {
|
|
168
|
+
accumulatedDeltaRef.current = Math.min(accumulatedDeltaRef.current, FIXED_PHYSICS_STEP_DELTA);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
accumulatedDeltaRef.current = 0;
|
|
160
173
|
}
|
|
161
174
|
if (debugState)
|
|
162
175
|
debugRenderer.update(debugState, world);
|