voxljs 0.1.0 → 0.1.1
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/dist/audio/SpatialAudioEngine.d.ts +10 -0
- package/dist/audio/SpatialAudioEngine.js +80 -0
- package/dist/audio/SpatialAudioEngine.js.map +1 -1
- package/dist/core/VoxelEngine.d.ts +99 -0
- package/dist/core/VoxelEngine.js +181 -0
- package/dist/core/VoxelEngine.js.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/input/InputManager.d.ts +45 -6
- package/dist/input/InputManager.js +178 -31
- package/dist/input/InputManager.js.map +1 -1
- package/dist/input/index.d.ts +1 -1
- package/dist/input/index.js +1 -1
- package/dist/input/index.js.map +1 -1
- package/dist/materials/Material.d.ts +6 -0
- package/dist/materials/Material.js +12 -0
- package/dist/materials/Material.js.map +1 -1
- package/dist/perf/EntityTickScheduler.d.ts +71 -0
- package/dist/perf/EntityTickScheduler.js +202 -0
- package/dist/perf/EntityTickScheduler.js.map +1 -0
- package/dist/perf/SpatialEntityGrid.d.ts +47 -0
- package/dist/perf/SpatialEntityGrid.js +200 -0
- package/dist/perf/SpatialEntityGrid.js.map +1 -0
- package/dist/perf/index.d.ts +2 -0
- package/dist/perf/index.js +2 -0
- package/dist/perf/index.js.map +1 -1
- package/dist/physics/VoxelPhysicsWorld.d.ts +308 -0
- package/dist/physics/VoxelPhysicsWorld.js +1020 -0
- package/dist/physics/VoxelPhysicsWorld.js.map +1 -0
- package/dist/physics/index.d.ts +1 -0
- package/dist/physics/index.js +1 -0
- package/dist/physics/index.js.map +1 -1
- package/dist/renderers/WebGLRenderer.js +1 -0
- package/dist/renderers/WebGLRenderer.js.map +1 -1
- package/dist/renderers/webgl/WebGLProgram.js +18 -19
- package/dist/renderers/webgl/WebGLProgram.js.map +1 -1
- package/dist/renderers/webgl/WebGLState.d.ts +17 -0
- package/dist/renderers/webgl/WebGLState.js +115 -1
- package/dist/renderers/webgl/WebGLState.js.map +1 -1
- package/dist/voxel/GrassFieldSystem.d.ts +186 -0
- package/dist/voxel/GrassFieldSystem.js +723 -0
- package/dist/voxel/GrassFieldSystem.js.map +1 -0
- package/dist/voxel/SilhouetteLightVolumes.d.ts +13 -2
- package/dist/voxel/SilhouetteLightVolumes.js +384 -78
- package/dist/voxel/SilhouetteLightVolumes.js.map +1 -1
- package/dist/voxel/VoxImporter.d.ts +32 -0
- package/dist/voxel/VoxImporter.js +228 -0
- package/dist/voxel/VoxImporter.js.map +1 -1
- package/dist/voxel/VoxelBrushes.d.ts +110 -0
- package/dist/voxel/VoxelBrushes.js +248 -0
- package/dist/voxel/VoxelBrushes.js.map +1 -0
- package/dist/voxel/VoxelPotatoShader.d.ts +8 -2
- package/dist/voxel/VoxelPotatoShader.js +68 -4
- package/dist/voxel/VoxelPotatoShader.js.map +1 -1
- package/dist/voxel/VoxelSilhouetteShadows.d.ts +69 -11
- package/dist/voxel/VoxelSilhouetteShadows.js +653 -32
- package/dist/voxel/VoxelSilhouetteShadows.js.map +1 -1
- package/dist/voxel/index.d.ts +4 -2
- package/dist/voxel/index.js +4 -2
- package/dist/voxel/index.js.map +1 -1
- package/dist/world/VoxelMobAISystem.d.ts +183 -0
- package/dist/world/VoxelMobAISystem.js +492 -0
- package/dist/world/VoxelMobAISystem.js.map +1 -0
- package/dist/world/index.d.ts +1 -0
- package/dist/world/index.js +1 -0
- package/dist/world/index.js.map +1 -1
- package/llms.txt +301 -0
- package/package.json +2 -1
|
@@ -0,0 +1,1020 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency Voxel Rigid-Body & Multi-Part Constraints Physics World for voxljs.
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Arbitrary voxel-scale terrain collisions (`voxelSize`: 1m, 50cm, 25cm, 10cm, 5cm, etc.)
|
|
6
|
+
* for dynamic/static box & sphere rigid bodies with internal-seam skipping and anti-tunneling.
|
|
7
|
+
* - Amanatides-Woo voxel terrain raycasts (`raycastTerrain`) for wheels, projectiles, and sensors.
|
|
8
|
+
* - Multi-part rigid-body constraints & joints (`addJoint`):
|
|
9
|
+
* - `'spring'` / `'distance'` (elastic or taut connectors between bodies or world anchors)
|
|
10
|
+
* - `'hinge'` (pin joints with optional angle limits and motorized angular drive)
|
|
11
|
+
* - `'wheel'` (raycast suspension, steering, drive/brake forces, lateral tire grip)
|
|
12
|
+
* - `'thruster'` (body-mounted directional force + off-center torque)
|
|
13
|
+
* - `'fixed'` (breakable rigid weld between two bodies)
|
|
14
|
+
*/
|
|
15
|
+
import { Quaternion } from '../math/Quaternion.js';
|
|
16
|
+
import { Vector3 } from '../math/Vector3.js';
|
|
17
|
+
const _v0 = new Vector3();
|
|
18
|
+
const _v1 = new Vector3();
|
|
19
|
+
const _v2 = new Vector3();
|
|
20
|
+
const _v3 = new Vector3();
|
|
21
|
+
const _v4 = new Vector3();
|
|
22
|
+
const _v5 = new Vector3();
|
|
23
|
+
const _v6 = new Vector3();
|
|
24
|
+
const _q0 = new Quaternion();
|
|
25
|
+
const _q1 = new Quaternion();
|
|
26
|
+
const _q2 = new Quaternion();
|
|
27
|
+
function computeScalarInertia(shape, mass, halfExtents, radius) {
|
|
28
|
+
if (mass <= 0)
|
|
29
|
+
return 0;
|
|
30
|
+
if (shape === 'sphere') {
|
|
31
|
+
return 0.4 * mass * radius * radius;
|
|
32
|
+
}
|
|
33
|
+
const sx = halfExtents.x * 2;
|
|
34
|
+
const sy = halfExtents.y * 2;
|
|
35
|
+
const sz = halfExtents.z * 2;
|
|
36
|
+
return (mass * (sx * sx + sy * sy + sz * sz)) / 18;
|
|
37
|
+
}
|
|
38
|
+
function getWorldPointVelocity(body, worldPoint, out) {
|
|
39
|
+
_v6.subVectors(worldPoint, body.position);
|
|
40
|
+
out.crossVectors(body.angularVelocity, _v6).add(body.linearVelocity);
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
function applyPointImpulse(body, impulse, worldPoint) {
|
|
44
|
+
if (body.isStatic || body.invMass === 0)
|
|
45
|
+
return;
|
|
46
|
+
body.linearVelocity.addScaledVector(impulse, body.invMass);
|
|
47
|
+
if (body.invInertia > 0) {
|
|
48
|
+
_v6.subVectors(worldPoint, body.position);
|
|
49
|
+
_v5.crossVectors(_v6, impulse);
|
|
50
|
+
body.angularVelocity.addScaledVector(_v5, body.invInertia);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export class VoxelPhysicsWorld {
|
|
54
|
+
constructor(options = {}) {
|
|
55
|
+
this._bodies = new Map();
|
|
56
|
+
this._joints = new Map();
|
|
57
|
+
this._nextBodyId = 1;
|
|
58
|
+
this._nextJointId = 1;
|
|
59
|
+
this._getVoxel = options.getVoxel ?? (() => 0);
|
|
60
|
+
this._isSolid = options.isSolid ?? ((id) => id > 0);
|
|
61
|
+
this.voxelSize = Math.max(1e-4, options.voxelSize ?? 1.0);
|
|
62
|
+
this.gravity = new Vector3(options.gravity?.x ?? 0, options.gravity?.y ?? -9.81, options.gravity?.z ?? 0);
|
|
63
|
+
this.solverIterations = Math.max(1, Math.floor(options.solverIterations ?? 6));
|
|
64
|
+
this.impactSpeedThreshold = Math.max(0, options.impactSpeedThreshold ?? 1.2);
|
|
65
|
+
this.onImpact = options.onImpact ?? null;
|
|
66
|
+
}
|
|
67
|
+
setTerrainSampler(getVoxel, isSolid) {
|
|
68
|
+
this._getVoxel = getVoxel;
|
|
69
|
+
if (isSolid)
|
|
70
|
+
this._isSolid = isSolid;
|
|
71
|
+
}
|
|
72
|
+
get bodyCount() {
|
|
73
|
+
return this._bodies.size;
|
|
74
|
+
}
|
|
75
|
+
get jointCount() {
|
|
76
|
+
return this._joints.size;
|
|
77
|
+
}
|
|
78
|
+
get bodies() {
|
|
79
|
+
return this._bodies.values();
|
|
80
|
+
}
|
|
81
|
+
get joints() {
|
|
82
|
+
return this._joints.values();
|
|
83
|
+
}
|
|
84
|
+
addBody(options = {}) {
|
|
85
|
+
const id = options.id ?? `body_${this._nextBodyId++}`;
|
|
86
|
+
const shape = options.shape ?? 'box';
|
|
87
|
+
const halfExtents = new Vector3(Math.max(1e-4, options.halfExtents?.x ?? 0.5), Math.max(1e-4, options.halfExtents?.y ?? 0.5), Math.max(1e-4, options.halfExtents?.z ?? 0.5));
|
|
88
|
+
const radius = Math.max(1e-4, options.radius ?? Math.max(halfExtents.x, halfExtents.y, halfExtents.z));
|
|
89
|
+
const isStatic = Boolean(options.isStatic) || (options.mass !== undefined && options.mass <= 0);
|
|
90
|
+
const mass = isStatic ? 0 : Math.max(1e-4, options.mass ?? 1.0);
|
|
91
|
+
const inertia = computeScalarInertia(shape, mass, halfExtents, radius);
|
|
92
|
+
const body = {
|
|
93
|
+
id,
|
|
94
|
+
shape,
|
|
95
|
+
position: new Vector3(options.position?.x ?? 0, options.position?.y ?? 0, options.position?.z ?? 0),
|
|
96
|
+
quaternion: new Quaternion(options.quaternion?.x ?? 0, options.quaternion?.y ?? 0, options.quaternion?.z ?? 0, options.quaternion?.w ?? 1).normalize(),
|
|
97
|
+
linearVelocity: new Vector3(options.linearVelocity?.x ?? 0, options.linearVelocity?.y ?? 0, options.linearVelocity?.z ?? 0),
|
|
98
|
+
angularVelocity: new Vector3(options.angularVelocity?.x ?? 0, options.angularVelocity?.y ?? 0, options.angularVelocity?.z ?? 0),
|
|
99
|
+
force: new Vector3(),
|
|
100
|
+
torque: new Vector3(),
|
|
101
|
+
halfExtents,
|
|
102
|
+
radius,
|
|
103
|
+
mass,
|
|
104
|
+
invMass: isStatic ? 0 : 1 / mass,
|
|
105
|
+
invInertia: isStatic || inertia <= 0 ? 0 : 1 / inertia,
|
|
106
|
+
restitution: Math.max(0, Math.min(1, options.restitution ?? 0.15)),
|
|
107
|
+
friction: Math.max(0, Math.min(2, options.friction ?? 0.6)),
|
|
108
|
+
linearDamping: Math.max(0, options.linearDamping ?? 0.04),
|
|
109
|
+
angularDamping: Math.max(0, options.angularDamping ?? 0.12),
|
|
110
|
+
gravityScale: options.gravityScale ?? 1,
|
|
111
|
+
isStatic,
|
|
112
|
+
grounded: false,
|
|
113
|
+
contactNormal: new Vector3(0, 1, 0),
|
|
114
|
+
userData: options.userData ? { ...options.userData } : {},
|
|
115
|
+
};
|
|
116
|
+
this._bodies.set(id, body);
|
|
117
|
+
return body;
|
|
118
|
+
}
|
|
119
|
+
getBody(id) {
|
|
120
|
+
return this._bodies.get(id);
|
|
121
|
+
}
|
|
122
|
+
removeBody(id) {
|
|
123
|
+
const removed = this._bodies.delete(id);
|
|
124
|
+
if (removed) {
|
|
125
|
+
for (const [jointId, joint] of this._joints) {
|
|
126
|
+
if (joint.bodyA === id ||
|
|
127
|
+
('bodyB' in joint && joint.bodyB === id)) {
|
|
128
|
+
this._joints.delete(jointId);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return removed;
|
|
133
|
+
}
|
|
134
|
+
applyForce(bodyId, force, worldPoint) {
|
|
135
|
+
const body = this._bodies.get(bodyId);
|
|
136
|
+
if (!body || body.isStatic)
|
|
137
|
+
return;
|
|
138
|
+
body.force.x += force.x;
|
|
139
|
+
body.force.y += force.y;
|
|
140
|
+
body.force.z += force.z;
|
|
141
|
+
if (worldPoint) {
|
|
142
|
+
const rx = worldPoint.x - body.position.x;
|
|
143
|
+
const ry = worldPoint.y - body.position.y;
|
|
144
|
+
const rz = worldPoint.z - body.position.z;
|
|
145
|
+
body.torque.x += ry * force.z - rz * force.y;
|
|
146
|
+
body.torque.y += rz * force.x - rx * force.z;
|
|
147
|
+
body.torque.z += rx * force.y - ry * force.x;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
applyTorque(bodyId, torque) {
|
|
151
|
+
const body = this._bodies.get(bodyId);
|
|
152
|
+
if (!body || body.isStatic)
|
|
153
|
+
return;
|
|
154
|
+
body.torque.x += torque.x;
|
|
155
|
+
body.torque.y += torque.y;
|
|
156
|
+
body.torque.z += torque.z;
|
|
157
|
+
}
|
|
158
|
+
applyImpulse(bodyId, impulse, worldPoint) {
|
|
159
|
+
const body = this._bodies.get(bodyId);
|
|
160
|
+
if (!body || body.isStatic)
|
|
161
|
+
return;
|
|
162
|
+
_v0.set(impulse.x, impulse.y, impulse.z);
|
|
163
|
+
if (worldPoint) {
|
|
164
|
+
_v1.set(worldPoint.x, worldPoint.y, worldPoint.z);
|
|
165
|
+
applyPointImpulse(body, _v0, _v1);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
body.linearVelocity.addScaledVector(_v0, body.invMass);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
addJoint(options) {
|
|
172
|
+
const id = options.id ?? `joint_${this._nextJointId++}`;
|
|
173
|
+
const bodyA = this._bodies.get(options.bodyA);
|
|
174
|
+
if (!bodyA) {
|
|
175
|
+
throw new Error(`VoxelPhysicsWorld.addJoint: unknown bodyA "${options.bodyA}"`);
|
|
176
|
+
}
|
|
177
|
+
if (options.type === 'spring' || options.type === 'distance') {
|
|
178
|
+
const bodyB = options.bodyB ? this._bodies.get(options.bodyB) : undefined;
|
|
179
|
+
if (options.bodyB && !bodyB) {
|
|
180
|
+
throw new Error(`VoxelPhysicsWorld.addJoint: unknown bodyB "${options.bodyB}"`);
|
|
181
|
+
}
|
|
182
|
+
const localAnchorA = new Vector3(options.localAnchorA?.x ?? 0, options.localAnchorA?.y ?? 0, options.localAnchorA?.z ?? 0);
|
|
183
|
+
const localAnchorB = new Vector3(options.localAnchorB?.x ?? 0, options.localAnchorB?.y ?? 0, options.localAnchorB?.z ?? 0);
|
|
184
|
+
const worldAnchorB = options.worldAnchorB
|
|
185
|
+
? new Vector3(options.worldAnchorB.x, options.worldAnchorB.y, options.worldAnchorB.z)
|
|
186
|
+
: null;
|
|
187
|
+
let restLength = options.restLength;
|
|
188
|
+
if (restLength === undefined) {
|
|
189
|
+
const wa = localAnchorA.clone().applyQuaternion(bodyA.quaternion).add(bodyA.position);
|
|
190
|
+
const wb = bodyB
|
|
191
|
+
? localAnchorB.clone().applyQuaternion(bodyB.quaternion).add(bodyB.position)
|
|
192
|
+
: (worldAnchorB ? worldAnchorB.clone() : wa.clone());
|
|
193
|
+
restLength = wa.distanceTo(wb);
|
|
194
|
+
}
|
|
195
|
+
const joint = {
|
|
196
|
+
id,
|
|
197
|
+
type: options.type,
|
|
198
|
+
bodyA: options.bodyA,
|
|
199
|
+
bodyB: options.bodyB ?? null,
|
|
200
|
+
localAnchorA,
|
|
201
|
+
localAnchorB,
|
|
202
|
+
worldAnchorB,
|
|
203
|
+
restLength: Math.max(0, restLength),
|
|
204
|
+
stiffness: options.stiffness ?? (options.type === 'distance' ? 240 : 80),
|
|
205
|
+
damping: options.damping ?? 8,
|
|
206
|
+
maxForce: options.maxForce ?? Infinity,
|
|
207
|
+
breakForce: options.breakForce ?? Infinity,
|
|
208
|
+
enabled: options.enabled ?? true,
|
|
209
|
+
broken: false,
|
|
210
|
+
currentLength: Math.max(0, restLength),
|
|
211
|
+
lastForce: 0,
|
|
212
|
+
};
|
|
213
|
+
this._joints.set(id, joint);
|
|
214
|
+
return joint;
|
|
215
|
+
}
|
|
216
|
+
if (options.type === 'hinge') {
|
|
217
|
+
const bodyB = this._bodies.get(options.bodyB);
|
|
218
|
+
if (!bodyB) {
|
|
219
|
+
throw new Error(`VoxelPhysicsWorld.addJoint: unknown bodyB "${options.bodyB}"`);
|
|
220
|
+
}
|
|
221
|
+
const axis = new Vector3(options.axis?.x ?? 0, options.axis?.y ?? 1, options.axis?.z ?? 0);
|
|
222
|
+
if (axis.lengthSq() < 1e-8)
|
|
223
|
+
axis.set(0, 1, 0);
|
|
224
|
+
axis.normalize();
|
|
225
|
+
const joint = {
|
|
226
|
+
id,
|
|
227
|
+
type: 'hinge',
|
|
228
|
+
bodyA: options.bodyA,
|
|
229
|
+
bodyB: options.bodyB,
|
|
230
|
+
localAnchorA: new Vector3(options.localAnchorA?.x ?? 0, options.localAnchorA?.y ?? 0, options.localAnchorA?.z ?? 0),
|
|
231
|
+
localAnchorB: new Vector3(options.localAnchorB?.x ?? 0, options.localAnchorB?.y ?? 0, options.localAnchorB?.z ?? 0),
|
|
232
|
+
axis,
|
|
233
|
+
minAngle: options.minAngle ?? -Infinity,
|
|
234
|
+
maxAngle: options.maxAngle ?? Infinity,
|
|
235
|
+
motorSpeed: options.motorSpeed ?? 0,
|
|
236
|
+
maxMotorTorque: options.maxMotorTorque ?? 40,
|
|
237
|
+
breakForce: options.breakForce ?? Infinity,
|
|
238
|
+
enabled: options.enabled ?? true,
|
|
239
|
+
broken: false,
|
|
240
|
+
currentAngle: 0,
|
|
241
|
+
lastForce: 0,
|
|
242
|
+
};
|
|
243
|
+
this._joints.set(id, joint);
|
|
244
|
+
return joint;
|
|
245
|
+
}
|
|
246
|
+
if (options.type === 'wheel') {
|
|
247
|
+
const suspDir = new Vector3(options.suspensionDirection?.x ?? 0, options.suspensionDirection?.y ?? -1, options.suspensionDirection?.z ?? 0);
|
|
248
|
+
if (suspDir.lengthSq() < 1e-8)
|
|
249
|
+
suspDir.set(0, -1, 0);
|
|
250
|
+
suspDir.normalize();
|
|
251
|
+
const axle = new Vector3(options.axleAxis?.x ?? 1, options.axleAxis?.y ?? 0, options.axleAxis?.z ?? 0);
|
|
252
|
+
if (axle.lengthSq() < 1e-8)
|
|
253
|
+
axle.set(1, 0, 0);
|
|
254
|
+
axle.normalize();
|
|
255
|
+
const fwd = new Vector3(options.forwardAxis?.x ?? 0, options.forwardAxis?.y ?? 0, options.forwardAxis?.z ?? -1);
|
|
256
|
+
if (fwd.lengthSq() < 1e-8)
|
|
257
|
+
fwd.set(0, 0, -1);
|
|
258
|
+
fwd.normalize();
|
|
259
|
+
const restLen = Math.max(0.02, options.suspensionRestLength ?? 0.5);
|
|
260
|
+
const joint = {
|
|
261
|
+
id,
|
|
262
|
+
type: 'wheel',
|
|
263
|
+
bodyA: options.bodyA,
|
|
264
|
+
chassisAnchor: new Vector3(options.chassisAnchor.x, options.chassisAnchor.y, options.chassisAnchor.z),
|
|
265
|
+
suspensionDirection: suspDir,
|
|
266
|
+
axleAxis: axle,
|
|
267
|
+
forwardAxis: fwd,
|
|
268
|
+
suspensionRestLength: restLen,
|
|
269
|
+
wheelRadius: Math.max(0.02, options.wheelRadius ?? 0.35),
|
|
270
|
+
stiffness: options.stiffness ?? 60,
|
|
271
|
+
damping: options.damping ?? 8,
|
|
272
|
+
steerAngle: options.steerAngle ?? 0,
|
|
273
|
+
driveForce: options.driveForce ?? 0,
|
|
274
|
+
brakeForce: options.brakeForce ?? 0,
|
|
275
|
+
lateralGrip: options.lateralGrip ?? 10,
|
|
276
|
+
maxSuspensionForce: options.maxSuspensionForce ?? 400,
|
|
277
|
+
enabled: options.enabled ?? true,
|
|
278
|
+
broken: false,
|
|
279
|
+
grounded: false,
|
|
280
|
+
compression: 0,
|
|
281
|
+
suspensionLength: restLen,
|
|
282
|
+
contactPoint: new Vector3(),
|
|
283
|
+
contactNormal: new Vector3(0, 1, 0),
|
|
284
|
+
wheelWorldPosition: new Vector3(),
|
|
285
|
+
spinAngle: 0,
|
|
286
|
+
lastForce: 0,
|
|
287
|
+
};
|
|
288
|
+
this._joints.set(id, joint);
|
|
289
|
+
return joint;
|
|
290
|
+
}
|
|
291
|
+
if (options.type === 'thruster') {
|
|
292
|
+
const dir = new Vector3(options.localDirection?.x ?? 0, options.localDirection?.y ?? 1, options.localDirection?.z ?? 0);
|
|
293
|
+
if (dir.lengthSq() < 1e-8)
|
|
294
|
+
dir.set(0, 1, 0);
|
|
295
|
+
dir.normalize();
|
|
296
|
+
const joint = {
|
|
297
|
+
id,
|
|
298
|
+
type: 'thruster',
|
|
299
|
+
bodyA: options.bodyA,
|
|
300
|
+
localOffset: new Vector3(options.localOffset?.x ?? 0, options.localOffset?.y ?? 0, options.localOffset?.z ?? 0),
|
|
301
|
+
localDirection: dir,
|
|
302
|
+
thrust: options.thrust,
|
|
303
|
+
active: options.active ?? true,
|
|
304
|
+
enabled: options.enabled ?? true,
|
|
305
|
+
broken: false,
|
|
306
|
+
lastForce: 0,
|
|
307
|
+
};
|
|
308
|
+
this._joints.set(id, joint);
|
|
309
|
+
return joint;
|
|
310
|
+
}
|
|
311
|
+
if (options.type !== 'fixed') {
|
|
312
|
+
throw new Error(`VoxelPhysicsWorld.addJoint: unsupported joint type.`);
|
|
313
|
+
}
|
|
314
|
+
// Fixed weld joint
|
|
315
|
+
const bodyB = this._bodies.get(options.bodyB);
|
|
316
|
+
if (!bodyB) {
|
|
317
|
+
throw new Error(`VoxelPhysicsWorld.addJoint: unknown bodyB "${options.bodyB}"`);
|
|
318
|
+
}
|
|
319
|
+
const invQA = bodyA.quaternion.clone().invert();
|
|
320
|
+
const localOffset = options.localOffset
|
|
321
|
+
? new Vector3(options.localOffset.x, options.localOffset.y, options.localOffset.z)
|
|
322
|
+
: bodyB.position.clone().sub(bodyA.position).applyQuaternion(invQA);
|
|
323
|
+
const localRotation = options.localRotation
|
|
324
|
+
? new Quaternion(options.localRotation.x, options.localRotation.y, options.localRotation.z, options.localRotation.w).normalize()
|
|
325
|
+
: invQA.clone().multiply(bodyB.quaternion).normalize();
|
|
326
|
+
const joint = {
|
|
327
|
+
id,
|
|
328
|
+
type: 'fixed',
|
|
329
|
+
bodyA: options.bodyA,
|
|
330
|
+
bodyB: options.bodyB,
|
|
331
|
+
localOffset,
|
|
332
|
+
localRotation,
|
|
333
|
+
stiffness: options.stiffness ?? 250,
|
|
334
|
+
damping: options.damping ?? 18,
|
|
335
|
+
breakForce: options.breakForce ?? Infinity,
|
|
336
|
+
enabled: options.enabled ?? true,
|
|
337
|
+
broken: false,
|
|
338
|
+
lastForce: 0,
|
|
339
|
+
};
|
|
340
|
+
this._joints.set(id, joint);
|
|
341
|
+
return joint;
|
|
342
|
+
}
|
|
343
|
+
getJoint(id) {
|
|
344
|
+
return this._joints.get(id);
|
|
345
|
+
}
|
|
346
|
+
removeJoint(id) {
|
|
347
|
+
return this._joints.delete(id);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Casts a ray against the voxel grid using Amanatides-Woo 3D DDA, scaled by `this.voxelSize`.
|
|
351
|
+
*/
|
|
352
|
+
raycastTerrain(origin, direction, maxDistance) {
|
|
353
|
+
const dirLen = Math.hypot(direction.x, direction.y, direction.z);
|
|
354
|
+
if (dirLen < 1e-8 || maxDistance <= 0)
|
|
355
|
+
return null;
|
|
356
|
+
const dx = direction.x / dirLen;
|
|
357
|
+
const dy = direction.y / dirLen;
|
|
358
|
+
const dz = direction.z / dirLen;
|
|
359
|
+
const invScale = 1 / this.voxelSize;
|
|
360
|
+
const ox = origin.x * invScale;
|
|
361
|
+
const oy = origin.y * invScale;
|
|
362
|
+
const oz = origin.z * invScale;
|
|
363
|
+
const maxVoxelDist = maxDistance * invScale;
|
|
364
|
+
let vx = Math.floor(ox);
|
|
365
|
+
let vy = Math.floor(oy);
|
|
366
|
+
let vz = Math.floor(oz);
|
|
367
|
+
const startBlock = this._getVoxel(vx, vy, vz);
|
|
368
|
+
if (this._isSolid(startBlock)) {
|
|
369
|
+
return {
|
|
370
|
+
distance: 0,
|
|
371
|
+
point: new Vector3(origin.x, origin.y, origin.z),
|
|
372
|
+
normal: new Vector3(-dx, -dy, -dz).normalize(),
|
|
373
|
+
voxel: [vx, vy, vz],
|
|
374
|
+
blockId: startBlock,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
const stepX = dx >= 0 ? 1 : -1;
|
|
378
|
+
const stepY = dy >= 0 ? 1 : -1;
|
|
379
|
+
const stepZ = dz >= 0 ? 1 : -1;
|
|
380
|
+
const tDeltaX = Math.abs(dx) > 1e-8 ? Math.abs(1 / dx) : Infinity;
|
|
381
|
+
const tDeltaY = Math.abs(dy) > 1e-8 ? Math.abs(1 / dy) : Infinity;
|
|
382
|
+
const tDeltaZ = Math.abs(dz) > 1e-8 ? Math.abs(1 / dz) : Infinity;
|
|
383
|
+
let tMaxX = Math.abs(dx) > 1e-8
|
|
384
|
+
? ((dx > 0 ? vx + 1 - ox : ox - vx) / Math.abs(dx))
|
|
385
|
+
: Infinity;
|
|
386
|
+
let tMaxY = Math.abs(dy) > 1e-8
|
|
387
|
+
? ((dy > 0 ? vy + 1 - oy : oy - vy) / Math.abs(dy))
|
|
388
|
+
: Infinity;
|
|
389
|
+
let tMaxZ = Math.abs(dz) > 1e-8
|
|
390
|
+
? ((dz > 0 ? vz + 1 - oz : oz - vz) / Math.abs(dz))
|
|
391
|
+
: Infinity;
|
|
392
|
+
let dist = 0;
|
|
393
|
+
let nx = 0;
|
|
394
|
+
let ny = 0;
|
|
395
|
+
let nz = 0;
|
|
396
|
+
const maxSteps = Math.min(2048, Math.ceil(maxVoxelDist * 3) + 4);
|
|
397
|
+
for (let step = 0; step < maxSteps; step++) {
|
|
398
|
+
if (tMaxX < tMaxY && tMaxX < tMaxZ) {
|
|
399
|
+
dist = tMaxX;
|
|
400
|
+
tMaxX += tDeltaX;
|
|
401
|
+
vx += stepX;
|
|
402
|
+
nx = -stepX;
|
|
403
|
+
ny = 0;
|
|
404
|
+
nz = 0;
|
|
405
|
+
}
|
|
406
|
+
else if (tMaxY < tMaxZ) {
|
|
407
|
+
dist = tMaxY;
|
|
408
|
+
tMaxY += tDeltaY;
|
|
409
|
+
vy += stepY;
|
|
410
|
+
nx = 0;
|
|
411
|
+
ny = -stepY;
|
|
412
|
+
nz = 0;
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
dist = tMaxZ;
|
|
416
|
+
tMaxZ += tDeltaZ;
|
|
417
|
+
vz += stepZ;
|
|
418
|
+
nx = 0;
|
|
419
|
+
ny = 0;
|
|
420
|
+
nz = -stepZ;
|
|
421
|
+
}
|
|
422
|
+
if (dist > maxVoxelDist)
|
|
423
|
+
return null;
|
|
424
|
+
const blockId = this._getVoxel(vx, vy, vz);
|
|
425
|
+
if (this._isSolid(blockId)) {
|
|
426
|
+
const worldDist = dist * this.voxelSize;
|
|
427
|
+
return {
|
|
428
|
+
distance: worldDist,
|
|
429
|
+
point: new Vector3(origin.x + dx * worldDist, origin.y + dy * worldDist, origin.z + dz * worldDist),
|
|
430
|
+
normal: new Vector3(nx, ny, nz),
|
|
431
|
+
voxel: [vx, vy, vz],
|
|
432
|
+
blockId,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Advances rigid bodies, forces, multi-part joints, and voxel collisions by `dt` seconds.
|
|
440
|
+
*/
|
|
441
|
+
step(dt) {
|
|
442
|
+
if (!Number.isFinite(dt) || dt <= 0)
|
|
443
|
+
return;
|
|
444
|
+
// 1. Apply force-based joints (thrusters, wheels, spring forces)
|
|
445
|
+
for (const joint of this._joints.values()) {
|
|
446
|
+
if (!joint.enabled || joint.broken)
|
|
447
|
+
continue;
|
|
448
|
+
if (joint.type === 'thruster') {
|
|
449
|
+
this._applyThrusterForce(joint);
|
|
450
|
+
}
|
|
451
|
+
else if (joint.type === 'wheel') {
|
|
452
|
+
this._applyWheelForces(joint, dt);
|
|
453
|
+
}
|
|
454
|
+
else if (joint.type === 'spring' || joint.type === 'distance') {
|
|
455
|
+
this._applySpringForce(joint);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
// 2. Integrate velocities & positions with anti-tunneling substeps
|
|
459
|
+
for (const body of this._bodies.values()) {
|
|
460
|
+
body.grounded = false;
|
|
461
|
+
if (body.isStatic) {
|
|
462
|
+
body.force.set(0, 0, 0);
|
|
463
|
+
body.torque.set(0, 0, 0);
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
// Gravity + accumulated forces
|
|
467
|
+
body.linearVelocity.x += (this.gravity.x * body.gravityScale + body.force.x * body.invMass) * dt;
|
|
468
|
+
body.linearVelocity.y += (this.gravity.y * body.gravityScale + body.force.y * body.invMass) * dt;
|
|
469
|
+
body.linearVelocity.z += (this.gravity.z * body.gravityScale + body.force.z * body.invMass) * dt;
|
|
470
|
+
if (body.invInertia > 0) {
|
|
471
|
+
body.angularVelocity.x += body.torque.x * body.invInertia * dt;
|
|
472
|
+
body.angularVelocity.y += body.torque.y * body.invInertia * dt;
|
|
473
|
+
body.angularVelocity.z += body.torque.z * body.invInertia * dt;
|
|
474
|
+
}
|
|
475
|
+
const linDamp = Math.max(0, 1 - body.linearDamping * dt);
|
|
476
|
+
const angDamp = Math.max(0, 1 - body.angularDamping * dt);
|
|
477
|
+
body.linearVelocity.multiplyScalar(linDamp);
|
|
478
|
+
body.angularVelocity.multiplyScalar(angDamp);
|
|
479
|
+
body.force.set(0, 0, 0);
|
|
480
|
+
body.torque.set(0, 0, 0);
|
|
481
|
+
// Integrate orientation
|
|
482
|
+
const omegaMag = body.angularVelocity.length();
|
|
483
|
+
if (omegaMag > 1e-6) {
|
|
484
|
+
_v0.copy(body.angularVelocity).divideScalar(omegaMag);
|
|
485
|
+
_q0.setFromAxisAngle(_v0, omegaMag * dt);
|
|
486
|
+
body.quaternion.premultiply(_q0).normalize();
|
|
487
|
+
}
|
|
488
|
+
// Substep linear translation when moving fast relative to voxelSize/body size
|
|
489
|
+
const speed = body.linearVelocity.length();
|
|
490
|
+
const minExtent = body.shape === 'sphere'
|
|
491
|
+
? body.radius
|
|
492
|
+
: Math.min(body.halfExtents.x, body.halfExtents.y, body.halfExtents.z);
|
|
493
|
+
const maxSafeStep = Math.max(0.01, Math.min(this.voxelSize, minExtent) * 0.45);
|
|
494
|
+
const moveDist = speed * dt;
|
|
495
|
+
const substeps = Math.min(8, Math.max(1, Math.ceil(moveDist / maxSafeStep)));
|
|
496
|
+
const subDt = dt / substeps;
|
|
497
|
+
for (let s = 0; s < substeps; s++) {
|
|
498
|
+
body.position.addScaledVector(body.linearVelocity, subDt);
|
|
499
|
+
this._resolveBodyVoxelCollisions(body);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// 3. Iterative positional & velocity constraints (distance, hinge, fixed welds)
|
|
503
|
+
for (let iter = 0; iter < this.solverIterations; iter++) {
|
|
504
|
+
const isLastIter = iter === this.solverIterations - 1;
|
|
505
|
+
for (const joint of this._joints.values()) {
|
|
506
|
+
if (!joint.enabled || joint.broken)
|
|
507
|
+
continue;
|
|
508
|
+
if (joint.type === 'distance') {
|
|
509
|
+
this._solveDistanceConstraint(joint, dt, isLastIter);
|
|
510
|
+
}
|
|
511
|
+
else if (joint.type === 'hinge') {
|
|
512
|
+
this._solveHingeConstraint(joint, dt, isLastIter);
|
|
513
|
+
}
|
|
514
|
+
else if (joint.type === 'fixed') {
|
|
515
|
+
this._solveFixedConstraint(joint, dt, isLastIter);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
// 4. Final voxel containment pass for bodies moved by constraints
|
|
520
|
+
for (const body of this._bodies.values()) {
|
|
521
|
+
if (!body.isStatic) {
|
|
522
|
+
this._resolveBodyVoxelCollisions(body);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
clear() {
|
|
527
|
+
this._joints.clear();
|
|
528
|
+
this._bodies.clear();
|
|
529
|
+
}
|
|
530
|
+
dispose() {
|
|
531
|
+
this.clear();
|
|
532
|
+
}
|
|
533
|
+
_applyThrusterForce(joint) {
|
|
534
|
+
const body = this._bodies.get(joint.bodyA);
|
|
535
|
+
if (!body || body.isStatic || !joint.active || joint.thrust === 0) {
|
|
536
|
+
joint.lastForce = 0;
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
_v0.copy(joint.localDirection).applyQuaternion(body.quaternion).normalize().multiplyScalar(joint.thrust);
|
|
540
|
+
_v1.copy(joint.localOffset).applyQuaternion(body.quaternion).add(body.position);
|
|
541
|
+
this.applyForce(joint.bodyA, _v0, _v1);
|
|
542
|
+
joint.lastForce = Math.abs(joint.thrust);
|
|
543
|
+
}
|
|
544
|
+
_applyWheelForces(joint, dt) {
|
|
545
|
+
const body = this._bodies.get(joint.bodyA);
|
|
546
|
+
if (!body)
|
|
547
|
+
return;
|
|
548
|
+
// World anchor & suspension direction
|
|
549
|
+
const worldAnchor = _v0.copy(joint.chassisAnchor).applyQuaternion(body.quaternion).add(body.position);
|
|
550
|
+
const worldDown = _v1.copy(joint.suspensionDirection).applyQuaternion(body.quaternion).normalize();
|
|
551
|
+
const maxRay = joint.suspensionRestLength + joint.wheelRadius;
|
|
552
|
+
const hit = this.raycastTerrain(worldAnchor, worldDown, maxRay);
|
|
553
|
+
if (!hit) {
|
|
554
|
+
joint.grounded = false;
|
|
555
|
+
joint.compression = 0;
|
|
556
|
+
joint.suspensionLength = joint.suspensionRestLength;
|
|
557
|
+
joint.lastForce = 0;
|
|
558
|
+
joint.wheelWorldPosition.copy(worldAnchor).addScaledVector(worldDown, joint.suspensionRestLength);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
joint.grounded = true;
|
|
562
|
+
joint.contactPoint.copy(hit.point);
|
|
563
|
+
joint.contactNormal.copy(hit.normal);
|
|
564
|
+
joint.suspensionLength = Math.max(0, hit.distance - joint.wheelRadius);
|
|
565
|
+
joint.compression = Math.max(0, Math.min(joint.suspensionRestLength, joint.suspensionRestLength - joint.suspensionLength));
|
|
566
|
+
joint.wheelWorldPosition.copy(worldAnchor).addScaledVector(worldDown, joint.suspensionLength);
|
|
567
|
+
if (body.isStatic)
|
|
568
|
+
return;
|
|
569
|
+
// Suspension spring-damper force along contact normal (prevents forward-speed bleed into damping when pitched)
|
|
570
|
+
const worldUp = _v2.copy(worldDown).negate();
|
|
571
|
+
const velAtAnchor = getWorldPointVelocity(body, worldAnchor, _v3);
|
|
572
|
+
const suspVel = velAtAnchor.dot(hit.normal);
|
|
573
|
+
const rawForce = joint.compression * joint.stiffness - suspVel * joint.damping;
|
|
574
|
+
const clampedForce = Math.max(0, Math.min(joint.maxSuspensionForce, rawForce));
|
|
575
|
+
joint.lastForce = clampedForce;
|
|
576
|
+
if (clampedForce > 0) {
|
|
577
|
+
_v4.copy(hit.normal).multiplyScalar(clampedForce);
|
|
578
|
+
this.applyForce(joint.bodyA, _v4, worldAnchor);
|
|
579
|
+
}
|
|
580
|
+
// Steered wheel forward and right axes projected onto contact surface
|
|
581
|
+
_q0.setFromAxisAngle(worldUp, joint.steerAngle);
|
|
582
|
+
const worldForward = _v4
|
|
583
|
+
.copy(joint.forwardAxis)
|
|
584
|
+
.applyQuaternion(body.quaternion)
|
|
585
|
+
.applyQuaternion(_q0);
|
|
586
|
+
// Project onto contact normal plane
|
|
587
|
+
worldForward.addScaledVector(hit.normal, -worldForward.dot(hit.normal));
|
|
588
|
+
if (worldForward.lengthSq() > 1e-8) {
|
|
589
|
+
worldForward.normalize();
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
worldForward.set(0, 0, -1);
|
|
593
|
+
}
|
|
594
|
+
const worldRight = _v5.crossVectors(worldForward, hit.normal).normalize();
|
|
595
|
+
const velAtContact = getWorldPointVelocity(body, hit.point, _v3);
|
|
596
|
+
const forwardSpeed = velAtContact.dot(worldForward);
|
|
597
|
+
const lateralSpeed = velAtContact.dot(worldRight);
|
|
598
|
+
// Advance visual wheel spin
|
|
599
|
+
joint.spinAngle += (forwardSpeed / joint.wheelRadius) * dt;
|
|
600
|
+
// Drive & braking force (applied at chassis wheel mount / roll-center to prevent unrealistic wheelie pitch)
|
|
601
|
+
let longForce = joint.driveForce;
|
|
602
|
+
if (joint.brakeForce > 0 && Math.abs(forwardSpeed) > 1e-4) {
|
|
603
|
+
longForce -= Math.sign(forwardSpeed) * Math.min(joint.brakeForce, (Math.abs(forwardSpeed) * body.mass) / Math.max(dt, 1e-4));
|
|
604
|
+
}
|
|
605
|
+
if (longForce !== 0) {
|
|
606
|
+
_v6.copy(worldForward).multiplyScalar(longForce);
|
|
607
|
+
this.applyForce(joint.bodyA, _v6, worldAnchor);
|
|
608
|
+
}
|
|
609
|
+
// Lateral tire grip (opposes sideways sliding, applied at chassis wheel mount)
|
|
610
|
+
if (joint.lateralGrip > 0 && Math.abs(lateralSpeed) > 1e-5) {
|
|
611
|
+
const maxLatForce = Math.max(clampedForce * 1.4, body.mass * 12);
|
|
612
|
+
const latForce = Math.max(-maxLatForce, Math.min(maxLatForce, -lateralSpeed * joint.lateralGrip * body.mass));
|
|
613
|
+
_v6.copy(worldRight).multiplyScalar(latForce);
|
|
614
|
+
this.applyForce(joint.bodyA, _v6, worldAnchor);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
_applySpringForce(joint) {
|
|
618
|
+
const bodyA = this._bodies.get(joint.bodyA);
|
|
619
|
+
if (!bodyA)
|
|
620
|
+
return;
|
|
621
|
+
const bodyB = joint.bodyB ? this._bodies.get(joint.bodyB) : null;
|
|
622
|
+
const wa = _v0.copy(joint.localAnchorA).applyQuaternion(bodyA.quaternion).add(bodyA.position);
|
|
623
|
+
const wb = bodyB
|
|
624
|
+
? _v1.copy(joint.localAnchorB).applyQuaternion(bodyB.quaternion).add(bodyB.position)
|
|
625
|
+
: _v1.copy(joint.worldAnchorB ?? wa);
|
|
626
|
+
const delta = _v2.subVectors(wb, wa);
|
|
627
|
+
const dist = delta.length();
|
|
628
|
+
joint.currentLength = dist;
|
|
629
|
+
if (dist < 1e-6) {
|
|
630
|
+
joint.lastForce = 0;
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const dir = delta.divideScalar(dist);
|
|
634
|
+
const velA = getWorldPointVelocity(bodyA, wa, _v3);
|
|
635
|
+
const velB = bodyB ? getWorldPointVelocity(bodyB, wb, _v4) : _v4.set(0, 0, 0);
|
|
636
|
+
const relVelAlong = (velB.x - velA.x) * dir.x + (velB.y - velA.y) * dir.y + (velB.z - velA.z) * dir.z;
|
|
637
|
+
const stretch = dist - joint.restLength;
|
|
638
|
+
let forceMag = stretch * joint.stiffness + relVelAlong * joint.damping;
|
|
639
|
+
if (Math.abs(forceMag) > joint.maxForce) {
|
|
640
|
+
forceMag = Math.sign(forceMag) * joint.maxForce;
|
|
641
|
+
}
|
|
642
|
+
joint.lastForce = Math.abs(forceMag);
|
|
643
|
+
if (joint.lastForce > joint.breakForce) {
|
|
644
|
+
joint.broken = true;
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const forceVec = _v5.copy(dir).multiplyScalar(forceMag);
|
|
648
|
+
if (!bodyA.isStatic) {
|
|
649
|
+
this.applyForce(joint.bodyA, forceVec, wa);
|
|
650
|
+
}
|
|
651
|
+
if (bodyB && !bodyB.isStatic) {
|
|
652
|
+
forceVec.negate();
|
|
653
|
+
this.applyForce(bodyB.id, forceVec, wb);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
_solveDistanceConstraint(joint, dt, checkBreak) {
|
|
657
|
+
const bodyA = this._bodies.get(joint.bodyA);
|
|
658
|
+
if (!bodyA)
|
|
659
|
+
return;
|
|
660
|
+
const bodyB = joint.bodyB ? this._bodies.get(joint.bodyB) : null;
|
|
661
|
+
const invMassA = bodyA.invMass;
|
|
662
|
+
const invMassB = bodyB ? bodyB.invMass : 0;
|
|
663
|
+
const totalInvMass = invMassA + invMassB;
|
|
664
|
+
if (totalInvMass <= 0)
|
|
665
|
+
return;
|
|
666
|
+
const wa = _v0.copy(joint.localAnchorA).applyQuaternion(bodyA.quaternion).add(bodyA.position);
|
|
667
|
+
const wb = bodyB
|
|
668
|
+
? _v1.copy(joint.localAnchorB).applyQuaternion(bodyB.quaternion).add(bodyB.position)
|
|
669
|
+
: _v1.copy(joint.worldAnchorB ?? wa);
|
|
670
|
+
const delta = _v2.subVectors(wb, wa);
|
|
671
|
+
const dist = delta.length();
|
|
672
|
+
joint.currentLength = dist;
|
|
673
|
+
if (dist < 1e-6)
|
|
674
|
+
return;
|
|
675
|
+
const error = dist - joint.restLength;
|
|
676
|
+
const dir = delta.divideScalar(dist);
|
|
677
|
+
// Positional correction
|
|
678
|
+
const corr = (error / totalInvMass) * 0.45;
|
|
679
|
+
if (invMassA > 0)
|
|
680
|
+
bodyA.position.addScaledVector(dir, corr * invMassA);
|
|
681
|
+
if (bodyB && invMassB > 0)
|
|
682
|
+
bodyB.position.addScaledVector(dir, -corr * invMassB);
|
|
683
|
+
// Velocity projection along constraint axis
|
|
684
|
+
const relVel = ((bodyB ? bodyB.linearVelocity.x : 0) - bodyA.linearVelocity.x) * dir.x +
|
|
685
|
+
((bodyB ? bodyB.linearVelocity.y : 0) - bodyA.linearVelocity.y) * dir.y +
|
|
686
|
+
((bodyB ? bodyB.linearVelocity.z : 0) - bodyA.linearVelocity.z) * dir.z;
|
|
687
|
+
const lambda = relVel / totalInvMass;
|
|
688
|
+
if (invMassA > 0)
|
|
689
|
+
bodyA.linearVelocity.addScaledVector(dir, lambda * invMassA);
|
|
690
|
+
if (bodyB && invMassB > 0)
|
|
691
|
+
bodyB.linearVelocity.addScaledVector(dir, -lambda * invMassB);
|
|
692
|
+
if (checkBreak) {
|
|
693
|
+
const estimatedForce = Math.abs(lambda) / Math.max(dt, 1e-4);
|
|
694
|
+
joint.lastForce = Math.max(joint.lastForce, estimatedForce);
|
|
695
|
+
if (joint.lastForce > joint.breakForce) {
|
|
696
|
+
joint.broken = true;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
_solveHingeConstraint(joint, dt, isLastIter) {
|
|
701
|
+
const bodyA = this._bodies.get(joint.bodyA);
|
|
702
|
+
const bodyB = this._bodies.get(joint.bodyB);
|
|
703
|
+
if (!bodyA || !bodyB)
|
|
704
|
+
return;
|
|
705
|
+
const totalInvMass = bodyA.invMass + bodyB.invMass;
|
|
706
|
+
if (totalInvMass > 0) {
|
|
707
|
+
// 1. Pin anchor positions together
|
|
708
|
+
const wa = _v0.copy(joint.localAnchorA).applyQuaternion(bodyA.quaternion).add(bodyA.position);
|
|
709
|
+
const wb = _v1.copy(joint.localAnchorB).applyQuaternion(bodyB.quaternion).add(bodyB.position);
|
|
710
|
+
const err = _v2.subVectors(wb, wa);
|
|
711
|
+
const errLen = err.length();
|
|
712
|
+
if (errLen > 1e-6) {
|
|
713
|
+
const corrFactor = 0.6 / totalInvMass;
|
|
714
|
+
if (bodyA.invMass > 0)
|
|
715
|
+
bodyA.position.addScaledVector(err, corrFactor * bodyA.invMass);
|
|
716
|
+
if (bodyB.invMass > 0)
|
|
717
|
+
bodyB.position.addScaledVector(err, -corrFactor * bodyB.invMass);
|
|
718
|
+
}
|
|
719
|
+
// Relative linear velocity at anchor
|
|
720
|
+
const va = getWorldPointVelocity(bodyA, wa, _v3);
|
|
721
|
+
const vb = getWorldPointVelocity(bodyB, wb, _v4);
|
|
722
|
+
const relV = _v5.subVectors(vb, va);
|
|
723
|
+
const impulse = relV.multiplyScalar(0.5 / totalInvMass);
|
|
724
|
+
if (bodyA.invMass > 0)
|
|
725
|
+
bodyA.linearVelocity.addScaledVector(impulse, bodyA.invMass);
|
|
726
|
+
if (bodyB.invMass > 0)
|
|
727
|
+
bodyB.linearVelocity.addScaledVector(impulse, -bodyB.invMass);
|
|
728
|
+
const estForce = impulse.length() / Math.max(dt, 1e-4);
|
|
729
|
+
joint.lastForce = estForce;
|
|
730
|
+
if (estForce > joint.breakForce) {
|
|
731
|
+
joint.broken = true;
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
// 2. Angular axis alignment + motor + limits (applied once per step on final iteration)
|
|
736
|
+
if (!isLastIter)
|
|
737
|
+
return;
|
|
738
|
+
const worldAxisA = _v0.copy(joint.axis).applyQuaternion(bodyA.quaternion).normalize();
|
|
739
|
+
const worldAxisB = _v1.copy(joint.axis).applyQuaternion(bodyB.quaternion).normalize();
|
|
740
|
+
// Remove off-axis relative angular velocity
|
|
741
|
+
const relOmega = _v2.subVectors(bodyB.angularVelocity, bodyA.angularVelocity);
|
|
742
|
+
let axialOmega = relOmega.dot(worldAxisA);
|
|
743
|
+
const offAxisOmega = _v3.copy(relOmega).addScaledVector(worldAxisA, -axialOmega);
|
|
744
|
+
const totalInvInertia = bodyA.invInertia + bodyB.invInertia;
|
|
745
|
+
if (totalInvInertia > 0) {
|
|
746
|
+
if (bodyA.invInertia > 0) {
|
|
747
|
+
bodyA.angularVelocity.addScaledVector(offAxisOmega, bodyA.invInertia / totalInvInertia);
|
|
748
|
+
}
|
|
749
|
+
if (bodyB.invInertia > 0) {
|
|
750
|
+
bodyB.angularVelocity.addScaledVector(offAxisOmega, -bodyB.invInertia / totalInvInertia);
|
|
751
|
+
}
|
|
752
|
+
// Align axis orientations gently
|
|
753
|
+
const alignCross = _v4.crossVectors(worldAxisB, worldAxisA);
|
|
754
|
+
const sinAngle = alignCross.length();
|
|
755
|
+
if (sinAngle > 1e-5) {
|
|
756
|
+
const angle = Math.asin(Math.min(1, sinAngle));
|
|
757
|
+
alignCross.divideScalar(sinAngle);
|
|
758
|
+
if (bodyB.invInertia > 0) {
|
|
759
|
+
_q0.setFromAxisAngle(alignCross, angle * (bodyB.invInertia / totalInvInertia));
|
|
760
|
+
bodyB.quaternion.premultiply(_q0).normalize();
|
|
761
|
+
}
|
|
762
|
+
if (bodyA.invInertia > 0) {
|
|
763
|
+
_q0.setFromAxisAngle(alignCross, -angle * (bodyA.invInertia / totalInvInertia));
|
|
764
|
+
bodyA.quaternion.premultiply(_q0).normalize();
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
// Motor drive around hinge axis
|
|
768
|
+
if (joint.motorSpeed !== 0 && joint.maxMotorTorque > 0) {
|
|
769
|
+
const speedError = joint.motorSpeed - axialOmega;
|
|
770
|
+
const maxDeltaOmega = joint.maxMotorTorque * totalInvInertia * dt;
|
|
771
|
+
const deltaOmega = Math.max(-maxDeltaOmega, Math.min(maxDeltaOmega, speedError));
|
|
772
|
+
if (bodyB.invInertia > 0) {
|
|
773
|
+
bodyB.angularVelocity.addScaledVector(worldAxisA, deltaOmega * (bodyB.invInertia / totalInvInertia));
|
|
774
|
+
}
|
|
775
|
+
if (bodyA.invInertia > 0) {
|
|
776
|
+
bodyA.angularVelocity.addScaledVector(worldAxisA, -deltaOmega * (bodyA.invInertia / totalInvInertia));
|
|
777
|
+
}
|
|
778
|
+
axialOmega += deltaOmega;
|
|
779
|
+
}
|
|
780
|
+
// Advance tracked hinge angle & clamp to [minAngle, maxAngle]
|
|
781
|
+
joint.currentAngle += axialOmega * dt;
|
|
782
|
+
if (joint.currentAngle < joint.minAngle) {
|
|
783
|
+
const correction = joint.minAngle - joint.currentAngle;
|
|
784
|
+
joint.currentAngle = joint.minAngle;
|
|
785
|
+
if (bodyB.invInertia > 0) {
|
|
786
|
+
_q0.setFromAxisAngle(worldAxisA, correction);
|
|
787
|
+
bodyB.quaternion.premultiply(_q0).normalize();
|
|
788
|
+
if (axialOmega < 0)
|
|
789
|
+
bodyB.angularVelocity.addScaledVector(worldAxisA, -axialOmega);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
else if (joint.currentAngle > joint.maxAngle) {
|
|
793
|
+
const correction = joint.maxAngle - joint.currentAngle;
|
|
794
|
+
joint.currentAngle = joint.maxAngle;
|
|
795
|
+
if (bodyB.invInertia > 0) {
|
|
796
|
+
_q0.setFromAxisAngle(worldAxisA, correction);
|
|
797
|
+
bodyB.quaternion.premultiply(_q0).normalize();
|
|
798
|
+
if (axialOmega > 0)
|
|
799
|
+
bodyB.angularVelocity.addScaledVector(worldAxisA, -axialOmega);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
_solveFixedConstraint(joint, dt, isLastIter) {
|
|
805
|
+
const bodyA = this._bodies.get(joint.bodyA);
|
|
806
|
+
const bodyB = this._bodies.get(joint.bodyB);
|
|
807
|
+
if (!bodyA || !bodyB)
|
|
808
|
+
return;
|
|
809
|
+
const totalInvMass = bodyA.invMass + bodyB.invMass;
|
|
810
|
+
if (totalInvMass <= 0)
|
|
811
|
+
return;
|
|
812
|
+
// Target position for bodyB in world space
|
|
813
|
+
const targetB = _v0.copy(joint.localOffset).applyQuaternion(bodyA.quaternion).add(bodyA.position);
|
|
814
|
+
const posErr = _v1.subVectors(targetB, bodyB.position);
|
|
815
|
+
// Relative velocity at weld
|
|
816
|
+
const relV = _v2.subVectors(bodyA.linearVelocity, bodyB.linearVelocity);
|
|
817
|
+
const impulse = _v3
|
|
818
|
+
.copy(relV)
|
|
819
|
+
.addScaledVector(posErr, 0.25 / Math.max(dt, 1e-4))
|
|
820
|
+
.multiplyScalar(1 / totalInvMass);
|
|
821
|
+
const estForce = impulse.length() / Math.max(dt, 1e-4);
|
|
822
|
+
joint.lastForce = estForce;
|
|
823
|
+
if (estForce > joint.breakForce) {
|
|
824
|
+
joint.broken = true;
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
// Apply corrective position & velocity
|
|
828
|
+
const posCorr = 0.65 / totalInvMass;
|
|
829
|
+
if (bodyA.invMass > 0)
|
|
830
|
+
bodyA.position.addScaledVector(posErr, -posCorr * bodyA.invMass);
|
|
831
|
+
if (bodyB.invMass > 0)
|
|
832
|
+
bodyB.position.addScaledVector(posErr, posCorr * bodyB.invMass);
|
|
833
|
+
const velLambda = 0.5 / totalInvMass;
|
|
834
|
+
if (bodyA.invMass > 0)
|
|
835
|
+
bodyA.linearVelocity.addScaledVector(relV, -velLambda * bodyA.invMass);
|
|
836
|
+
if (bodyB.invMass > 0)
|
|
837
|
+
bodyB.linearVelocity.addScaledVector(relV, velLambda * bodyB.invMass);
|
|
838
|
+
if (isLastIter) {
|
|
839
|
+
// Lock relative orientation
|
|
840
|
+
const targetQB = _q1.multiplyQuaternions(bodyA.quaternion, joint.localRotation).normalize();
|
|
841
|
+
if (bodyB.invInertia > 0 && bodyA.invInertia === 0) {
|
|
842
|
+
bodyB.quaternion.copy(targetQB);
|
|
843
|
+
bodyB.angularVelocity.copy(bodyA.angularVelocity);
|
|
844
|
+
}
|
|
845
|
+
else if (bodyA.invInertia > 0 && bodyB.invInertia === 0) {
|
|
846
|
+
_q2.copy(joint.localRotation).invert();
|
|
847
|
+
bodyA.quaternion.multiplyQuaternions(bodyB.quaternion, _q2).normalize();
|
|
848
|
+
bodyA.angularVelocity.copy(bodyB.angularVelocity);
|
|
849
|
+
}
|
|
850
|
+
else if (bodyA.invInertia + bodyB.invInertia > 0) {
|
|
851
|
+
const weightB = bodyB.invInertia / (bodyA.invInertia + bodyB.invInertia);
|
|
852
|
+
bodyB.quaternion.slerp(targetQB, Math.min(1, weightB * 0.85 + 0.15)).normalize();
|
|
853
|
+
const avgOmegaX = (bodyA.angularVelocity.x + bodyB.angularVelocity.x) * 0.5;
|
|
854
|
+
const avgOmegaY = (bodyA.angularVelocity.y + bodyB.angularVelocity.y) * 0.5;
|
|
855
|
+
const avgOmegaZ = (bodyA.angularVelocity.z + bodyB.angularVelocity.z) * 0.5;
|
|
856
|
+
bodyA.angularVelocity.set(avgOmegaX, avgOmegaY, avgOmegaZ);
|
|
857
|
+
bodyB.angularVelocity.set(avgOmegaX, avgOmegaY, avgOmegaZ);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
_resolveBodyVoxelCollisions(body) {
|
|
862
|
+
const s = this.voxelSize;
|
|
863
|
+
const invS = 1 / s;
|
|
864
|
+
const hx = body.shape === 'sphere' ? body.radius : body.halfExtents.x;
|
|
865
|
+
const hy = body.shape === 'sphere' ? body.radius : body.halfExtents.y;
|
|
866
|
+
const hz = body.shape === 'sphere' ? body.radius : body.halfExtents.z;
|
|
867
|
+
const minVx = Math.floor((body.position.x - hx) * invS);
|
|
868
|
+
const maxVx = Math.floor((body.position.x + hx) * invS);
|
|
869
|
+
const minVy = Math.floor((body.position.y - hy) * invS);
|
|
870
|
+
const maxVy = Math.floor((body.position.y + hy) * invS);
|
|
871
|
+
const minVz = Math.floor((body.position.z - hz) * invS);
|
|
872
|
+
const maxVz = Math.floor((body.position.z + hz) * invS);
|
|
873
|
+
for (let vy = minVy; vy <= maxVy; vy++) {
|
|
874
|
+
for (let vz = minVz; vz <= maxVz; vz++) {
|
|
875
|
+
for (let vx = minVx; vx <= maxVx; vx++) {
|
|
876
|
+
const blockId = this._getVoxel(vx, vy, vz);
|
|
877
|
+
if (!this._isSolid(blockId))
|
|
878
|
+
continue;
|
|
879
|
+
const bMinX = vx * s;
|
|
880
|
+
const bMaxX = bMinX + s;
|
|
881
|
+
const bMinY = vy * s;
|
|
882
|
+
const bMaxY = bMinY + s;
|
|
883
|
+
const bMinZ = vz * s;
|
|
884
|
+
const bMaxZ = bMinZ + s;
|
|
885
|
+
let nx = 0;
|
|
886
|
+
let ny = 0;
|
|
887
|
+
let nz = 0;
|
|
888
|
+
let penetration = 0;
|
|
889
|
+
let contactX = body.position.x;
|
|
890
|
+
let contactY = body.position.y;
|
|
891
|
+
let contactZ = body.position.z;
|
|
892
|
+
if (body.shape === 'sphere') {
|
|
893
|
+
const cx = Math.max(bMinX, Math.min(body.position.x, bMaxX));
|
|
894
|
+
const cy = Math.max(bMinY, Math.min(body.position.y, bMaxY));
|
|
895
|
+
const cz = Math.max(bMinZ, Math.min(body.position.z, bMaxZ));
|
|
896
|
+
const dx = body.position.x - cx;
|
|
897
|
+
const dy = body.position.y - cy;
|
|
898
|
+
const dz = body.position.z - cz;
|
|
899
|
+
const distSq = dx * dx + dy * dy + dz * dz;
|
|
900
|
+
if (distSq >= body.radius * body.radius)
|
|
901
|
+
continue;
|
|
902
|
+
contactX = cx;
|
|
903
|
+
contactY = cy;
|
|
904
|
+
contactZ = cz;
|
|
905
|
+
if (distSq > 1e-10) {
|
|
906
|
+
const dist = Math.sqrt(distSq);
|
|
907
|
+
nx = dx / dist;
|
|
908
|
+
ny = dy / dist;
|
|
909
|
+
nz = dz / dist;
|
|
910
|
+
penetration = body.radius - dist;
|
|
911
|
+
}
|
|
912
|
+
else {
|
|
913
|
+
// Sphere center inside voxel: push out along nearest exposed face
|
|
914
|
+
const dTop = bMaxY - body.position.y;
|
|
915
|
+
const dBot = body.position.y - bMinY;
|
|
916
|
+
const dRight = bMaxX - body.position.x;
|
|
917
|
+
const dLeft = body.position.x - bMinX;
|
|
918
|
+
const dFront = bMaxZ - body.position.z;
|
|
919
|
+
const dBack = body.position.z - bMinZ;
|
|
920
|
+
const minD = Math.min(dTop, dBot, dRight, dLeft, dFront, dBack);
|
|
921
|
+
penetration = minD + body.radius;
|
|
922
|
+
if (minD === dTop)
|
|
923
|
+
ny = 1;
|
|
924
|
+
else if (minD === dBot)
|
|
925
|
+
ny = -1;
|
|
926
|
+
else if (minD === dRight)
|
|
927
|
+
nx = 1;
|
|
928
|
+
else if (minD === dLeft)
|
|
929
|
+
nx = -1;
|
|
930
|
+
else if (minD === dFront)
|
|
931
|
+
nz = 1;
|
|
932
|
+
else
|
|
933
|
+
nz = -1;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
else {
|
|
937
|
+
// AABB vs Voxel AABB with internal-seam deprioritization
|
|
938
|
+
const overlapX = Math.min(body.position.x + hx, bMaxX) - Math.max(body.position.x - hx, bMinX);
|
|
939
|
+
const overlapY = Math.min(body.position.y + hy, bMaxY) - Math.max(body.position.y - hy, bMinY);
|
|
940
|
+
const overlapZ = Math.min(body.position.z + hz, bMaxZ) - Math.max(body.position.z - hz, bMinZ);
|
|
941
|
+
if (overlapX <= 0 || overlapY <= 0 || overlapZ <= 0)
|
|
942
|
+
continue;
|
|
943
|
+
const centerX = bMinX + s * 0.5;
|
|
944
|
+
const centerY = bMinY + s * 0.5;
|
|
945
|
+
const centerZ = bMinZ + s * 0.5;
|
|
946
|
+
const signX = body.position.x >= centerX ? 1 : -1;
|
|
947
|
+
const signY = body.position.y >= centerY ? 1 : -1;
|
|
948
|
+
const signZ = body.position.z >= centerZ ? 1 : -1;
|
|
949
|
+
// Penalize axes that push directly into an adjacent solid voxel (internal seam)
|
|
950
|
+
const blockedX = this._isSolid(this._getVoxel(vx + signX, vy, vz));
|
|
951
|
+
const blockedY = this._isSolid(this._getVoxel(vx, vy + signY, vz));
|
|
952
|
+
const blockedZ = this._isSolid(this._getVoxel(vx, vy, vz + signZ));
|
|
953
|
+
const scoreX = overlapX + (blockedX ? 1000 : 0);
|
|
954
|
+
const scoreY = overlapY + (blockedY ? 1000 : 0);
|
|
955
|
+
const scoreZ = overlapZ + (blockedZ ? 1000 : 0);
|
|
956
|
+
if (scoreY <= scoreX && scoreY <= scoreZ) {
|
|
957
|
+
ny = signY;
|
|
958
|
+
penetration = overlapY;
|
|
959
|
+
contactY = signY > 0 ? bMaxY : bMinY;
|
|
960
|
+
}
|
|
961
|
+
else if (scoreX <= scoreZ) {
|
|
962
|
+
nx = signX;
|
|
963
|
+
penetration = overlapX;
|
|
964
|
+
contactX = signX > 0 ? bMaxX : bMinX;
|
|
965
|
+
}
|
|
966
|
+
else {
|
|
967
|
+
nz = signZ;
|
|
968
|
+
penetration = overlapZ;
|
|
969
|
+
contactZ = signZ > 0 ? bMaxZ : bMinZ;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
if (penetration <= 0)
|
|
973
|
+
continue;
|
|
974
|
+
// Position resolution
|
|
975
|
+
body.position.x += nx * penetration;
|
|
976
|
+
body.position.y += ny * penetration;
|
|
977
|
+
body.position.z += nz * penetration;
|
|
978
|
+
if (ny > 0.5) {
|
|
979
|
+
body.grounded = true;
|
|
980
|
+
body.contactNormal.set(nx, ny, nz);
|
|
981
|
+
}
|
|
982
|
+
// Velocity impulse & Coulomb friction
|
|
983
|
+
const vDotN = body.linearVelocity.x * nx +
|
|
984
|
+
body.linearVelocity.y * ny +
|
|
985
|
+
body.linearVelocity.z * nz;
|
|
986
|
+
if (vDotN < 0) {
|
|
987
|
+
const impactSpeed = -vDotN;
|
|
988
|
+
const restitution = impactSpeed > 0.8 ? body.restitution : 0;
|
|
989
|
+
const jn = -(1 + restitution) * vDotN;
|
|
990
|
+
body.linearVelocity.x += nx * jn;
|
|
991
|
+
body.linearVelocity.y += ny * jn;
|
|
992
|
+
body.linearVelocity.z += nz * jn;
|
|
993
|
+
// Tangential friction
|
|
994
|
+
const tx = body.linearVelocity.x - (body.linearVelocity.x * nx + body.linearVelocity.y * ny + body.linearVelocity.z * nz) * nx;
|
|
995
|
+
const ty = body.linearVelocity.y - (body.linearVelocity.x * nx + body.linearVelocity.y * ny + body.linearVelocity.z * nz) * ny;
|
|
996
|
+
const tz = body.linearVelocity.z - (body.linearVelocity.x * nx + body.linearVelocity.y * ny + body.linearVelocity.z * nz) * nz;
|
|
997
|
+
const tLen = Math.hypot(tx, ty, tz);
|
|
998
|
+
if (tLen > 1e-6 && body.friction > 0) {
|
|
999
|
+
const jt = Math.min(tLen, jn * body.friction);
|
|
1000
|
+
body.linearVelocity.x -= (tx / tLen) * jt;
|
|
1001
|
+
body.linearVelocity.y -= (ty / tLen) * jt;
|
|
1002
|
+
body.linearVelocity.z -= (tz / tLen) * jt;
|
|
1003
|
+
}
|
|
1004
|
+
if (this.onImpact && impactSpeed >= this.impactSpeedThreshold) {
|
|
1005
|
+
this.onImpact({
|
|
1006
|
+
bodyId: body.id,
|
|
1007
|
+
speed: impactSpeed,
|
|
1008
|
+
normal: new Vector3(nx, ny, nz),
|
|
1009
|
+
point: new Vector3(contactX, contactY, contactZ),
|
|
1010
|
+
voxel: [vx, vy, vz],
|
|
1011
|
+
blockId,
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
//# sourceMappingURL=VoxelPhysicsWorld.js.map
|