Skip to content

Commit 937c7bf

Browse files
committed
improved friction, added body.frictionStatic
1 parent 296059c commit 937c7bf

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/body/Body.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var Body = {};
5252
density: 0.001,
5353
restitution: 0,
5454
friction: 0.1,
55+
frictionStatic: 0.5,
5556
frictionAir: 0.01,
5657
collisionFilter: {
5758
category: 0x0001,

src/collision/Pair.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var Pair = {};
3232
timeUpdated: timestamp,
3333
inverseMass: parentA.inverseMass + parentB.inverseMass,
3434
friction: Math.min(parentA.friction, parentB.friction),
35+
frictionStatic: Math.max(parentA.frictionStatic, parentB.frictionStatic),
3536
restitution: Math.max(parentA.restitution, parentB.restitution),
3637
slop: Math.max(parentA.slop, parentB.slop)
3738
};
@@ -57,6 +58,7 @@ var Pair = {};
5758
pair.collision = collision;
5859
pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
5960
pair.friction = Math.min(parentA.friction, parentB.friction);
61+
pair.frictionStatic = Math.max(parentA.frictionStatic, parentB.frictionStatic);
6062
pair.restitution = Math.max(parentA.restitution, parentB.restitution);
6163
pair.slop = Math.max(parentA.slop, parentB.slop);
6264
activeContacts.length = 0;

src/collision/Resolver.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var Resolver = {};
1111
Resolver._restingThresh = 4;
1212
Resolver._positionDampen = 0.9;
1313
Resolver._positionWarming = 0.8;
14+
Resolver._frictionNormalMultiplier = 5;
1415

1516
/**
1617
* Description
@@ -264,20 +265,25 @@ var Resolver = {};
264265

265266
// raw impulses
266267
var normalImpulse = (1 + pair.restitution) * normalVelocity,
267-
normalForce = Common.clamp(pair.separation + normalVelocity, 0, 1);
268+
normalForce = Common.clamp(pair.separation + normalVelocity, 0, 1) * Resolver._frictionNormalMultiplier;
268269

269270
// coulomb friction
270-
var tangentImpulse = tangentVelocity;
271-
if (tangentSpeed > normalForce * pair.friction * timeScaleSquared)
272-
tangentImpulse = normalForce * pair.friction * timeScaleSquared * tangentVelocityDirection;
271+
var tangentImpulse = tangentVelocity,
272+
maxFriction = Infinity;
273+
274+
if (tangentSpeed > pair.friction * pair.frictionStatic * normalForce * timeScaleSquared) {
275+
tangentImpulse = pair.friction * tangentVelocityDirection * timeScaleSquared;
276+
maxFriction = tangentSpeed;
277+
}
273278

274279
// modify impulses accounting for mass, inertia and offset
275280
var oAcN = Vector.cross(offsetA, normal),
276281
oBcN = Vector.cross(offsetB, normal),
277-
share = contactShare / (pair.inverseMass + bodyA.inverseInertia * oAcN * oAcN + bodyB.inverseInertia * oBcN * oBcN);
282+
share = contactShare / (bodyA.inverseMass + bodyB.inverseMass + bodyA.inverseInertia * oAcN * oAcN + bodyB.inverseInertia * oBcN * oBcN);
283+
278284
normalImpulse *= share;
279-
tangentImpulse *= share;
280-
285+
tangentImpulse *= Math.min(share, 1);
286+
281287
// handle high velocity and resting collisions separately
282288
if (normalVelocity < 0 && normalVelocity * normalVelocity > Resolver._restingThresh * timeScaleSquared) {
283289
// high velocity so clear cached contact impulse
@@ -293,7 +299,7 @@ var Resolver = {};
293299

294300
// tangent impulse, tends to -maxFriction or maxFriction
295301
var contactTangentImpulse = contact.tangentImpulse;
296-
contact.tangentImpulse = Common.clamp(contact.tangentImpulse + tangentImpulse, -tangentSpeed, tangentSpeed);
302+
contact.tangentImpulse = Common.clamp(contact.tangentImpulse + tangentImpulse, -maxFriction, maxFriction);
297303
tangentImpulse = contact.tangentImpulse - contactTangentImpulse;
298304
}
299305

0 commit comments

Comments
 (0)