mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-23 09:26:51 -05:00
improved performance by always reusing collision objects
This commit is contained in:
parent
0add2f00c9
commit
4631768d6f
1 changed files with 23 additions and 10 deletions
|
@ -27,20 +27,26 @@ var SAT = {};
|
||||||
canReusePrevCol = false;
|
canReusePrevCol = false;
|
||||||
|
|
||||||
if (prevCol) {
|
if (prevCol) {
|
||||||
var motion = bodyA.speed * bodyA.speed + bodyA.angularSpeed * bodyA.angularSpeed;
|
// estimate total motion
|
||||||
motion += bodyB.speed * bodyB.speed + bodyB.angularSpeed * bodyB.angularSpeed;
|
var motion = bodyA.speed * bodyA.speed + bodyA.angularSpeed * bodyA.angularSpeed
|
||||||
|
+ bodyB.speed * bodyB.speed + bodyB.angularSpeed * bodyB.angularSpeed;
|
||||||
|
|
||||||
// can reuse if collision was resting
|
// we may be able to (partially) reuse collision result
|
||||||
|
// but only safe if collision was resting
|
||||||
canReusePrevCol = prevCol && prevCol.collided && motion < 0.2;
|
canReusePrevCol = prevCol && prevCol.collided && motion < 0.2;
|
||||||
|
|
||||||
|
// reuse collision object
|
||||||
|
collision = prevCol;
|
||||||
|
} else {
|
||||||
|
collision = { collided: false, bodyA: bodyA, bodyB: bodyB };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevCol && canReusePrevCol) {
|
if (prevCol && canReusePrevCol) {
|
||||||
|
// if we can reuse the collision result
|
||||||
// only need to test the previously found axis
|
// we only need to test the previously found axis
|
||||||
var axes = [prevCol.bodyA.axes[prevCol.axisNumber]];
|
var axes = [prevCol.bodyA.axes[prevCol.axisNumber]];
|
||||||
minOverlap = _overlapAxes(prevCol.bodyA.vertices, prevCol.bodyB.vertices, axes);
|
|
||||||
|
|
||||||
collision = previousCollision;
|
minOverlap = _overlapAxes(prevCol.bodyA.vertices, prevCol.bodyB.vertices, axes);
|
||||||
collision.reused = true;
|
collision.reused = true;
|
||||||
|
|
||||||
if (minOverlap.overlap <= 0) {
|
if (minOverlap.overlap <= 0) {
|
||||||
|
@ -48,26 +54,33 @@ var SAT = {};
|
||||||
return collision;
|
return collision;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
collision = { collided: false, bodyA: bodyA, bodyB: bodyB };
|
// if we can't reuse a result, perform a full SAT test
|
||||||
|
|
||||||
overlapAB = _overlapAxes(bodyA.vertices, bodyB.vertices, bodyA.axes);
|
overlapAB = _overlapAxes(bodyA.vertices, bodyB.vertices, bodyA.axes);
|
||||||
|
|
||||||
if (overlapAB.overlap <= 0)
|
if (overlapAB.overlap <= 0) {
|
||||||
|
collision.collided = false;
|
||||||
return collision;
|
return collision;
|
||||||
|
}
|
||||||
|
|
||||||
overlapBA = _overlapAxes(bodyB.vertices, bodyA.vertices, bodyB.axes);
|
overlapBA = _overlapAxes(bodyB.vertices, bodyA.vertices, bodyB.axes);
|
||||||
|
|
||||||
if (overlapBA.overlap <= 0)
|
if (overlapBA.overlap <= 0) {
|
||||||
|
collision.collided = false;
|
||||||
return collision;
|
return collision;
|
||||||
|
}
|
||||||
|
|
||||||
if (overlapAB.overlap < overlapBA.overlap) {
|
if (overlapAB.overlap < overlapBA.overlap) {
|
||||||
minOverlap = overlapAB;
|
minOverlap = overlapAB;
|
||||||
|
collision.bodyA = bodyA;
|
||||||
|
collision.bodyB = bodyB;
|
||||||
} else {
|
} else {
|
||||||
minOverlap = overlapBA;
|
minOverlap = overlapBA;
|
||||||
collision.bodyA = bodyB;
|
collision.bodyA = bodyB;
|
||||||
collision.bodyB = bodyA;
|
collision.bodyB = bodyA;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// important for reuse later
|
||||||
collision.axisNumber = minOverlap.axisNumber;
|
collision.axisNumber = minOverlap.axisNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue