/
CollisionJumpTable.ts
214 lines (176 loc) · 6.7 KB
/
CollisionJumpTable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { Circle } from './Circle';
import { CollisionContact } from './CollisionContact';
import { ConvexPolygon } from './ConvexPolygon';
import { Edge } from './Edge';
import { Vector } from '../Algebra';
export const CollisionJumpTable = {
CollideCircleCircle(circleA: Circle, circleB: Circle): CollisionContact {
const radius = circleA.radius + circleB.radius;
const circleAPos = circleA.worldPos;
const circleBPos = circleB.worldPos;
if (circleAPos.distance(circleBPos) > radius) {
return null;
}
const axisOfCollision = circleBPos.sub(circleAPos).normalize();
const mvt = axisOfCollision.scale(radius - circleBPos.distance(circleAPos));
const pointOfCollision = circleA.getFurthestPoint(axisOfCollision);
return new CollisionContact(circleA.collider, circleB.collider, mvt, pointOfCollision, axisOfCollision);
},
CollideCirclePolygon(circle: Circle, polygon: ConvexPolygon): CollisionContact {
let minAxis = circle.testSeparatingAxisTheorem(polygon);
if (!minAxis) {
return null;
}
// make sure that the minAxis is pointing away from circle
const samedir = minAxis.dot(polygon.center.sub(circle.center));
minAxis = samedir < 0 ? minAxis.negate() : minAxis;
const verts: Vector[] = [];
const point1 = polygon.getFurthestPoint(minAxis.negate());
const point2 = circle.getFurthestPoint(minAxis); //.add(cc);
if (circle.contains(point1)) {
verts.push(point1);
}
if (polygon.contains(point2)) {
verts.push(point2);
}
if (verts.length === 0) {
return null;
}
return new CollisionContact(
circle.collider,
polygon.collider,
minAxis,
verts.length === 2 ? verts[0].average(verts[1]) : verts[0],
minAxis.normalize()
);
},
CollideCircleEdge(circle: Circle, edge: Edge): CollisionContact {
// center of the circle
const cc = circle.center;
// vector in the direction of the edge
const e = edge.end.sub(edge.begin);
// amount of overlap with the circle's center along the edge direction
const u = e.dot(edge.end.sub(cc));
const v = e.dot(cc.sub(edge.begin));
// Potential region A collision (circle is on the left side of the edge, before the beginning)
if (v <= 0) {
const da = edge.begin.sub(cc);
const dda = da.dot(da); // quick and dirty way of calc'n distance in r^2 terms saves some sqrts
// save some sqrts
if (dda > circle.radius * circle.radius) {
return null; // no collision
}
return new CollisionContact(
circle.collider,
edge.collider,
da.normalize().scale(circle.radius - Math.sqrt(dda)),
edge.begin,
da.normalize()
);
}
// Potential region B collision (circle is on the right side of the edge, after the end)
if (u <= 0) {
const db = edge.end.sub(cc);
const ddb = db.dot(db);
if (ddb > circle.radius * circle.radius) {
return null;
}
return new CollisionContact(
circle.collider,
edge.collider,
db.normalize().scale(circle.radius - Math.sqrt(ddb)),
edge.end,
db.normalize()
);
}
// Otherwise potential region AB collision (circle is in the middle of the edge between the beginning and end)
const den = e.dot(e);
const pointOnEdge = edge.begin
.scale(u)
.add(edge.end.scale(v))
.scale(1 / den);
const d = cc.sub(pointOnEdge);
const dd = d.dot(d);
if (dd > circle.radius * circle.radius) {
return null; // no collision
}
let n = e.perpendicular();
// flip correct direction
if (n.dot(cc.sub(edge.begin)) < 0) {
n.x = -n.x;
n.y = -n.y;
}
n = n.normalize();
const mvt = n.scale(Math.abs(circle.radius - Math.sqrt(dd)));
return new CollisionContact(circle.collider, edge.collider, mvt.negate(), pointOnEdge, n.negate());
},
CollideEdgeEdge(): CollisionContact {
// Edge-edge collision doesn't make sense
return null;
},
CollidePolygonEdge(polygon: ConvexPolygon, edge: Edge): CollisionContact {
// 3 cases:
// (1) Polygon lands on the full face
// (2) Polygon lands on the right point
// (3) Polygon lands on the left point
const e = edge.end.sub(edge.begin);
let edgeNormal = e.normal();
if (polygon.contains(edge.begin)) {
const { distance: mtv, face } = polygon.getClosestFace(edge.begin);
if (mtv) {
return new CollisionContact(polygon.collider, edge.collider, mtv.negate(), edge.begin.add(mtv.negate()), face.normal().negate());
}
}
if (polygon.contains(edge.end)) {
const { distance: mtv, face } = polygon.getClosestFace(edge.end);
if (mtv) {
return new CollisionContact(polygon.collider, edge.collider, mtv.negate(), edge.end.add(mtv.negate()), face.normal().negate());
}
}
const pc = polygon.center;
const ec = edge.center;
const dir = ec.sub(pc).normalize();
// build a temporary polygon from the edge to use SAT
const linePoly = new ConvexPolygon({
collider: edge.collider,
points: [edge.begin, edge.end, edge.end.add(dir.scale(30)), edge.begin.add(dir.scale(30))]
});
let minAxis = polygon.testSeparatingAxisTheorem(linePoly);
// no minAxis, no overlap, no collision
if (!minAxis) {
return null;
}
// flip the normal and axis to always have positive collisions
edgeNormal = edgeNormal.dot(dir) < 0 ? edgeNormal.negate() : edgeNormal;
minAxis = minAxis.dot(dir) < 0 ? minAxis.negate() : minAxis;
return new CollisionContact(polygon.collider, edge.collider, minAxis, polygon.getFurthestPoint(edgeNormal), edgeNormal);
},
CollidePolygonPolygon(polyA: ConvexPolygon, polyB: ConvexPolygon): CollisionContact {
// do a SAT test to find a min axis if it exists
let minAxis = polyA.testSeparatingAxisTheorem(polyB);
// no overlap, no collision return null
if (!minAxis) {
return null;
}
// make sure that minAxis is pointing from A -> B
const sameDir = minAxis.dot(polyB.center.sub(polyA.center));
minAxis = sameDir < 0 ? minAxis.negate() : minAxis;
// find rough point of collision
// todo this could be better
const verts: Vector[] = [];
const pointA = polyA.getFurthestPoint(minAxis);
const pointB = polyB.getFurthestPoint(minAxis.negate());
if (polyB.contains(pointA)) {
verts.push(pointA);
}
if (polyA.contains(pointB)) {
verts.push(pointB);
}
// no candidates, pick something
if (verts.length === 0) {
verts.push(pointB);
}
const contact = verts.length === 2 ? verts[0].add(verts[1]).scale(0.5) : verts[0];
return new CollisionContact(polyA.collider, polyB.collider, minAxis, contact, minAxis.normalize());
}
};