From d1f1c1907a7ce763d7742b2a8edf6f940b4c22d3 Mon Sep 17 00:00:00 2001 From: liabru Date: Tue, 10 Mar 2020 08:48:42 +0000 Subject: [PATCH] Revert "Merge branch 'pr/527'" This reverts commit 4df048da79d5fc0e112096418ad6aa6097197eef, reversing changes made to 52f3734b3e9f2ddb956641baadd5aedbfa62b8ed. --- src/body/Body.js | 1 - src/collision/Resolver.js | 68 ++++++++++++++++++--------------------- src/core/Engine.js | 2 +- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/body/Body.js b/src/body/Body.js index 9c39195..f38d2c8 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -49,7 +49,6 @@ var Axes = require('../geometry/Axes'); force: { x: 0, y: 0 }, torque: 0, positionImpulse: { x: 0, y: 0 }, - previousPositionImpulse: { x: 0, y: 0 }, constraintImpulse: { x: 0, y: 0, angle: 0 }, totalContacts: 0, speed: 0, diff --git a/src/collision/Resolver.js b/src/collision/Resolver.js index 37c9692..c53817f 100644 --- a/src/collision/Resolver.js +++ b/src/collision/Resolver.js @@ -48,33 +48,23 @@ var Bounds = require('../geometry/Bounds'); * Find a solution for pair positions. * @method solvePosition * @param {pair[]} pairs - * @param {body[]} bodies * @param {number} timeScale */ - Resolver.solvePosition = function(pairs, bodies, timeScale) { + Resolver.solvePosition = function(pairs, timeScale) { var i, - normalX, - normalY, pair, collision, bodyA, bodyB, normal, - separation, - penetration, - positionImpulseA, - positionImpulseB, + bodyBtoA, contactShare, - bodyBtoAX, - bodyBtoAY, positionImpulse, - impulseCoefficient = timeScale * Resolver._positionDampen; - - for (i = 0; i < bodies.length; i++) { - var body = bodies[i]; - body.previousPositionImpulse.x = body.positionImpulse.x; - body.previousPositionImpulse.y = body.positionImpulse.y; - } + contactCount = {}, + tempA = Vector._temp[0], + tempB = Vector._temp[1], + tempC = Vector._temp[2], + tempD = Vector._temp[3]; // find impulses required to resolve penetration for (i = 0; i < pairs.length; i++) { @@ -88,35 +78,39 @@ var Bounds = require('../geometry/Bounds'); bodyB = collision.parentB; normal = collision.normal; - positionImpulseA = bodyA.previousPositionImpulse; - positionImpulseB = bodyB.previousPositionImpulse; + // get current separation between body edges involved in collision + bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, bodyB.position, tempA), + Vector.add(bodyA.positionImpulse, + Vector.sub(bodyB.position, collision.penetration, tempB), tempC), tempD); - penetration = collision.penetration; + pair.separation = Vector.dot(normal, bodyBtoA); + } + + for (i = 0; i < pairs.length; i++) { + pair = pairs[i]; - bodyBtoAX = positionImpulseB.x - positionImpulseA.x + penetration.x; - bodyBtoAY = positionImpulseB.y - positionImpulseA.y + penetration.y; - - normalX = normal.x; - normalY = normal.y; - - separation = normalX * bodyBtoAX + normalY * bodyBtoAY; - pair.separation = separation; - - positionImpulse = (separation - pair.slop) * impulseCoefficient; + if (!pair.isActive || pair.isSensor) + continue; + + collision = pair.collision; + bodyA = collision.parentA; + bodyB = collision.parentB; + normal = collision.normal; + positionImpulse = (pair.separation - pair.slop) * timeScale; if (bodyA.isStatic || bodyB.isStatic) positionImpulse *= 2; if (!(bodyA.isStatic || bodyA.isSleeping)) { - contactShare = positionImpulse / bodyA.totalContacts; - bodyA.positionImpulse.x += normalX * contactShare; - bodyA.positionImpulse.y += normalY * contactShare; + contactShare = Resolver._positionDampen / bodyA.totalContacts; + bodyA.positionImpulse.x += normal.x * positionImpulse * contactShare; + bodyA.positionImpulse.y += normal.y * positionImpulse * contactShare; } if (!(bodyB.isStatic || bodyB.isSleeping)) { - contactShare = positionImpulse / bodyB.totalContacts; - bodyB.positionImpulse.x -= normalX * contactShare; - bodyB.positionImpulse.y -= normalY * contactShare; + contactShare = Resolver._positionDampen / bodyB.totalContacts; + bodyB.positionImpulse.x -= normal.x * positionImpulse * contactShare; + bodyB.positionImpulse.y -= normal.y * positionImpulse * contactShare; } } }; @@ -347,4 +341,4 @@ var Bounds = require('../geometry/Bounds'); } }; -})(); \ No newline at end of file +})(); diff --git a/src/core/Engine.js b/src/core/Engine.js index 4afd6f4..644a4d7 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -195,7 +195,7 @@ var Body = require('../body/Body'); // iteratively resolve position between collisions Resolver.preSolvePosition(pairs.list); for (i = 0; i < engine.positionIterations; i++) { - Resolver.solvePosition(pairs.list, allBodies, timing.timeScale); + Resolver.solvePosition(pairs.list, timing.timeScale); } Resolver.postSolvePosition(allBodies);