Skip to content

Commit f773e2c

Browse files
committed
JoltPhysics.js: Added restitution.
1 parent edf772b commit f773e2c

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

examples/jsm/physics/JoltPhysics.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,9 @@ async function JoltPhysics() {
110110

111111
if ( shape === null ) return;
112112

113-
// shape.setMass( mass );
114-
// shape.setRestitution( restitution );
115-
116113
const body = mesh.isInstancedMesh
117-
? createInstancedBody( mesh, mass, shape )
118-
: createBody( mesh.position, mesh.quaternion, mass, shape );
114+
? createInstancedBody( mesh, mass, restitution, shape )
115+
: createBody( mesh.position, mesh.quaternion, mass, restitution, shape );
119116

120117
if ( mass > 0 ) {
121118

@@ -126,7 +123,7 @@ async function JoltPhysics() {
126123

127124
}
128125

129-
function createInstancedBody( mesh, mass, shape ) {
126+
function createInstancedBody( mesh, mass, restitution, shape ) {
130127

131128
const array = mesh.instanceMatrix.array;
132129

@@ -136,24 +133,26 @@ async function JoltPhysics() {
136133

137134
const position = _position.fromArray( array, i * 16 + 12 );
138135
const quaternion = _quaternion.setFromRotationMatrix( _matrix.fromArray( array, i * 16 ) ); // TODO Copilot did this
139-
bodies.push( createBody( position, quaternion, mass, shape ) );
136+
bodies.push( createBody( position, quaternion, mass, restitution, shape ) );
140137

141138
}
142139

143140
return bodies;
144141

145142
}
146143

147-
function createBody( position, quaternion, mass, shape ) {
144+
function createBody( position, rotation, mass, restitution, shape ) {
148145

149146
const pos = new Jolt.Vec3( position.x, position.y, position.z );
150-
const rot = new Jolt.Quat( quaternion.x, quaternion.y, quaternion.z, quaternion.w );
147+
const rot = new Jolt.Quat( rotation.x, rotation.y, rotation.z, rotation.w );
151148

152149
const motion = mass > 0 ? Jolt.EMotionType_Dynamic : Jolt.EMotionType_Static;
153150
const layer = mass > 0 ? LAYER_MOVING : LAYER_NON_MOVING;
154151

155-
let creationSettings = new Jolt.BodyCreationSettings( shape, pos, rot, motion, layer );
156-
let body = bodyInterface.CreateBody( creationSettings );
152+
const creationSettings = new Jolt.BodyCreationSettings( shape, pos, rot, motion, layer );
153+
creationSettings.mRestitution = restitution;
154+
155+
const body = bodyInterface.CreateBody( creationSettings );
157156

158157
bodyInterface.AddBody( body.GetID(), Jolt.EActivation_Activate );
159158

@@ -165,19 +164,21 @@ async function JoltPhysics() {
165164

166165
function setMeshPosition( mesh, position, index = 0 ) {
167166

168-
const body = meshMap.get( mesh );
169-
170167
if ( mesh.isInstancedMesh ) {
171168

172-
const body2 = body[ index ];
169+
const bodies = meshMap.get( mesh );
170+
171+
const body = bodies[ index ];
172+
173+
bodyInterface.RemoveBody( body.GetID() );
174+
bodyInterface.DestroyBody( body.GetID() );
173175

174-
bodyInterface.RemoveBody( body2.GetID() );
175-
bodyInterface.DestroyBody( body2.GetID() );
176+
const physics = mesh.userData.physics;
176177

177-
let shape = body2.GetShape();
178-
let newBody = createBody( position, { x: 0, y: 0, z: 0, w: 1 }, 1, shape );
178+
let shape = body.GetShape();
179+
let body2 = createBody( position, { x: 0, y: 0, z: 0, w: 1 }, physics.mass, physics.restitution, shape );
179180

180-
body[ index ] = newBody;
181+
bodies[ index ] = body2;
181182

182183
} else {
183184

@@ -253,10 +254,10 @@ async function JoltPhysics() {
253254
const body = meshMap.get( mesh );
254255

255256
const position = body.GetPosition();
256-
const quaternion = body.GetRotation();
257+
const rotation = body.GetRotation();
257258

258259
mesh.position.set( position.GetX(), position.GetY(), position.GetZ() );
259-
mesh.quaternion.set( quaternion.GetX(), quaternion.GetY(), quaternion.GetZ(), quaternion.GetW() );
260+
mesh.quaternion.set( rotation.GetX(), rotation.GetY(), rotation.GetZ(), rotation.GetW() );
260261

261262
}
262263

0 commit comments

Comments
 (0)