Skip to content

Commit 6f8a54b

Browse files
committed
optimised Collision.collides
1 parent 51f49ce commit 6f8a54b

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

src/collision/Collision.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,32 @@ var Pair = require('./Pair');
100100
}
101101

102102
var normal = collision.normal,
103+
tangent = collision.tangent,
104+
penetration = collision.penetration,
103105
supports = collision.supports,
106+
depth = minOverlap.overlap,
104107
minAxis = minOverlap.axis,
105-
minAxisX = minAxis.x,
106-
minAxisY = minAxis.y;
108+
normalX = minAxis.x,
109+
normalY = minAxis.y,
110+
deltaX = bodyB.position.x - bodyA.position.x,
111+
deltaY = bodyB.position.y - bodyA.position.y;
107112

108113
// ensure normal is facing away from bodyA
109-
if (minAxisX * (bodyB.position.x - bodyA.position.x) + minAxisY * (bodyB.position.y - bodyA.position.y) < 0) {
110-
normal.x = minAxisX;
111-
normal.y = minAxisY;
112-
} else {
113-
normal.x = -minAxisX;
114-
normal.y = -minAxisY;
114+
if (normalX * deltaX + normalY * deltaY >= 0) {
115+
normalX = -normalX;
116+
normalY = -normalY;
115117
}
118+
119+
normal.x = normalX;
120+
normal.y = normalY;
116121

117-
collision.tangent.x = -normal.y;
118-
collision.tangent.y = normal.x;
122+
tangent.x = -normalY;
123+
tangent.y = normalX;
119124

120-
collision.depth = minOverlap.overlap;
125+
penetration.x = normalX * depth;
126+
penetration.y = normalY * depth;
121127

122-
collision.penetration.x = normal.x * collision.depth;
123-
collision.penetration.y = normal.y * collision.depth;
128+
collision.depth = depth;
124129

125130
// find support points, there is always either exactly one or two
126131
var supportsB = Collision._findSupports(bodyA, bodyB, normal, 1),

0 commit comments

Comments
 (0)