|
84 | 84 | cargo.SetFriction(Math.max((0.0, 1.0 - 0.1 * i))); |
85 | 85 | } |
86 | 86 |
|
| 87 | + const cLocalSpaceVelocity = new Jolt.Vec3(0, 0, -10.0); |
| 88 | + const cLocalSpaceAngularVelocity = new Jolt.Vec3(0, DegreesToRadians(10.0), 0); |
| 89 | + |
87 | 90 | // Register contact listener |
88 | 91 | const contactListener = new Jolt.ContactListenerJS(); |
89 | 92 | contactListener.OnContactAdded = (body1, body2, manifold, settings) => { |
|
94 | 97 |
|
95 | 98 | const body1ID = body1.GetID().GetIndexAndSequenceNumber(); |
96 | 99 | const body2ID = body2.GetID().GetIndexAndSequenceNumber(); |
97 | | - const rotation1 = wrapQuat(body1.GetRotation()); |
98 | | - const rotation2 = wrapQuat(body2.GetRotation()); |
| 100 | + const rotation1 = body1.GetRotation().Clone(); // Next call to GetRotation() will overwrite rotation1 |
| 101 | + const rotation2 = body2.GetRotation(); |
99 | 102 |
|
100 | 103 | const body1LinearBelt = linearBelts.find(belt => belt.GetID().GetIndexAndSequenceNumber() == body1ID) |
101 | 104 | const body2LinearBelt = linearBelts.find(belt => belt.GetID().GetIndexAndSequenceNumber() == body2ID) |
102 | 105 | if (body1LinearBelt || body2LinearBelt) { |
103 | 106 | // Determine the world space surface velocity of both bodies |
104 | | - const cLocalSpaceVelocity = new THREE.Vector3(0, 0, -10.0); |
105 | | - const body1LinearSurfaceVelocity = body1LinearBelt ? cLocalSpaceVelocity.applyQuaternion(rotation1) : new THREE.Vector3(0, 0, 0); |
106 | | - const body2LinearSurfaceVelocity = body2LinearBelt ? cLocalSpaceVelocity.applyQuaternion(rotation2) : new THREE.Vector3(0, 0, 0); |
| 107 | + const body1LinearSurfaceVelocity = body1LinearBelt ? rotation1.MulVec3(cLocalSpaceVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
| 108 | + const body2LinearSurfaceVelocity = body2LinearBelt ? rotation2.MulVec3(cLocalSpaceVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
107 | 109 |
|
108 | 110 | // Calculate the relative surface velocity |
109 | | - const v = body2LinearSurfaceVelocity.sub(body1LinearSurfaceVelocity); |
110 | | - settings.mRelativeLinearSurfaceVelocity.Set(v.x, v.y, v.z); |
| 111 | + body2LinearSurfaceVelocity.Sub(body1LinearSurfaceVelocity); |
| 112 | + settings.mRelativeLinearSurfaceVelocity = body2LinearSurfaceVelocity; |
111 | 113 | } |
112 | 114 |
|
113 | 115 | const angularBodyId = angularBelt.GetID().GetIndexAndSequenceNumber(); |
114 | 116 | const body1Angular = body1ID == angularBodyId; |
115 | 117 | const body2Angular = body2ID == angularBodyId; |
116 | 118 | if (body1Angular || body2Angular) { |
117 | 119 | // Determine the world space angular surface velocity of both bodies |
118 | | - const cLocalSpaceAngularVelocity = new THREE.Vector3(0, DegreesToRadians(10.0), 0); |
119 | | - const body1AngularSurfaceVelocity = body1Angular ? cLocalSpaceAngularVelocity.applyQuaternion(rotation1) : new THREE.Vector3(0, 0, 0); |
120 | | - const body2AngularSurfaceVelocity = body2Angular ? cLocalSpaceAngularVelocity.applyQuaternion(rotation2) : new THREE.Vector3(0, 0, 0); |
| 120 | + const body1AngularSurfaceVelocity = body1Angular ? rotation1.MulVec3(cLocalSpaceAngularVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
| 121 | + const body2AngularSurfaceVelocity = body2Angular ? rotation2.MulVec3(cLocalSpaceAngularVelocity).Clone() : new Jolt.Vec3(0, 0, 0); |
121 | 122 |
|
122 | 123 | // 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 |
123 | | - const COM1 = wrapVec3(body1.GetCenterOfMassPosition()); |
124 | | - const COM2 = wrapVec3(body2.GetCenterOfMassPosition()); |
125 | | - const body2LinearSurfaceVelocity = body2Angular ? |
126 | | - body2AngularSurfaceVelocity.cross(COM1.clone().sub(COM2)) : new THREE.Vector3(0, 0, 0); |
| 124 | + const COM1 = body1.GetCenterOfMassPosition().Clone(); |
| 125 | + const COM2 = body2.GetCenterOfMassPosition(); |
| 126 | + const body2LinearSurfaceVelocity = body2Angular ? body2AngularSurfaceVelocity.Cross(COM1.Sub(COM2)).Clone() : new Jolt.Vec3(0, 0, 0); |
127 | 127 |
|
128 | 128 | // Calculate the relative angular surface velocity |
129 | | - const rls = body2LinearSurfaceVelocity; |
130 | | - settings.mRelativeLinearSurfaceVelocity.Set(rls.x, rls.y, rls.z); |
131 | | - const ras = body2AngularSurfaceVelocity.sub(body1AngularSurfaceVelocity); |
132 | | - settings.mRelativeAngularSurfaceVelocity.Set(ras.x, ras.y, ras.z); |
| 129 | + settings.mRelativeLinearSurfaceVelocity = body2LinearSurfaceVelocity; |
| 130 | + body2AngularSurfaceVelocity.Sub(body1AngularSurfaceVelocity); |
| 131 | + settings.mRelativeAngularSurfaceVelocity = body2AngularSurfaceVelocity; |
133 | 132 | } |
134 | | - |
135 | 133 | }; |
136 | 134 | contactListener.OnContactPersisted = (body1, body2, manifold, settings) => { |
137 | 135 | // Same behavior as contact added |
|
0 commit comments