0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -05:00

implemented temporary vector pool

This commit is contained in:
liabru 2015-01-24 19:48:27 +00:00
parent b327a40e9b
commit 7a477f2088
3 changed files with 50 additions and 22 deletions

View file

@ -27,7 +27,10 @@ var Resolver = {};
vertex, vertex,
vertexCorrected, vertexCorrected,
normal, normal,
bodyBtoA; bodyBtoA,
tempA = Vector._temp[0],
tempB = Vector._temp[1],
tempC = Vector._temp[2];
// find impulses required to resolve penetration // find impulses required to resolve penetration
for (i = 0; i < pairs.length; i++) { for (i = 0; i < pairs.length; i++) {
@ -43,9 +46,10 @@ var Resolver = {};
vertexCorrected = collision.supportCorrected; vertexCorrected = collision.supportCorrected;
normal = collision.normal; normal = collision.normal;
// get current separation between body edges involved in collision // get current separation between body edges involved in collision
bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, vertex), bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, vertex, tempA),
Vector.add(bodyA.positionImpulse, vertexCorrected)); Vector.add(bodyA.positionImpulse, vertexCorrected, tempB), tempC);
pair.separation = Vector.dot(normal, bodyBtoA); pair.separation = Vector.dot(normal, bodyBtoA);
} }
@ -110,8 +114,7 @@ var Resolver = {};
* @param {pair[]} pairs * @param {pair[]} pairs
*/ */
Resolver.preSolveVelocity = function(pairs) { Resolver.preSolveVelocity = function(pairs) {
var impulse = {}, var i,
i,
j, j,
pair, pair,
contacts, contacts,
@ -124,7 +127,9 @@ var Resolver = {};
contactVertex, contactVertex,
normalImpulse, normalImpulse,
tangentImpulse, tangentImpulse,
offset; offset,
impulse = Vector._temp[0],
tempA = Vector._temp[1];
for (i = 0; i < pairs.length; i++) { for (i = 0; i < pairs.length; i++) {
pair = pairs[i]; pair = pairs[i];
@ -152,14 +157,14 @@ var Resolver = {};
// apply impulse from contact // apply impulse from contact
if (!(bodyA.isStatic || bodyA.isSleeping)) { if (!(bodyA.isStatic || bodyA.isSleeping)) {
offset = Vector.sub(contactVertex, bodyA.position); offset = Vector.sub(contactVertex, bodyA.position, tempA);
bodyA.positionPrev.x += impulse.x * bodyA.inverseMass; bodyA.positionPrev.x += impulse.x * bodyA.inverseMass;
bodyA.positionPrev.y += impulse.y * bodyA.inverseMass; bodyA.positionPrev.y += impulse.y * bodyA.inverseMass;
bodyA.anglePrev += Vector.cross(offset, impulse) * bodyA.inverseInertia; bodyA.anglePrev += Vector.cross(offset, impulse) * bodyA.inverseInertia;
} }
if (!(bodyB.isStatic || bodyB.isSleeping)) { if (!(bodyB.isStatic || bodyB.isSleeping)) {
offset = Vector.sub(contactVertex, bodyB.position); offset = Vector.sub(contactVertex, bodyB.position, tempA);
bodyB.positionPrev.x -= impulse.x * bodyB.inverseMass; bodyB.positionPrev.x -= impulse.x * bodyB.inverseMass;
bodyB.positionPrev.y -= impulse.y * bodyB.inverseMass; bodyB.positionPrev.y -= impulse.y * bodyB.inverseMass;
bodyB.anglePrev -= Vector.cross(offset, impulse) * bodyB.inverseInertia; bodyB.anglePrev -= Vector.cross(offset, impulse) * bodyB.inverseInertia;
@ -174,8 +179,13 @@ var Resolver = {};
* @param {pair[]} pairs * @param {pair[]} pairs
*/ */
Resolver.solveVelocity = function(pairs, timeScale) { Resolver.solveVelocity = function(pairs, timeScale) {
var impulse = {}, var timeScaleSquared = timeScale * timeScale,
timeScaleSquared = timeScale * timeScale; impulse = Vector._temp[0],
tempA = Vector._temp[1],
tempB = Vector._temp[2],
tempC = Vector._temp[3],
tempD = Vector._temp[4],
tempE = Vector._temp[5];
for (var i = 0; i < pairs.length; i++) { for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i]; var pair = pairs[i];
@ -203,11 +213,11 @@ var Resolver = {};
for (var j = 0; j < contacts.length; j++) { for (var j = 0; j < contacts.length; j++) {
var contact = contacts[j], var contact = contacts[j],
contactVertex = contact.vertex, contactVertex = contact.vertex,
offsetA = Vector.sub(contactVertex, bodyA.position), offsetA = Vector.sub(contactVertex, bodyA.position, tempA),
offsetB = Vector.sub(contactVertex, bodyB.position), offsetB = Vector.sub(contactVertex, bodyB.position, tempB),
velocityPointA = Vector.add(bodyA.velocity, Vector.mult(Vector.perp(offsetA), bodyA.angularVelocity)), velocityPointA = Vector.add(bodyA.velocity, Vector.mult(Vector.perp(offsetA), bodyA.angularVelocity), tempC),
velocityPointB = Vector.add(bodyB.velocity, Vector.mult(Vector.perp(offsetB), bodyB.angularVelocity)), velocityPointB = Vector.add(bodyB.velocity, Vector.mult(Vector.perp(offsetB), bodyB.angularVelocity), tempD),
relativeVelocity = Vector.sub(velocityPointA, velocityPointB), relativeVelocity = Vector.sub(velocityPointA, velocityPointB, tempE),
normalVelocity = Vector.dot(normal, relativeVelocity); normalVelocity = Vector.dot(normal, relativeVelocity);
var tangentVelocity = Vector.dot(tangent, relativeVelocity), var tangentVelocity = Vector.dot(tangent, relativeVelocity),

View file

@ -145,8 +145,8 @@ var SAT = {};
* @return result * @return result
*/ */
var _overlapAxes = function(verticesA, verticesB, axes) { var _overlapAxes = function(verticesA, verticesB, axes) {
var projectionA = {}, var projectionA = Vector._temp[0],
projectionB = {}, projectionB = Vector._temp[1],
result = { overlap: Number.MAX_VALUE }, result = { overlap: Number.MAX_VALUE },
overlap, overlap,
axis; axis;
@ -213,7 +213,7 @@ var SAT = {};
*/ */
var _findSupports = function(bodyA, bodyB, normal) { var _findSupports = function(bodyA, bodyB, normal) {
var nearestDistance = Number.MAX_VALUE, var nearestDistance = Number.MAX_VALUE,
vertexToBody = { x: 0, y: 0 }, vertexToBody = Vector._temp[0],
vertices = bodyB.vertices, vertices = bodyB.vertices,
bodyAPosition = bodyA.position, bodyAPosition = bodyA.position,
distance, distance,

View file

@ -127,10 +127,14 @@ var Vector = {};
* @method add * @method add
* @param {vector} vectorA * @param {vector} vectorA
* @param {vector} vectorB * @param {vector} vectorB
* @param {vector} [output]
* @return {vector} A new vector of vectorA and vectorB added * @return {vector} A new vector of vectorA and vectorB added
*/ */
Vector.add = function(vectorA, vectorB) { Vector.add = function(vectorA, vectorB, output) {
return { x: vectorA.x + vectorB.x, y: vectorA.y + vectorB.y }; if (!output) output = {};
output.x = vectorA.x + vectorB.x;
output.y = vectorA.y + vectorB.y;
return output;
}; };
/** /**
@ -138,10 +142,14 @@ var Vector = {};
* @method sub * @method sub
* @param {vector} vectorA * @param {vector} vectorA
* @param {vector} vectorB * @param {vector} vectorB
* @param {vector} [output]
* @return {vector} A new vector of vectorA and vectorB subtracted * @return {vector} A new vector of vectorA and vectorB subtracted
*/ */
Vector.sub = function(vectorA, vectorB) { Vector.sub = function(vectorA, vectorB, output) {
return { x: vectorA.x - vectorB.x, y: vectorA.y - vectorB.y }; if (!output) output = {};
output.x = vectorA.x - vectorB.x;
output.y = vectorA.y - vectorB.y;
return output;
}; };
/** /**
@ -199,4 +207,14 @@ var Vector = {};
return Math.atan2(vectorB.y - vectorA.y, vectorB.x - vectorA.x); return Math.atan2(vectorB.y - vectorA.y, vectorB.x - vectorA.x);
}; };
/**
* Temporary vector pool (not thread-safe).
* @property _temp
* @type {vector[]}
* @private
*/
Vector._temp = [Vector.create(), Vector.create(),
Vector.create(), Vector.create(),
Vector.create(), Vector.create()];
})(); })();