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

optimised Resolver.postSolvePosition

This commit is contained in:
liabru 2021-04-27 00:31:34 +01:00
parent 3cf65e8051
commit 49fbfba511

View file

@ -121,34 +121,40 @@ var Bounds = require('../geometry/Bounds');
* @param {body[]} bodies * @param {body[]} bodies
*/ */
Resolver.postSolvePosition = function(bodies) { Resolver.postSolvePosition = function(bodies) {
var positionWarming = Resolver._positionWarming;
for (var i = 0; i < bodies.length; i++) { for (var i = 0; i < bodies.length; i++) {
var body = bodies[i]; var body = bodies[i],
positionImpulse = body.positionImpulse,
positionImpulseX = positionImpulse.x,
positionImpulseY = positionImpulse.y,
velocity = body.velocity;
// reset contact count // reset contact count
body.totalContacts = 0; body.totalContacts = 0;
if (body.positionImpulse.x !== 0 || body.positionImpulse.y !== 0) { if (positionImpulseX !== 0 || positionImpulseY !== 0) {
// update body geometry // update body geometry
for (var j = 0; j < body.parts.length; j++) { for (var j = 0; j < body.parts.length; j++) {
var part = body.parts[j]; var part = body.parts[j];
Vertices.translate(part.vertices, body.positionImpulse); Vertices.translate(part.vertices, positionImpulse);
Bounds.update(part.bounds, part.vertices, body.velocity); Bounds.update(part.bounds, part.vertices, velocity);
part.position.x += body.positionImpulse.x; part.position.x += positionImpulseX;
part.position.y += body.positionImpulse.y; part.position.y += positionImpulseY;
} }
// move the body without changing velocity // move the body without changing velocity
body.positionPrev.x += body.positionImpulse.x; body.positionPrev.x += positionImpulseX;
body.positionPrev.y += body.positionImpulse.y; body.positionPrev.y += positionImpulseY;
if (Vector.dot(body.positionImpulse, body.velocity) < 0) { if (positionImpulseX * velocity.x + positionImpulseY * velocity.y < 0) {
// reset cached impulse if the body has velocity along it // reset cached impulse if the body has velocity along it
body.positionImpulse.x = 0; positionImpulse.x = 0;
body.positionImpulse.y = 0; positionImpulse.y = 0;
} else { } else {
// warm the next iteration // warm the next iteration
body.positionImpulse.x *= Resolver._positionWarming; positionImpulse.x *= positionWarming;
body.positionImpulse.y *= Resolver._positionWarming; positionImpulse.y *= positionWarming;
} }
} }
} }