Skip to content

Commit 84d9f59

Browse files
committed
improved collision detection for compounds
1 parent 5ab2bf3 commit 84d9f59

3 files changed

Lines changed: 34 additions & 20 deletions

File tree

src/collision/SAT.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ var SAT = {};
2828

2929
if (prevCol) {
3030
// estimate total motion
31-
var motion = bodyA.speed * bodyA.speed + bodyA.angularSpeed * bodyA.angularSpeed
32-
+ bodyB.speed * bodyB.speed + bodyB.angularSpeed * bodyB.angularSpeed;
31+
var parentA = bodyA.parent,
32+
parentB = bodyB.parent,
33+
motion = parentA.speed * parentA.speed + parentA.angularSpeed * parentA.angularSpeed
34+
+ parentB.speed * parentB.speed + parentB.angularSpeed * parentB.angularSpeed;
3335

3436
// we may be able to (partially) reuse collision result
3537
// but only safe if collision was resting
@@ -113,7 +115,7 @@ var SAT = {};
113115
if (Vertices.contains(bodyA.vertices, verticesB[0]))
114116
supports.push(verticesB[0]);
115117

116-
if (Vertices.contains(bodyA.vertices, verticesB[1]))
118+
if (verticesB[1] && Vertices.contains(bodyA.vertices, verticesB[1]))
117119
supports.push(verticesB[1]);
118120

119121
// find the supports from bodyA that are inside bodyB
@@ -123,12 +125,12 @@ var SAT = {};
123125
if (Vertices.contains(bodyB.vertices, verticesA[0]))
124126
supports.push(verticesA[0]);
125127

126-
if (supports.length < 2 && Vertices.contains(bodyB.vertices, verticesA[1]))
128+
if (verticesA[1] && supports.length < 2 && Vertices.contains(bodyB.vertices, verticesA[1]))
127129
supports.push(verticesA[1]);
128130
}
129131

130132
// account for the edge case of overlapping but no vertex containment
131-
if (supports.length < 2)
133+
if (supports.length < 1)
132134
supports = [verticesB[0]];
133135

134136
collision.supports = supports;
@@ -220,8 +222,8 @@ var SAT = {};
220222
bodyAPosition = bodyA.position,
221223
distance,
222224
vertex,
223-
vertexA = vertices[0],
224-
vertexB = vertices[1];
225+
vertexA,
226+
vertexB;
225227

226228
// find closest vertex on bodyB
227229
for (var i = 0; i < vertices.length; i++) {
@@ -244,15 +246,21 @@ var SAT = {};
244246
nearestDistance = -Vector.dot(normal, vertexToBody);
245247
vertexB = vertex;
246248

247-
var nextIndex = (vertexA.index + 1) % vertices.length;
248-
vertex = vertices[nextIndex];
249-
vertexToBody.x = vertex.x - bodyAPosition.x;
250-
vertexToBody.y = vertex.y - bodyAPosition.y;
251-
distance = -Vector.dot(normal, vertexToBody);
252-
if (distance < nearestDistance) {
253-
vertexB = vertex;
249+
// if the closest vertex is internal, we can't use the next connected vertex
250+
if (!vertexA.isInternal) {
251+
var nextIndex = (vertexA.index + 1) % vertices.length;
252+
vertex = vertices[nextIndex];
253+
vertexToBody.x = vertex.x - bodyAPosition.x;
254+
vertexToBody.y = vertex.y - bodyAPosition.y;
255+
distance = -Vector.dot(normal, vertexToBody);
256+
if (distance < nearestDistance) {
257+
vertexB = vertex;
258+
}
254259
}
255260

261+
if (!vertexB)
262+
return [vertexA];
263+
256264
return [vertexA, vertexB];
257265
};
258266

src/geometry/Axes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ var Axes = {};
1919

2020
// find the unique axes, using edge normal gradients
2121
for (var i = 0; i < vertices.length; i++) {
22+
// skip internal edges
23+
if (vertices[i].isInternal) {
24+
continue;
25+
}
26+
2227
var j = (i + 1) % vertices.length,
2328
normal = Vector.normalise({
2429
x: vertices[j].y - vertices[i].y,

src/geometry/Vertices.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ var Vertices = {};
3535

3636
for (var i = 0; i < points.length; i++) {
3737
var point = points[i],
38-
vertex = {};
39-
40-
vertex.x = point.x;
41-
vertex.y = point.y;
42-
vertex.index = i;
43-
vertex.body = body;
38+
vertex = {
39+
x: point.x,
40+
y: point.y,
41+
index: i,
42+
body: body,
43+
isInternal: false
44+
};
4445

4546
vertices.push(vertex);
4647
}

0 commit comments

Comments
 (0)