reze-engine 0.16.0 → 0.16.2
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.
- package/README.md +139 -284
- package/dist/physics/constraint.d.ts +7 -0
- package/dist/physics/constraint.d.ts.map +1 -1
- package/dist/physics/constraint.js +7 -0
- package/dist/physics/physics.d.ts +12 -1
- package/dist/physics/physics.d.ts.map +1 -1
- package/dist/physics/physics.js +344 -47
- package/dist/physics/solver.d.ts.map +1 -1
- package/dist/physics/solver.js +215 -39
- package/dist/physics/world.d.ts.map +1 -1
- package/dist/physics/world.js +12 -1
- package/dist/physics-debug.d.ts +30 -0
- package/dist/physics-debug.d.ts.map +1 -0
- package/dist/physics-debug.js +526 -0
- package/dist/pmx-loader.d.ts.map +1 -1
- package/dist/pmx-loader.js +26 -13
- package/dist/shaders/passes/physics-debug.d.ts +2 -0
- package/dist/shaders/passes/physics-debug.d.ts.map +1 -0
- package/dist/shaders/passes/physics-debug.js +69 -0
- package/package.json +1 -1
- package/src/physics/constraint.ts +29 -5
- package/src/physics/physics.ts +355 -55
- package/src/physics/solver.ts +205 -38
- package/src/physics/world.ts +13 -1
- package/src/pmx-loader.ts +24 -12
- package/dist/gpu-profile.d.ts +0 -19
- package/dist/gpu-profile.d.ts.map +0 -1
- package/dist/gpu-profile.js +0 -120
- package/dist/physics/profile.d.ts +0 -18
- package/dist/physics/profile.d.ts.map +0 -1
- package/dist/physics/profile.js +0 -44
package/dist/physics/physics.js
CHANGED
|
@@ -24,6 +24,41 @@ export class RezePhysics {
|
|
|
24
24
|
this.contacts = new ContactPool();
|
|
25
25
|
this.prevPositions = new Float32Array(this.store.count * 3);
|
|
26
26
|
this.prevOrientations = new Float32Array(this.store.count * 4);
|
|
27
|
+
this.kinTargetPos = new Float32Array(this.store.count * 3);
|
|
28
|
+
this.kinTargetOri = new Float32Array(this.store.count * 4);
|
|
29
|
+
this.kinTargetVel = new Float32Array(this.store.count * 3);
|
|
30
|
+
this.kinTargetAngVel = new Float32Array(this.store.count * 3);
|
|
31
|
+
this.kinRoot = this.buildKinematicRoots();
|
|
32
|
+
this.teleportFlags = new Uint8Array(this.store.count);
|
|
33
|
+
}
|
|
34
|
+
// BFS over the joint graph from every kinematic body, assigning each
|
|
35
|
+
// reachable dynamic body the kinematic body it (transitively) hangs off.
|
|
36
|
+
buildKinematicRoots() {
|
|
37
|
+
const N = this.store.count;
|
|
38
|
+
const root = new Int32Array(N).fill(-1);
|
|
39
|
+
const types = this.store.type;
|
|
40
|
+
const adj = Array.from({ length: N }, () => []);
|
|
41
|
+
for (const c of this.constraints) {
|
|
42
|
+
adj[c.bodyA].push(c.bodyB);
|
|
43
|
+
adj[c.bodyB].push(c.bodyA);
|
|
44
|
+
}
|
|
45
|
+
const queue = [];
|
|
46
|
+
for (let i = 0; i < N; i++) {
|
|
47
|
+
if (types[i] === RigidbodyType.Static || types[i] === RigidbodyType.Kinematic) {
|
|
48
|
+
root[i] = i;
|
|
49
|
+
queue.push(i);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (let h = 0; h < queue.length; h++) {
|
|
53
|
+
const i = queue[h];
|
|
54
|
+
for (const j of adj[i]) {
|
|
55
|
+
if (root[j] !== -1)
|
|
56
|
+
continue;
|
|
57
|
+
root[j] = root[i];
|
|
58
|
+
queue.push(j);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return root;
|
|
27
62
|
}
|
|
28
63
|
// Snapshot the current body pose as the interpolation "previous" state.
|
|
29
64
|
savePrevState() {
|
|
@@ -75,25 +110,51 @@ export class RezePhysics {
|
|
|
75
110
|
// that skip frame 0 don't pop bodies on first step.
|
|
76
111
|
this.snapBodiesToBones(boneWorldMatrices);
|
|
77
112
|
this.savePrevState();
|
|
113
|
+
// Seed kinematic targets so the first frame's target-delta velocity
|
|
114
|
+
// is zero instead of a jump from the origin.
|
|
115
|
+
this.kinTargetPos.set(this.store.positions);
|
|
116
|
+
this.kinTargetOri.set(this.store.orientations);
|
|
78
117
|
this.firstFrame = false;
|
|
79
118
|
}
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
|
|
119
|
+
// Compute this frame's kinematic targets from the current bone pose. A
|
|
120
|
+
// target jump beyond what continuous motion can produce (timeline scrub)
|
|
121
|
+
// is a teleport, handled per kinematic root: rigidly carry that root's
|
|
122
|
+
// dynamic chains across the jump with zeroed momentum, snap the root,
|
|
123
|
+
// and keep simulating — dragging cloth through a discontinuity at the
|
|
124
|
+
// raw derived velocity is what used to explode the solver. Chains under
|
|
125
|
+
// unaffected roots keep their momentum untouched.
|
|
126
|
+
if (this.computeKinematicTargets(boneWorldMatrices, dt)) {
|
|
127
|
+
this.carryDynamicThroughTeleport();
|
|
128
|
+
this.snapKinematicToTargets(true);
|
|
129
|
+
this.savePrevState(); // prev == curr so interpolation doesn't streak
|
|
130
|
+
}
|
|
84
131
|
// Fixed-timestep substeps. The maxSubSteps cap prevents runaway after
|
|
85
132
|
// a long stall (tab backgrounded, etc.). Snapshot the pose before each step so
|
|
86
133
|
// after the loop prevState is one substep behind the live (current) state.
|
|
134
|
+
// Kinematic bodies split the remaining gap evenly across this frame's
|
|
135
|
+
// substeps (f = 1/substeps-left) and land exactly on the frame's bone
|
|
136
|
+
// pose by the last one: at 60 Hz render that reproduces the classic
|
|
137
|
+
// sync-once-per-frame behavior exactly (no fractional lag trembling
|
|
138
|
+
// against the rendered mesh), while at lower rates the per-substep
|
|
139
|
+
// constraint error stays bounded to one fixed step of bone motion.
|
|
87
140
|
this.timeAccum += dt;
|
|
88
|
-
let
|
|
89
|
-
|
|
141
|
+
let nSub = Math.floor(this.timeAccum / this.fixedTimeStep);
|
|
142
|
+
if (nSub > this.maxSubSteps)
|
|
143
|
+
nSub = this.maxSubSteps;
|
|
144
|
+
for (let k = 0; k < nSub; k++) {
|
|
90
145
|
this.savePrevState();
|
|
146
|
+
this.advanceKinematicToTargets(1 / (nSub - k));
|
|
91
147
|
this.world.step(this.store, this.constraints, this.contacts, this.fixedTimeStep);
|
|
148
|
+
this.restoreNonFiniteBodies();
|
|
92
149
|
this.timeAccum -= this.fixedTimeStep;
|
|
93
|
-
sub++;
|
|
94
150
|
}
|
|
95
|
-
if (
|
|
151
|
+
if (this.timeAccum >= this.fixedTimeStep) {
|
|
152
|
+
// Substep budget exhausted mid-catchup: drop the remaining time and
|
|
153
|
+
// snap kinematic bodies the rest of the way (zero velocity) so they
|
|
154
|
+
// don't start next frame lagging behind their bones.
|
|
96
155
|
this.timeAccum = 0;
|
|
156
|
+
this.snapKinematicToTargets(false);
|
|
157
|
+
}
|
|
97
158
|
// Fraction into the next (not-yet-taken) step; always in [0, 1).
|
|
98
159
|
const alpha = this.fixedTimeStep > 0 ? this.timeAccum / this.fixedTimeStep : 0;
|
|
99
160
|
this.applyDynamicsToBones(boneWorldMatrices, alpha);
|
|
@@ -130,21 +191,31 @@ export class RezePhysics {
|
|
|
130
191
|
av[i3 + 2] = 0;
|
|
131
192
|
}
|
|
132
193
|
}
|
|
133
|
-
//
|
|
134
|
-
//
|
|
135
|
-
// the
|
|
136
|
-
//
|
|
137
|
-
|
|
194
|
+
// Fill kinTargetPos/kinTargetOri = boneWorld × bodyOffset for every bone-
|
|
195
|
+
// bound Static/Kinematic body. Returns true if any target is discontinuous
|
|
196
|
+
// with the current body pose — farther than continuous motion can carry it
|
|
197
|
+
// in one render frame, or rotated more than 90°.
|
|
198
|
+
computeKinematicTargets(boneWorldMatrices, dt) {
|
|
138
199
|
const N = this.store.count;
|
|
139
200
|
const offsets = this.store.bodyOffsetMatrix;
|
|
140
201
|
const positions = this.store.positions;
|
|
141
202
|
const orientations = this.store.orientations;
|
|
142
|
-
const lv = this.store.linearVelocities;
|
|
143
|
-
const av = this.store.angularVelocities;
|
|
144
203
|
const types = this.store.type;
|
|
145
204
|
const boneIdx = this.store.boneIndex;
|
|
205
|
+
const tp = this.kinTargetPos;
|
|
206
|
+
const to = this.kinTargetOri;
|
|
207
|
+
// 250 units/s scaled by frame time, floored at 4 units: far above the
|
|
208
|
+
// fastest limb motion (a false positive freezes that chain's momentum
|
|
209
|
+
// for a frame, which reads as stutter), far below any real scrub jump.
|
|
210
|
+
const maxJump = Math.max(4, 250 * dt);
|
|
211
|
+
const maxJumpSq = maxJump * maxJump;
|
|
212
|
+
const flags = this.teleportFlags;
|
|
213
|
+
let teleport = false;
|
|
214
|
+
const tv = this.kinTargetVel;
|
|
215
|
+
const tav = this.kinTargetAngVel;
|
|
146
216
|
const invDt = dt > 0 ? 1 / dt : 0;
|
|
147
217
|
for (let i = 0; i < N; i++) {
|
|
218
|
+
flags[i] = 0;
|
|
148
219
|
const t = types[i];
|
|
149
220
|
if (t !== RigidbodyType.Static && t !== RigidbodyType.Kinematic)
|
|
150
221
|
continue;
|
|
@@ -154,45 +225,271 @@ export class RezePhysics {
|
|
|
154
225
|
Mat4.multiplyArrays(boneWorldMatrices[b].values, 0, offsets, i * 16, _bodyMat, 0);
|
|
155
226
|
const i3 = i * 3;
|
|
156
227
|
const i4 = i * 4;
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
const
|
|
228
|
+
// Previous frame's target — the reference for the trajectory velocity.
|
|
229
|
+
const oldTx = tp[i3 + 0], oldTy = tp[i3 + 1], oldTz = tp[i3 + 2];
|
|
230
|
+
const oldOx = to[i4 + 0], oldOy = to[i4 + 1], oldOz = to[i4 + 2], oldOw = to[i4 + 3];
|
|
231
|
+
tp[i3 + 0] = _bodyMat[12];
|
|
232
|
+
tp[i3 + 1] = _bodyMat[13];
|
|
233
|
+
tp[i3 + 2] = _bodyMat[14];
|
|
234
|
+
Mat4.toQuatFromArrayInto(_bodyMat, 0, _scratchQuat);
|
|
235
|
+
const nOx = _scratchQuat.x, nOy = _scratchQuat.y, nOz = _scratchQuat.z, nOw = _scratchQuat.w;
|
|
236
|
+
to[i4 + 0] = nOx;
|
|
237
|
+
to[i4 + 1] = nOy;
|
|
238
|
+
to[i4 + 2] = nOz;
|
|
239
|
+
to[i4 + 3] = nOw;
|
|
240
|
+
const dx = tp[i3 + 0] - positions[i3 + 0];
|
|
241
|
+
const dy = tp[i3 + 1] - positions[i3 + 1];
|
|
242
|
+
const dz = tp[i3 + 2] - positions[i3 + 2];
|
|
243
|
+
// |q·q'| = cos(θ/2); below cos(45°) the body turned more than 90°.
|
|
244
|
+
const dot = to[i4 + 0] * orientations[i4 + 0] +
|
|
245
|
+
to[i4 + 1] * orientations[i4 + 1] +
|
|
246
|
+
to[i4 + 2] * orientations[i4 + 2] +
|
|
247
|
+
to[i4 + 3] * orientations[i4 + 3];
|
|
248
|
+
if (dx * dx + dy * dy + dz * dz > maxJumpSq || Math.abs(dot) < 0.7071) {
|
|
249
|
+
flags[i] = 1;
|
|
250
|
+
teleport = true;
|
|
251
|
+
tv[i3 + 0] = 0;
|
|
252
|
+
tv[i3 + 1] = 0;
|
|
253
|
+
tv[i3 + 2] = 0;
|
|
254
|
+
tav[i3 + 0] = 0;
|
|
255
|
+
tav[i3 + 1] = 0;
|
|
256
|
+
tav[i3 + 2] = 0;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
tv[i3 + 0] = (tp[i3 + 0] - oldTx) * invDt;
|
|
260
|
+
tv[i3 + 1] = (tp[i3 + 1] - oldTy) * invDt;
|
|
261
|
+
tv[i3 + 2] = (tp[i3 + 2] - oldTz) * invDt;
|
|
262
|
+
// ω ≈ 2 · qDiff.xyz / dt with qDiff = qNew · conj(qOld). Shortest-arc
|
|
263
|
+
// sign keeps qDiff and −qDiff (same rotation) from doubling ω.
|
|
264
|
+
const cox = -oldOx, coy = -oldOy, coz = -oldOz, cow = oldOw;
|
|
265
|
+
const qdx = nOw * cox + nOx * cow + nOy * coz - nOz * coy;
|
|
266
|
+
const qdy = nOw * coy - nOx * coz + nOy * cow + nOz * cox;
|
|
267
|
+
const qdz = nOw * coz + nOx * coy - nOy * cox + nOz * cow;
|
|
268
|
+
const qdw = nOw * cow - nOx * cox - nOy * coy - nOz * coz;
|
|
269
|
+
const sign = qdw < 0 ? -1 : 1;
|
|
270
|
+
tav[i3 + 0] = 2 * sign * qdx * invDt;
|
|
271
|
+
tav[i3 + 1] = 2 * sign * qdy * invDt;
|
|
272
|
+
tav[i3 + 2] = 2 * sign * qdz * invDt;
|
|
273
|
+
}
|
|
274
|
+
return teleport;
|
|
275
|
+
}
|
|
276
|
+
// Move kinematic bodies fraction f of the way to the frame target with the
|
|
277
|
+
// target-trajectory velocity, so joints see continuous anchor motion (and a
|
|
278
|
+
// smooth velocity signal) instead of a frame-sized jump on substep 1.
|
|
279
|
+
advanceKinematicToTargets(f) {
|
|
280
|
+
const N = this.store.count;
|
|
281
|
+
const positions = this.store.positions;
|
|
282
|
+
const orientations = this.store.orientations;
|
|
283
|
+
const lv = this.store.linearVelocities;
|
|
284
|
+
const av = this.store.angularVelocities;
|
|
285
|
+
const types = this.store.type;
|
|
286
|
+
const boneIdx = this.store.boneIndex;
|
|
287
|
+
const tp = this.kinTargetPos;
|
|
288
|
+
const to = this.kinTargetOri;
|
|
289
|
+
const tv = this.kinTargetVel;
|
|
290
|
+
const tav = this.kinTargetAngVel;
|
|
291
|
+
for (let i = 0; i < N; i++) {
|
|
292
|
+
const t = types[i];
|
|
293
|
+
if (t !== RigidbodyType.Static && t !== RigidbodyType.Kinematic)
|
|
294
|
+
continue;
|
|
295
|
+
const b = boneIdx[i];
|
|
296
|
+
if (b < 0)
|
|
297
|
+
continue;
|
|
298
|
+
const i3 = i * 3;
|
|
299
|
+
const i4 = i * 4;
|
|
300
|
+
positions[i3 + 0] += (tp[i3 + 0] - positions[i3 + 0]) * f;
|
|
301
|
+
positions[i3 + 1] += (tp[i3 + 1] - positions[i3 + 1]) * f;
|
|
302
|
+
positions[i3 + 2] += (tp[i3 + 2] - positions[i3 + 2]) * f;
|
|
303
|
+
lv[i3 + 0] = tv[i3 + 0];
|
|
304
|
+
lv[i3 + 1] = tv[i3 + 1];
|
|
305
|
+
lv[i3 + 2] = tv[i3 + 2];
|
|
306
|
+
av[i3 + 0] = tav[i3 + 0];
|
|
307
|
+
av[i3 + 1] = tav[i3 + 1];
|
|
308
|
+
av[i3 + 2] = tav[i3 + 2];
|
|
309
|
+
// Shortest-arc nlerp toward the target orientation.
|
|
160
310
|
const oldOx = orientations[i4 + 0], oldOy = orientations[i4 + 1];
|
|
161
311
|
const oldOz = orientations[i4 + 2], oldOw = orientations[i4 + 3];
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
312
|
+
let tx = to[i4 + 0], ty = to[i4 + 1], tz = to[i4 + 2], tw = to[i4 + 3];
|
|
313
|
+
if (oldOx * tx + oldOy * ty + oldOz * tz + oldOw * tw < 0) {
|
|
314
|
+
tx = -tx;
|
|
315
|
+
ty = -ty;
|
|
316
|
+
tz = -tz;
|
|
317
|
+
tw = -tw;
|
|
318
|
+
}
|
|
319
|
+
let newOx = oldOx + (tx - oldOx) * f;
|
|
320
|
+
let newOy = oldOy + (ty - oldOy) * f;
|
|
321
|
+
let newOz = oldOz + (tz - oldOz) * f;
|
|
322
|
+
let newOw = oldOw + (tw - oldOw) * f;
|
|
323
|
+
const len2 = newOx * newOx + newOy * newOy + newOz * newOz + newOw * newOw;
|
|
324
|
+
if (len2 > 1e-12) {
|
|
325
|
+
const inv = 1 / Math.sqrt(len2);
|
|
326
|
+
newOx *= inv;
|
|
327
|
+
newOy *= inv;
|
|
328
|
+
newOz *= inv;
|
|
329
|
+
newOw *= inv;
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
newOx = tx;
|
|
333
|
+
newOy = ty;
|
|
334
|
+
newOz = tz;
|
|
335
|
+
newOw = tw;
|
|
336
|
+
}
|
|
168
337
|
orientations[i4 + 0] = newOx;
|
|
169
338
|
orientations[i4 + 1] = newOy;
|
|
170
339
|
orientations[i4 + 2] = newOz;
|
|
171
340
|
orientations[i4 + 3] = newOw;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
// Snap kinematic bodies straight to the frame target with zero velocity.
|
|
344
|
+
// Used on teleports (onlyFlagged) and when the substep budget runs out
|
|
345
|
+
// mid-catchup (all).
|
|
346
|
+
snapKinematicToTargets(onlyFlagged) {
|
|
347
|
+
const N = this.store.count;
|
|
348
|
+
const positions = this.store.positions;
|
|
349
|
+
const orientations = this.store.orientations;
|
|
350
|
+
const lv = this.store.linearVelocities;
|
|
351
|
+
const av = this.store.angularVelocities;
|
|
352
|
+
const types = this.store.type;
|
|
353
|
+
const boneIdx = this.store.boneIndex;
|
|
354
|
+
const tp = this.kinTargetPos;
|
|
355
|
+
const to = this.kinTargetOri;
|
|
356
|
+
const flags = this.teleportFlags;
|
|
357
|
+
for (let i = 0; i < N; i++) {
|
|
358
|
+
const t = types[i];
|
|
359
|
+
if (t !== RigidbodyType.Static && t !== RigidbodyType.Kinematic)
|
|
360
|
+
continue;
|
|
361
|
+
if (boneIdx[i] < 0)
|
|
362
|
+
continue;
|
|
363
|
+
if (onlyFlagged && !flags[i])
|
|
364
|
+
continue;
|
|
365
|
+
const i3 = i * 3;
|
|
366
|
+
const i4 = i * 4;
|
|
367
|
+
positions[i3 + 0] = tp[i3 + 0];
|
|
368
|
+
positions[i3 + 1] = tp[i3 + 1];
|
|
369
|
+
positions[i3 + 2] = tp[i3 + 2];
|
|
370
|
+
orientations[i4 + 0] = to[i4 + 0];
|
|
371
|
+
orientations[i4 + 1] = to[i4 + 1];
|
|
372
|
+
orientations[i4 + 2] = to[i4 + 2];
|
|
373
|
+
orientations[i4 + 3] = to[i4 + 3];
|
|
374
|
+
lv[i3 + 0] = 0;
|
|
375
|
+
lv[i3 + 1] = 0;
|
|
376
|
+
lv[i3 + 2] = 0;
|
|
377
|
+
av[i3 + 0] = 0;
|
|
378
|
+
av[i3 + 1] = 0;
|
|
379
|
+
av[i3 + 2] = 0;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
// Rigidly carry each dynamic body whose kinematic root teleported along
|
|
383
|
+
// with that root's current→target transform delta (velocity zeroed),
|
|
384
|
+
// preserving the cloth pose relative to the character across the jump.
|
|
385
|
+
// Must run before snapKinematicToTargets (it reads the pre-snap pose).
|
|
386
|
+
carryDynamicThroughTeleport() {
|
|
387
|
+
const N = this.store.count;
|
|
388
|
+
const positions = this.store.positions;
|
|
389
|
+
const orientations = this.store.orientations;
|
|
390
|
+
const lv = this.store.linearVelocities;
|
|
391
|
+
const av = this.store.angularVelocities;
|
|
392
|
+
const types = this.store.type;
|
|
393
|
+
const tp = this.kinTargetPos;
|
|
394
|
+
const to = this.kinTargetOri;
|
|
395
|
+
const root = this.kinRoot;
|
|
396
|
+
const flags = this.teleportFlags;
|
|
397
|
+
for (let i = 0; i < N; i++) {
|
|
398
|
+
if (types[i] !== RigidbodyType.Dynamic)
|
|
399
|
+
continue;
|
|
400
|
+
const k = root[i];
|
|
401
|
+
if (k < 0 || !flags[k] || this.store.boneIndex[k] < 0)
|
|
402
|
+
continue;
|
|
403
|
+
const k3 = k * 3;
|
|
404
|
+
const k4 = k * 4;
|
|
405
|
+
// Root delta rotation R = qTarget · conj(qCurrent).
|
|
406
|
+
const cx = -orientations[k4 + 0], cy = -orientations[k4 + 1], cz = -orientations[k4 + 2], cw = orientations[k4 + 3];
|
|
407
|
+
const txq = to[k4 + 0], tyq = to[k4 + 1], tzq = to[k4 + 2], twq = to[k4 + 3];
|
|
408
|
+
const rx = twq * cx + txq * cw + tyq * cz - tzq * cy;
|
|
409
|
+
const ry = twq * cy - txq * cz + tyq * cw + tzq * cx;
|
|
410
|
+
const rz = twq * cz + txq * cy - tyq * cx + tzq * cw;
|
|
411
|
+
const rw = twq * cw - txq * cx - tyq * cy - tzq * cz;
|
|
412
|
+
const i3 = i * 3;
|
|
413
|
+
const i4 = i * 4;
|
|
414
|
+
// Position: rotate the offset from the root by R, re-anchor at target.
|
|
415
|
+
const ox = positions[i3 + 0] - positions[k3 + 0];
|
|
416
|
+
const oy = positions[i3 + 1] - positions[k3 + 1];
|
|
417
|
+
const oz = positions[i3 + 2] - positions[k3 + 2];
|
|
418
|
+
// v' = v + 2·rw·(r × v) + 2·(r × (r × v))
|
|
419
|
+
const c1x = ry * oz - rz * oy;
|
|
420
|
+
const c1y = rz * ox - rx * oz;
|
|
421
|
+
const c1z = rx * oy - ry * ox;
|
|
422
|
+
const c2x = ry * c1z - rz * c1y;
|
|
423
|
+
const c2y = rz * c1x - rx * c1z;
|
|
424
|
+
const c2z = rx * c1y - ry * c1x;
|
|
425
|
+
positions[i3 + 0] = tp[k3 + 0] + ox + 2 * (rw * c1x + c2x);
|
|
426
|
+
positions[i3 + 1] = tp[k3 + 1] + oy + 2 * (rw * c1y + c2y);
|
|
427
|
+
positions[i3 + 2] = tp[k3 + 2] + oz + 2 * (rw * c1z + c2z);
|
|
428
|
+
// Orientation: q' = R · q, renormalized.
|
|
429
|
+
const qx = orientations[i4 + 0], qy = orientations[i4 + 1], qz = orientations[i4 + 2], qw = orientations[i4 + 3];
|
|
430
|
+
let nx = rw * qx + rx * qw + ry * qz - rz * qy;
|
|
431
|
+
let ny = rw * qy - rx * qz + ry * qw + rz * qx;
|
|
432
|
+
let nz = rw * qz + rx * qy - ry * qx + rz * qw;
|
|
433
|
+
let nw = rw * qw - rx * qx - ry * qy - rz * qz;
|
|
434
|
+
const len2 = nx * nx + ny * ny + nz * nz + nw * nw;
|
|
435
|
+
if (len2 > 1e-12) {
|
|
436
|
+
const inv = 1 / Math.sqrt(len2);
|
|
437
|
+
nx *= inv;
|
|
438
|
+
ny *= inv;
|
|
439
|
+
nz *= inv;
|
|
440
|
+
nw *= inv;
|
|
441
|
+
orientations[i4 + 0] = nx;
|
|
442
|
+
orientations[i4 + 1] = ny;
|
|
443
|
+
orientations[i4 + 2] = nz;
|
|
444
|
+
orientations[i4 + 3] = nw;
|
|
195
445
|
}
|
|
446
|
+
// Momentum doesn't carry across a discontinuity.
|
|
447
|
+
lv[i3 + 0] = 0;
|
|
448
|
+
lv[i3 + 1] = 0;
|
|
449
|
+
lv[i3 + 2] = 0;
|
|
450
|
+
av[i3 + 0] = 0;
|
|
451
|
+
av[i3 + 1] = 0;
|
|
452
|
+
av[i3 + 2] = 0;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
// Backstop: if a dynamic body's state went non-finite despite the velocity
|
|
456
|
+
// caps, restore its previous-substep pose with zero velocity instead of
|
|
457
|
+
// letting NaNs spread through constraints and contacts. Runs after every
|
|
458
|
+
// substep so prevState is always a finite pose.
|
|
459
|
+
restoreNonFiniteBodies() {
|
|
460
|
+
const N = this.store.count;
|
|
461
|
+
const positions = this.store.positions;
|
|
462
|
+
const orientations = this.store.orientations;
|
|
463
|
+
const lv = this.store.linearVelocities;
|
|
464
|
+
const av = this.store.angularVelocities;
|
|
465
|
+
const types = this.store.type;
|
|
466
|
+
const prevPos = this.prevPositions;
|
|
467
|
+
const prevOri = this.prevOrientations;
|
|
468
|
+
for (let i = 0; i < N; i++) {
|
|
469
|
+
if (types[i] !== RigidbodyType.Dynamic)
|
|
470
|
+
continue;
|
|
471
|
+
const i3 = i * 3;
|
|
472
|
+
const i4 = i * 4;
|
|
473
|
+
// NaN/Inf propagates through the sum, so one check covers all 13 slots.
|
|
474
|
+
const s = positions[i3 + 0] + positions[i3 + 1] + positions[i3 + 2] +
|
|
475
|
+
orientations[i4 + 0] + orientations[i4 + 1] + orientations[i4 + 2] + orientations[i4 + 3] +
|
|
476
|
+
lv[i3 + 0] + lv[i3 + 1] + lv[i3 + 2] +
|
|
477
|
+
av[i3 + 0] + av[i3 + 1] + av[i3 + 2];
|
|
478
|
+
if (Number.isFinite(s))
|
|
479
|
+
continue;
|
|
480
|
+
positions[i3 + 0] = prevPos[i3 + 0];
|
|
481
|
+
positions[i3 + 1] = prevPos[i3 + 1];
|
|
482
|
+
positions[i3 + 2] = prevPos[i3 + 2];
|
|
483
|
+
orientations[i4 + 0] = prevOri[i4 + 0];
|
|
484
|
+
orientations[i4 + 1] = prevOri[i4 + 1];
|
|
485
|
+
orientations[i4 + 2] = prevOri[i4 + 2];
|
|
486
|
+
orientations[i4 + 3] = prevOri[i4 + 3];
|
|
487
|
+
lv[i3 + 0] = 0;
|
|
488
|
+
lv[i3 + 1] = 0;
|
|
489
|
+
lv[i3 + 2] = 0;
|
|
490
|
+
av[i3 + 0] = 0;
|
|
491
|
+
av[i3 + 1] = 0;
|
|
492
|
+
av[i3 + 2] = 0;
|
|
196
493
|
}
|
|
197
494
|
}
|
|
198
495
|
// Dynamic bodies write their transform back to the bone matrix:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solver.d.ts","sourceRoot":"","sources":["../../src/physics/solver.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AAC5C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAE1D,OAAO,KAAK,EAAW,WAAW,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"solver.d.ts","sourceRoot":"","sources":["../../src/physics/solver.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAA;AAC5C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAE1D,OAAO,KAAK,EAAW,WAAW,EAAE,MAAM,WAAW,CAAA;AA6BrD,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,cAAc,EACrB,WAAW,EAAE,sBAAsB,EAAE,EACrC,QAAQ,EAAE,WAAW,EACrB,EAAE,EAAE,MAAM,EACV,UAAU,EAAE,MAAM,GACjB,IAAI,CAyBN"}
|