0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-25 13:39:06 -05:00

improved friction, added body.frictionStatic

This commit is contained in:
liabru 2015-05-03 15:50:38 +01:00
parent 296059cba2
commit 937c7bfcb6
3 changed files with 17 additions and 8 deletions

View file

@ -52,6 +52,7 @@ var Body = {};
density: 0.001, density: 0.001,
restitution: 0, restitution: 0,
friction: 0.1, friction: 0.1,
frictionStatic: 0.5,
frictionAir: 0.01, frictionAir: 0.01,
collisionFilter: { collisionFilter: {
category: 0x0001, category: 0x0001,

View file

@ -32,6 +32,7 @@ var Pair = {};
timeUpdated: timestamp, timeUpdated: timestamp,
inverseMass: parentA.inverseMass + parentB.inverseMass, inverseMass: parentA.inverseMass + parentB.inverseMass,
friction: Math.min(parentA.friction, parentB.friction), friction: Math.min(parentA.friction, parentB.friction),
frictionStatic: Math.max(parentA.frictionStatic, parentB.frictionStatic),
restitution: Math.max(parentA.restitution, parentB.restitution), restitution: Math.max(parentA.restitution, parentB.restitution),
slop: Math.max(parentA.slop, parentB.slop) slop: Math.max(parentA.slop, parentB.slop)
}; };
@ -57,6 +58,7 @@ var Pair = {};
pair.collision = collision; pair.collision = collision;
pair.inverseMass = parentA.inverseMass + parentB.inverseMass; pair.inverseMass = parentA.inverseMass + parentB.inverseMass;
pair.friction = Math.min(parentA.friction, parentB.friction); 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.restitution = Math.max(parentA.restitution, parentB.restitution);
pair.slop = Math.max(parentA.slop, parentB.slop); pair.slop = Math.max(parentA.slop, parentB.slop);
activeContacts.length = 0; activeContacts.length = 0;

View file

@ -11,6 +11,7 @@ var Resolver = {};
Resolver._restingThresh = 4; Resolver._restingThresh = 4;
Resolver._positionDampen = 0.9; Resolver._positionDampen = 0.9;
Resolver._positionWarming = 0.8; Resolver._positionWarming = 0.8;
Resolver._frictionNormalMultiplier = 5;
/** /**
* Description * Description
@ -264,20 +265,25 @@ var Resolver = {};
// raw impulses // raw impulses
var normalImpulse = (1 + pair.restitution) * normalVelocity, 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 // coulomb friction
var tangentImpulse = tangentVelocity; var tangentImpulse = tangentVelocity,
if (tangentSpeed > normalForce * pair.friction * timeScaleSquared) maxFriction = Infinity;
tangentImpulse = normalForce * pair.friction * timeScaleSquared * tangentVelocityDirection;
if (tangentSpeed > pair.friction * pair.frictionStatic * normalForce * timeScaleSquared) {
tangentImpulse = pair.friction * tangentVelocityDirection * timeScaleSquared;
maxFriction = tangentSpeed;
}
// modify impulses accounting for mass, inertia and offset // modify impulses accounting for mass, inertia and offset
var oAcN = Vector.cross(offsetA, normal), var oAcN = Vector.cross(offsetA, normal),
oBcN = Vector.cross(offsetB, 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; normalImpulse *= share;
tangentImpulse *= share; tangentImpulse *= Math.min(share, 1);
// handle high velocity and resting collisions separately // handle high velocity and resting collisions separately
if (normalVelocity < 0 && normalVelocity * normalVelocity > Resolver._restingThresh * timeScaleSquared) { if (normalVelocity < 0 && normalVelocity * normalVelocity > Resolver._restingThresh * timeScaleSquared) {
// high velocity so clear cached contact impulse // high velocity so clear cached contact impulse
@ -293,7 +299,7 @@ var Resolver = {};
// tangent impulse, tends to -maxFriction or maxFriction // tangent impulse, tends to -maxFriction or maxFriction
var contactTangentImpulse = contact.tangentImpulse; 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; tangentImpulse = contact.tangentImpulse - contactTangentImpulse;
} }