Skip to content

Commit

Permalink
improved friction, added body.frictionStatic
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed May 3, 2015
1 parent 296059c commit 937c7bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var Body = {};
density: 0.001,
restitution: 0,
friction: 0.1,
frictionStatic: 0.5,
frictionAir: 0.01,
collisionFilter: {
category: 0x0001,
Expand Down
2 changes: 2 additions & 0 deletions src/collision/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var Pair = {};
timeUpdated: timestamp,
inverseMass: parentA.inverseMass + parentB.inverseMass,
friction: Math.min(parentA.friction, parentB.friction),
frictionStatic: Math.max(parentA.frictionStatic, parentB.frictionStatic),
restitution: Math.max(parentA.restitution, parentB.restitution),
slop: Math.max(parentA.slop, parentB.slop)
};
Expand All @@ -57,6 +58,7 @@ var Pair = {};
pair.collision = collision;
pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
pair.friction = Math.min(parentA.friction, parentB.friction);
pair.frictionStatic = Math.max(parentA.frictionStatic, parentB.frictionStatic);
pair.restitution = Math.max(parentA.restitution, parentB.restitution);
pair.slop = Math.max(parentA.slop, parentB.slop);
activeContacts.length = 0;
Expand Down
22 changes: 14 additions & 8 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var Resolver = {};
Resolver._restingThresh = 4;
Resolver._positionDampen = 0.9;
Resolver._positionWarming = 0.8;
Resolver._frictionNormalMultiplier = 5;

/**
* Description
Expand Down Expand Up @@ -264,20 +265,25 @@ var Resolver = {};

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

// coulomb friction
var tangentImpulse = tangentVelocity;
if (tangentSpeed > normalForce * pair.friction * timeScaleSquared)
tangentImpulse = normalForce * pair.friction * timeScaleSquared * tangentVelocityDirection;
var tangentImpulse = tangentVelocity,
maxFriction = Infinity;

if (tangentSpeed > pair.friction * pair.frictionStatic * normalForce * timeScaleSquared) {
tangentImpulse = pair.friction * tangentVelocityDirection * timeScaleSquared;
maxFriction = tangentSpeed;
}

// modify impulses accounting for mass, inertia and offset
var oAcN = Vector.cross(offsetA, normal),
oBcN = Vector.cross(offsetB, normal),
share = contactShare / (pair.inverseMass + bodyA.inverseInertia * oAcN * oAcN + bodyB.inverseInertia * oBcN * oBcN);
share = contactShare / (bodyA.inverseMass + bodyB.inverseMass + bodyA.inverseInertia * oAcN * oAcN + bodyB.inverseInertia * oBcN * oBcN);

normalImpulse *= share;
tangentImpulse *= share;
tangentImpulse *= Math.min(share, 1);

// handle high velocity and resting collisions separately
if (normalVelocity < 0 && normalVelocity * normalVelocity > Resolver._restingThresh * timeScaleSquared) {
// high velocity so clear cached contact impulse
Expand All @@ -293,7 +299,7 @@ var Resolver = {};

// tangent impulse, tends to -maxFriction or maxFriction
var contactTangentImpulse = contact.tangentImpulse;
contact.tangentImpulse = Common.clamp(contact.tangentImpulse + tangentImpulse, -tangentSpeed, tangentSpeed);
contact.tangentImpulse = Common.clamp(contact.tangentImpulse + tangentImpulse, -maxFriction, maxFriction);
tangentImpulse = contact.tangentImpulse - contactTangentImpulse;
}

Expand Down

0 comments on commit 937c7bf

Please sign in to comment.