0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

improved constraint performance

This commit is contained in:
liabru 2014-03-30 18:21:40 +01:00
parent 419a4300cc
commit b24408635a
3 changed files with 43 additions and 18 deletions

View file

@ -27,6 +27,7 @@ var Body = {};
force: { x: 0, y: 0 }, force: { x: 0, y: 0 },
torque: 0, torque: 0,
positionImpulse: { x: 0, y: 0 }, positionImpulse: { x: 0, y: 0 },
constraintImpulse: { x: 0, y: 0, angle: 0 },
speed: 0, speed: 0,
angularSpeed: 0, angularSpeed: 0,
velocity: { x: 0, y: 0 }, velocity: { x: 0, y: 0 },

View file

@ -64,21 +64,21 @@ var Constraint = {};
/** /**
* Description * Description
* @method updateAll * @method solveAll
* @param {constraint[]} constraints * @param {constraint[]} constraints
*/ */
Constraint.updateAll = function(constraints) { Constraint.solveAll = function(constraints) {
for (var i = 0; i < constraints.length; i++) { for (var i = 0; i < constraints.length; i++) {
Constraint.update(constraints[i]); Constraint.solve(constraints[i]);
} }
}; };
/** /**
* Description * Description
* @method update * @method solve
* @param {constraint} constraint * @param {constraint} constraint
*/ */
Constraint.update = function(constraint) { Constraint.solve = function(constraint) {
var bodyA = constraint.bodyA, var bodyA = constraint.bodyA,
bodyB = constraint.bodyB, bodyB = constraint.bodyB,
pointA = constraint.pointA, pointA = constraint.pointA,
@ -189,16 +189,15 @@ var Constraint = {};
// TODO: solve this properlly // TODO: solve this properlly
torque = Common.clamp(torque, -0.01, 0.01); torque = Common.clamp(torque, -0.01, 0.01);
// keep track of applied impulses for post solving
bodyA.constraintImpulse.x -= force.x;
bodyA.constraintImpulse.y -= force.y;
bodyA.constraintImpulse.angle += torque;
// apply forces // apply forces
bodyA.position.x -= force.x; bodyA.position.x -= force.x;
bodyA.position.y -= force.y; bodyA.position.y -= force.y;
bodyA.angle += torque; bodyA.angle += torque;
// update geometry
Vertices.translate(bodyA.vertices, force, -1);
Vertices.rotate(bodyA.vertices, torque, bodyA.position);
Axes.rotate(bodyA.axes, torque);
Bounds.update(bodyA.bounds, bodyA.vertices, bodyA.velocity);
} }
if (bodyB && !bodyB.isStatic) { if (bodyB && !bodyB.isStatic) {
@ -210,20 +209,44 @@ var Constraint = {};
// TODO: solve this properlly // TODO: solve this properlly
torque = Common.clamp(torque, -0.01, 0.01); torque = Common.clamp(torque, -0.01, 0.01);
// keep track of applied impulses for post solving
bodyB.constraintImpulse.x += force.x;
bodyB.constraintImpulse.y += force.y;
bodyB.constraintImpulse.angle -= torque;
// apply forces // apply forces
bodyB.position.x += force.x; bodyB.position.x += force.x;
bodyB.position.y += force.y; bodyB.position.y += force.y;
bodyB.angle -= torque; bodyB.angle -= torque;
// update geometry
Vertices.translate(bodyB.vertices, force);
Vertices.rotate(bodyB.vertices, -torque, bodyB.position);
Axes.rotate(bodyB.axes, -torque);
Bounds.update(bodyB.bounds, bodyB.vertices, bodyB.velocity);
} }
}; };
/**
* Performs body updates required after solving constraints
* @method postSolveAll
* @param {body[]} bodies
*/
Constraint.postSolveAll = function(bodies) {
for (var i = 0; i < bodies.length; i++) {
var body = bodies[i],
impulse = body.constraintImpulse;
if (impulse.x !== 0 || impulse.y !== 0 || impulse.angle !== 0) {
// update geometry
Vertices.translate(body.vertices, impulse);
Vertices.rotate(body.vertices, impulse.angle, body.position);
Axes.rotate(body.axes, impulse.angle);
Bounds.update(body.bounds, body.vertices);
// reset body.constraintImpulse
impulse.x = 0;
impulse.y = 0;
impulse.angle = 0;
}
}
};
/** /**
* Returns the next unique constraintId * Returns the next unique constraintId
* @method nextId * @method nextId

View file

@ -255,8 +255,9 @@ var Engine = {};
// update all constraints // update all constraints
for (i = 0; i < engine.constraintIterations; i++) { for (i = 0; i < engine.constraintIterations; i++) {
Constraint.updateAll(allConstraints); Constraint.solveAll(allConstraints);
} }
Constraint.postSolveAll(allBodies);
// broadphase pass: find potential collision pairs // broadphase pass: find potential collision pairs
if (broadphase.controller) { if (broadphase.controller) {