Skip to content

Commit a21b419

Browse files
SollyBunnyMugen87
andauthored
AmmoPhysics: Support restitution (#32347)
Co-authored-by: Michael Herzog <[email protected]>
1 parent acc03cc commit a21b419

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

examples/jsm/physics/AmmoPhysics.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async function AmmoPhysics() {
8383

8484
if ( physics ) {
8585

86-
addMesh( child, physics.mass );
86+
addMesh( child, physics.mass, physics.restitution );
8787

8888
}
8989

@@ -93,27 +93,27 @@ async function AmmoPhysics() {
9393

9494
}
9595

96-
function addMesh( mesh, mass = 0 ) {
96+
function addMesh( mesh, mass = 0, restitution = 0 ) {
9797

9898
const shape = getShape( mesh.geometry );
9999

100100
if ( shape !== null ) {
101101

102102
if ( mesh.isInstancedMesh ) {
103103

104-
handleInstancedMesh( mesh, mass, shape );
104+
handleInstancedMesh( mesh, shape, mass, restitution );
105105

106106
} else if ( mesh.isMesh ) {
107107

108-
handleMesh( mesh, mass, shape );
108+
handleMesh( mesh, shape, mass, restitution );
109109

110110
}
111111

112112
}
113113

114114
}
115115

116-
function handleMesh( mesh, mass, shape ) {
116+
function handleMesh( mesh, shape, mass, restitution ) {
117117

118118
const position = mesh.position;
119119
const quaternion = mesh.quaternion;
@@ -129,6 +129,7 @@ async function AmmoPhysics() {
129129
shape.calculateLocalInertia( mass, localInertia );
130130

131131
const rbInfo = new AmmoLib.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia );
132+
rbInfo.set_m_restitution( restitution );
132133

133134
const body = new AmmoLib.btRigidBody( rbInfo );
134135
// body.setFriction( 4 );
@@ -144,7 +145,7 @@ async function AmmoPhysics() {
144145

145146
}
146147

147-
function handleInstancedMesh( mesh, mass, shape ) {
148+
function handleInstancedMesh( mesh, shape, mass, restitution ) {
148149

149150
const array = mesh.instanceMatrix.array;
150151

@@ -163,6 +164,7 @@ async function AmmoPhysics() {
163164
shape.calculateLocalInertia( mass, localInertia );
164165

165166
const rbInfo = new AmmoLib.btRigidBodyConstructionInfo( mass, motionState, shape, localInertia );
167+
rbInfo.set_m_restitution( restitution );
166168

167169
const body = new AmmoLib.btRigidBody( rbInfo );
168170
world.addRigidBody( body );
@@ -302,6 +304,7 @@ async function AmmoPhysics() {
302304
* @name AmmoPhysics#addMesh
303305
* @param {Mesh} mesh The mesh to add.
304306
* @param {number} [mass=0] The mass in kg of the mesh.
307+
* @param {number} [restitution=0] The restitution of the mesh, usually from 0 to 1. Represents how "bouncy" objects are when they collide with each other.
305308
*/
306309
addMesh: addMesh,
307310

examples/physics_ammo_break.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@
338338
physicsShape.calculateLocalInertia( mass, localInertia );
339339

340340
const rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia );
341-
const body = new Ammo.btRigidBody( rbInfo );
341+
rbInfo.set_m_restitution( 0.5 );
342+
rbInfo.set_m_friction( 0.5 );
342343

343-
body.setFriction( 0.5 );
344+
const body = new Ammo.btRigidBody( rbInfo );
344345

345346
if ( vel ) {
346347

0 commit comments

Comments
 (0)