From 182ba905d4da10bd126d55d751bbe74c25f0ada6 Mon Sep 17 00:00:00 2001 From: liabru Date: Sun, 23 Jul 2023 13:50:26 +0100 Subject: [PATCH] optimised Resolver.solveVelocity --- src/collision/Resolver.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/collision/Resolver.js b/src/collision/Resolver.js index 0335902e..df0a5937 100644 --- a/src/collision/Resolver.js +++ b/src/collision/Resolver.js @@ -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++) { @@ -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;