Skip to content

Commit

Permalink
optimised Resolver.solveVelocity
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jul 23, 2023
1 parent 97d502e commit 182ba90
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,23 @@ var Bounds = require('../geometry/Bounds');
var collision = pair.collision,
bodyA = collision.parentA,
bodyB = collision.parentB,
bodyAVelocity = bodyA.velocity,
bodyBVelocity = bodyB.velocity,
normalX = collision.normal.x,
normalY = collision.normal.y,
tangentX = collision.tangent.x,
tangentY = collision.tangent.y,
inverseMassTotal = pair.inverseMass,
friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier,
contacts = pair.contacts,
contactCount = pair.contactCount,
contactShare = 1 / contactCount,
inverseMassTotal = bodyA.inverseMass + bodyB.inverseMass,
friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier;

// update body velocities
bodyAVelocity.x = bodyA.position.x - bodyA.positionPrev.x;
bodyAVelocity.y = bodyA.position.y - bodyA.positionPrev.y;
bodyBVelocity.x = bodyB.position.x - bodyB.positionPrev.x;
bodyBVelocity.y = bodyB.position.y - bodyB.positionPrev.y;
bodyA.angularVelocity = bodyA.angle - bodyA.anglePrev;
bodyB.angularVelocity = bodyB.angle - bodyB.anglePrev;
contactShare = 1 / contactCount;

// get body velocities
var bodyAVelocityX = bodyA.position.x - bodyA.positionPrev.x,
bodyAVelocityY = bodyA.position.y - bodyA.positionPrev.y,
bodyAAngularVelocity = bodyA.angle - bodyA.anglePrev,
bodyBVelocityX = bodyB.position.x - bodyB.positionPrev.x,
bodyBVelocityY = bodyB.position.y - bodyB.positionPrev.y,
bodyBAngularVelocity = bodyB.angle - bodyB.anglePrev;

// resolve each contact
for (j = 0; j < contactCount; j++) {
Expand All @@ -278,10 +276,10 @@ var Bounds = require('../geometry/Bounds');
offsetBX = contactVertex.x - bodyB.position.x,
offsetBY = contactVertex.y - bodyB.position.y;

var velocityPointAX = bodyAVelocity.x - offsetAY * bodyA.angularVelocity,
velocityPointAY = bodyAVelocity.y + offsetAX * bodyA.angularVelocity,
velocityPointBX = bodyBVelocity.x - offsetBY * bodyB.angularVelocity,
velocityPointBY = bodyBVelocity.y + offsetBX * bodyB.angularVelocity;
var velocityPointAX = bodyAVelocityX - offsetAY * bodyAAngularVelocity,
velocityPointAY = bodyAVelocityY + offsetAX * bodyAAngularVelocity,
velocityPointBX = bodyBVelocityX - offsetBY * bodyBAngularVelocity,
velocityPointBY = bodyBVelocityY + offsetBX * bodyBAngularVelocity;

var relativeVelocityX = velocityPointAX - velocityPointBX,
relativeVelocityY = velocityPointAY - velocityPointBY;
Expand Down

0 comments on commit 182ba90

Please sign in to comment.