Skip to content

Commit

Permalink
implemented temporary vector pool
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jan 24, 2015
1 parent b327a40 commit 7a477f2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
40 changes: 25 additions & 15 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ var Resolver = {};
vertex,
vertexCorrected,
normal,
bodyBtoA;
bodyBtoA,
tempA = Vector._temp[0],
tempB = Vector._temp[1],
tempC = Vector._temp[2];

// find impulses required to resolve penetration
for (i = 0; i < pairs.length; i++) {
Expand All @@ -43,9 +46,10 @@ var Resolver = {};
vertexCorrected = collision.supportCorrected;
normal = collision.normal;


// get current separation between body edges involved in collision
bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, vertex),
Vector.add(bodyA.positionImpulse, vertexCorrected));
bodyBtoA = Vector.sub(Vector.add(bodyB.positionImpulse, vertex, tempA),
Vector.add(bodyA.positionImpulse, vertexCorrected, tempB), tempC);

pair.separation = Vector.dot(normal, bodyBtoA);
}
Expand Down Expand Up @@ -110,8 +114,7 @@ var Resolver = {};
* @param {pair[]} pairs
*/
Resolver.preSolveVelocity = function(pairs) {
var impulse = {},
i,
var i,
j,
pair,
contacts,
Expand All @@ -124,7 +127,9 @@ var Resolver = {};
contactVertex,
normalImpulse,
tangentImpulse,
offset;
offset,
impulse = Vector._temp[0],
tempA = Vector._temp[1];

for (i = 0; i < pairs.length; i++) {
pair = pairs[i];
Expand Down Expand Up @@ -152,14 +157,14 @@ var Resolver = {};

// apply impulse from contact
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.y += impulse.y * bodyA.inverseMass;
bodyA.anglePrev += Vector.cross(offset, impulse) * bodyA.inverseInertia;
}

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.y -= impulse.y * bodyB.inverseMass;
bodyB.anglePrev -= Vector.cross(offset, impulse) * bodyB.inverseInertia;
Expand All @@ -174,8 +179,13 @@ var Resolver = {};
* @param {pair[]} pairs
*/
Resolver.solveVelocity = function(pairs, timeScale) {
var impulse = {},
timeScaleSquared = timeScale * timeScale;
var 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++) {
var pair = pairs[i];
Expand Down Expand Up @@ -203,11 +213,11 @@ var Resolver = {};
for (var j = 0; j < contacts.length; j++) {
var contact = contacts[j],
contactVertex = contact.vertex,
offsetA = Vector.sub(contactVertex, bodyA.position),
offsetB = Vector.sub(contactVertex, bodyB.position),
velocityPointA = Vector.add(bodyA.velocity, Vector.mult(Vector.perp(offsetA), bodyA.angularVelocity)),
velocityPointB = Vector.add(bodyB.velocity, Vector.mult(Vector.perp(offsetB), bodyB.angularVelocity)),
relativeVelocity = Vector.sub(velocityPointA, velocityPointB),
offsetA = Vector.sub(contactVertex, bodyA.position, tempA),
offsetB = Vector.sub(contactVertex, bodyB.position, tempB),
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), tempD),
relativeVelocity = Vector.sub(velocityPointA, velocityPointB, tempE),
normalVelocity = Vector.dot(normal, relativeVelocity);

var tangentVelocity = Vector.dot(tangent, relativeVelocity),
Expand Down
6 changes: 3 additions & 3 deletions src/collision/SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ var SAT = {};
* @return result
*/
var _overlapAxes = function(verticesA, verticesB, axes) {
var projectionA = {},
projectionB = {},
var projectionA = Vector._temp[0],
projectionB = Vector._temp[1],
result = { overlap: Number.MAX_VALUE },
overlap,
axis;
Expand Down Expand Up @@ -213,7 +213,7 @@ var SAT = {};
*/
var _findSupports = function(bodyA, bodyB, normal) {
var nearestDistance = Number.MAX_VALUE,
vertexToBody = { x: 0, y: 0 },
vertexToBody = Vector._temp[0],
vertices = bodyB.vertices,
bodyAPosition = bodyA.position,
distance,
Expand Down
26 changes: 22 additions & 4 deletions src/geometry/Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,29 @@ var Vector = {};
* @method add
* @param {vector} vectorA
* @param {vector} vectorB
* @param {vector} [output]
* @return {vector} A new vector of vectorA and vectorB added
*/
Vector.add = function(vectorA, vectorB) {
return { x: vectorA.x + vectorB.x, y: vectorA.y + vectorB.y };
Vector.add = function(vectorA, vectorB, output) {
if (!output) output = {};
output.x = vectorA.x + vectorB.x;
output.y = vectorA.y + vectorB.y;
return output;
};

/**
* Subtracts the two vectors.
* @method sub
* @param {vector} vectorA
* @param {vector} vectorB
* @param {vector} [output]
* @return {vector} A new vector of vectorA and vectorB subtracted
*/
Vector.sub = function(vectorA, vectorB) {
return { x: vectorA.x - vectorB.x, y: vectorA.y - vectorB.y };
Vector.sub = function(vectorA, vectorB, output) {
if (!output) output = {};
output.x = vectorA.x - vectorB.x;
output.y = vectorA.y - vectorB.y;
return output;
};

/**
Expand Down Expand Up @@ -199,4 +207,14 @@ var Vector = {};
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()];

})();

0 comments on commit 7a477f2

Please sign in to comment.