|
20 | 20 | <script src="js/example.js"></script> |
21 | 21 |
|
22 | 22 | <script id="workerScript" type="module"> |
23 | | - import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.min.js'; |
24 | | - |
25 | | - const wrapQuat = (q) => new THREE.Quaternion(q.GetX(), q.GetY(), q.GetZ(), q.GetW()); |
26 | | - const wrapVec3 = (v) => new THREE.Vector3(v.GetX(), v.GetY(), v.GetZ()); |
27 | | - const DegreesToRadians = (deg) => deg * (Math.PI / 180.0); |
28 | 23 | const onWorker = globalThis.onWorker = async (Jolt, args) => { |
| 24 | + Jolt.Vec3.prototype.Clone = function () { return new Jolt.Vec3(this.GetX(), this.GetY(), this.GetZ()); }; |
| 25 | + Jolt.RVec3.prototype.Clone = function () { return new Jolt.RVec3(this.GetX(), this.GetY(), this.GetZ()); }; |
| 26 | + Jolt.Quat.prototype.Clone = function () { return new Jolt.Quat(this.GetX(), this.GetY(), this.GetZ(), this.GetW()); }; |
| 27 | + const DegreesToRadians = (deg) => deg * (Math.PI / 180.0); |
| 28 | + |
| 29 | + const cLocalSpaceVelocity = new Jolt.Vec3(0, 0, -10.0); |
| 30 | + const cLocalSpaceAngularVelocity = new Jolt.Vec3(0, DegreesToRadians(10.0), 0); |
| 31 | + |
29 | 32 | const { contactListenerPtr, linearBelts, angularBelt } = args; |
30 | 33 | const contactListener = Jolt.wrapPointer(contactListenerPtr, Jolt.ContactListenerJS); |
31 | 34 |
|
|
37 | 40 |
|
38 | 41 | const body1ID = body1.GetID().GetIndexAndSequenceNumber(); |
39 | 42 | const body2ID = body2.GetID().GetIndexAndSequenceNumber(); |
40 | | - const rotation1 = wrapQuat(body1.GetRotation()); |
41 | | - const rotation2 = wrapQuat(body2.GetRotation()); |
| 43 | + const rotation1 = body1.GetRotation().Clone(); // Next call to GetRotation() will overwrite rotation1 |
| 44 | + const rotation2 = body2.GetRotation(); |
42 | 45 |
|
43 | 46 | const body1LinearBelt = linearBelts.find(belt => belt == body1ID) |
44 | 47 | const body2LinearBelt = linearBelts.find(belt => belt == body2ID) |
45 | 48 | if (body1LinearBelt || body2LinearBelt) { |
46 | 49 | // Determine the world space surface velocity of both bodies |
47 | | - const cLocalSpaceVelocity = new THREE.Vector3(0, 0, -10.0); |
48 | | - const body1LinearSurfaceVelocity = body1LinearBelt ? cLocalSpaceVelocity.applyQuaternion(rotation1) : new THREE.Vector3(0, 0, 0); |
49 | | - const body2LinearSurfaceVelocity = body2LinearBelt ? cLocalSpaceVelocity.applyQuaternion(rotation2) : new THREE.Vector3(0, 0, 0); |
| 50 | + const body1LinearSurfaceVelocity = body1LinearBelt ? rotation1.MulVec3(cLocalSpaceVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
| 51 | + const body2LinearSurfaceVelocity = body2LinearBelt ? rotation2.MulVec3(cLocalSpaceVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
50 | 52 |
|
51 | 53 | // Calculate the relative surface velocity |
52 | | - const v = body2LinearSurfaceVelocity.sub(body1LinearSurfaceVelocity); |
53 | | - settings.mRelativeLinearSurfaceVelocity.Set(v.x, v.y, v.z); |
| 54 | + body2LinearSurfaceVelocity.Sub(body1LinearSurfaceVelocity); |
| 55 | + settings.mRelativeLinearSurfaceVelocity = body2LinearSurfaceVelocity; |
54 | 56 | } |
55 | 57 |
|
56 | 58 | const angularBodyId = angularBelt[0]; |
57 | 59 | const body1Angular = body1ID == angularBodyId; |
58 | 60 | const body2Angular = body2ID == angularBodyId; |
59 | 61 | if (body1Angular || body2Angular) { |
60 | 62 | // Determine the world space angular surface velocity of both bodies |
61 | | - const cLocalSpaceAngularVelocity = new THREE.Vector3(0, DegreesToRadians(10.0), 0); |
62 | | - const body1AngularSurfaceVelocity = body1Angular ? cLocalSpaceAngularVelocity.applyQuaternion(rotation1) : new THREE.Vector3(0, 0, 0); |
63 | | - const body2AngularSurfaceVelocity = body2Angular ? cLocalSpaceAngularVelocity.applyQuaternion(rotation2) : new THREE.Vector3(0, 0, 0); |
| 63 | + const body1AngularSurfaceVelocity = body1Angular ? rotation1.MulVec3(cLocalSpaceAngularVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
| 64 | + const body2AngularSurfaceVelocity = body2Angular ? rotation2.MulVec3(cLocalSpaceAngularVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
64 | 65 |
|
65 | 66 | // Note that the angular velocity is the angular velocity around body 1's center of mass, so we need to add the linear velocity of body 2's center of mass |
66 | | - const COM1 = wrapVec3(body1.GetCenterOfMassPosition()); |
67 | | - const COM2 = wrapVec3(body2.GetCenterOfMassPosition()); |
68 | | - const body2LinearSurfaceVelocity = body2Angular ? |
69 | | - body2AngularSurfaceVelocity.cross(COM1.clone().sub(COM2)) : new THREE.Vector3(0, 0, 0); |
| 67 | + const COM1 = body1.GetCenterOfMassPosition().Clone(); |
| 68 | + const COM2 = body2.GetCenterOfMassPosition(); |
| 69 | + const body2LinearSurfaceVelocity = body2Angular ? body2AngularSurfaceVelocity.Cross(COM1.Sub(COM2)).Clone() : new Jolt.Vec3(0, 0, 0); |
70 | 70 |
|
71 | 71 | // Calculate the relative angular surface velocity |
72 | | - const rls = body2LinearSurfaceVelocity; |
73 | | - settings.mRelativeLinearSurfaceVelocity.Set(rls.x, rls.y, rls.z); |
74 | | - const ras = body2AngularSurfaceVelocity.sub(body1AngularSurfaceVelocity); |
75 | | - settings.mRelativeAngularSurfaceVelocity.Set(ras.x, ras.y, ras.z); |
| 72 | + settings.mRelativeLinearSurfaceVelocity = body2LinearSurfaceVelocity; |
| 73 | + body2AngularSurfaceVelocity.Sub(body1AngularSurfaceVelocity); |
| 74 | + settings.mRelativeAngularSurfaceVelocity = body2AngularSurfaceVelocity; |
76 | 75 | } |
77 | | - |
78 | 76 | }; |
79 | 77 | contactListener.OnContactPersisted = (body1, body2, manifold, settings) => { |
80 | 78 | // Same behavior as contact added |
|
0 commit comments