mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-30 10:20:52 -05:00
optimised Resolver.preSolveVelocity
This commit is contained in:
parent
fceb0cab68
commit
0b07a31944
1 changed files with 35 additions and 45 deletions
|
@ -9,8 +9,6 @@ var Resolver = {};
|
||||||
module.exports = Resolver;
|
module.exports = Resolver;
|
||||||
|
|
||||||
var Vertices = require('../geometry/Vertices');
|
var Vertices = require('../geometry/Vertices');
|
||||||
var Vector = require('../geometry/Vector');
|
|
||||||
var Common = require('../core/Common');
|
|
||||||
var Bounds = require('../geometry/Bounds');
|
var Bounds = require('../geometry/Bounds');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
@ -169,61 +167,53 @@ var Bounds = require('../geometry/Bounds');
|
||||||
* @param {pair[]} pairs
|
* @param {pair[]} pairs
|
||||||
*/
|
*/
|
||||||
Resolver.preSolveVelocity = function(pairs) {
|
Resolver.preSolveVelocity = function(pairs) {
|
||||||
var i,
|
var pairsLength = pairs.length,
|
||||||
j,
|
i,
|
||||||
pair,
|
j;
|
||||||
contacts,
|
|
||||||
collision,
|
|
||||||
bodyA,
|
|
||||||
bodyB,
|
|
||||||
normal,
|
|
||||||
tangent,
|
|
||||||
contact,
|
|
||||||
contactVertex,
|
|
||||||
normalImpulse,
|
|
||||||
tangentImpulse,
|
|
||||||
offset,
|
|
||||||
impulse = Vector._temp[0],
|
|
||||||
tempA = Vector._temp[1];
|
|
||||||
|
|
||||||
for (i = 0; i < pairs.length; i++) {
|
for (i = 0; i < pairsLength; i++) {
|
||||||
pair = pairs[i];
|
var pair = pairs[i];
|
||||||
|
|
||||||
if (!pair.isActive || pair.isSensor)
|
if (!pair.isActive || pair.isSensor)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
contacts = pair.activeContacts;
|
var contacts = pair.activeContacts,
|
||||||
collision = pair.collision;
|
contactsLength = contacts.length,
|
||||||
bodyA = collision.parentA;
|
collision = pair.collision,
|
||||||
bodyB = collision.parentB;
|
bodyA = collision.parentA,
|
||||||
normal = collision.normal;
|
bodyB = collision.parentB,
|
||||||
tangent = collision.tangent;
|
normal = collision.normal,
|
||||||
|
tangent = collision.tangent;
|
||||||
|
|
||||||
// resolve each contact
|
// resolve each contact
|
||||||
for (j = 0; j < contacts.length; j++) {
|
for (j = 0; j < contactsLength; j++) {
|
||||||
contact = contacts[j];
|
var contact = contacts[j],
|
||||||
contactVertex = contact.vertex;
|
contactVertex = contact.vertex,
|
||||||
normalImpulse = contact.normalImpulse;
|
normalImpulse = contact.normalImpulse,
|
||||||
tangentImpulse = contact.tangentImpulse;
|
tangentImpulse = contact.tangentImpulse;
|
||||||
|
|
||||||
if (normalImpulse !== 0 || tangentImpulse !== 0) {
|
if (normalImpulse !== 0 || tangentImpulse !== 0) {
|
||||||
// total impulse from contact
|
// total impulse from contact
|
||||||
impulse.x = (normal.x * normalImpulse) + (tangent.x * tangentImpulse);
|
var impulseX = normal.x * normalImpulse + tangent.x * tangentImpulse,
|
||||||
impulse.y = (normal.y * normalImpulse) + (tangent.y * tangentImpulse);
|
impulseY = normal.y * normalImpulse + tangent.y * tangentImpulse;
|
||||||
|
|
||||||
// apply impulse from contact
|
// apply impulse from contact
|
||||||
if (!(bodyA.isStatic || bodyA.isSleeping)) {
|
if (!(bodyA.isStatic || bodyA.isSleeping)) {
|
||||||
offset = Vector.sub(contactVertex, bodyA.position, tempA);
|
bodyA.positionPrev.x += impulseX * bodyA.inverseMass;
|
||||||
bodyA.positionPrev.x += impulse.x * bodyA.inverseMass;
|
bodyA.positionPrev.y += impulseY * bodyA.inverseMass;
|
||||||
bodyA.positionPrev.y += impulse.y * bodyA.inverseMass;
|
bodyA.anglePrev += bodyA.inverseInertia * (
|
||||||
bodyA.anglePrev += Vector.cross(offset, impulse) * bodyA.inverseInertia;
|
(contactVertex.x - bodyA.position.x) * impulseY
|
||||||
|
- (contactVertex.y - bodyA.position.y) * impulseX
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(bodyB.isStatic || bodyB.isSleeping)) {
|
if (!(bodyB.isStatic || bodyB.isSleeping)) {
|
||||||
offset = Vector.sub(contactVertex, bodyB.position, tempA);
|
bodyB.positionPrev.x -= impulseX * bodyB.inverseMass;
|
||||||
bodyB.positionPrev.x -= impulse.x * bodyB.inverseMass;
|
bodyB.positionPrev.y -= impulseY * bodyB.inverseMass;
|
||||||
bodyB.positionPrev.y -= impulse.y * bodyB.inverseMass;
|
bodyB.anglePrev -= bodyB.inverseInertia * (
|
||||||
bodyB.anglePrev -= Vector.cross(offset, impulse) * bodyB.inverseInertia;
|
(contactVertex.x - bodyB.position.x) * impulseY
|
||||||
|
- (contactVertex.y - bodyB.position.y) * impulseX
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -281,7 +271,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
var tangentVelocity = Vector.dot(tangent, relativeVelocity),
|
var tangentVelocity = Vector.dot(tangent, relativeVelocity),
|
||||||
tangentSpeed = Math.abs(tangentVelocity),
|
tangentSpeed = Math.abs(tangentVelocity),
|
||||||
tangentVelocityDirection = Common.sign(tangentVelocity);
|
tangentVelocityDirection = Common.sign(tangentVelocity);
|
||||||
|
|
||||||
// raw impulses
|
// raw impulses
|
||||||
var normalImpulse = (1 + pair.restitution) * normalVelocity,
|
var normalImpulse = (1 + pair.restitution) * normalVelocity,
|
||||||
normalForce = Common.clamp(pair.separation + normalVelocity, 0, 1) * Resolver._frictionNormalMultiplier;
|
normalForce = Common.clamp(pair.separation + normalVelocity, 0, 1) * Resolver._frictionNormalMultiplier;
|
||||||
|
|
Loading…
Reference in a new issue