mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
implemented temporary vector pool
This commit is contained in:
parent
b327a40e9b
commit
7a477f2088
3 changed files with 50 additions and 22 deletions
|
@ -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++) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -110,8 +114,7 @@ var Resolver = {};
|
|||
* @param {pair[]} pairs
|
||||
*/
|
||||
Resolver.preSolveVelocity = function(pairs) {
|
||||
var impulse = {},
|
||||
i,
|
||||
var i,
|
||||
j,
|
||||
pair,
|
||||
contacts,
|
||||
|
@ -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];
|
||||
|
@ -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;
|
||||
|
@ -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];
|
||||
|
@ -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),
|
||||
|
|
|
@ -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;
|
||||
|
@ -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,
|
||||
|
|
|
@ -127,10 +127,14 @@ 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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -138,10 +142,14 @@ var Vector = {};
|
|||
* @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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -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()];
|
||||
|
||||
})();
|
Loading…
Reference in a new issue