-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseparating-axis.ts
314 lines (267 loc) · 10.7 KB
/
separating-axis.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { Box } from "./box";
import { Circle } from "./circle";
import { Collider } from "./collider";
import { Contact } from "./contact";
import { Vector } from "./vector";
export interface ContactInfo {
/**
* Collider A
*/
collider: Collider,
/**
* Signed value (negative means overlap, positive no overlap)
*/
separation: number,
/**
* Axis of separation from the collider's perpective
*/
axis: Vector,
/**
* Side of separation (reference) from the collider's perpsective
*/
side?: [Vector, Vector],
/**
* Local side of separation (reference) from the collider's perspective
*/
localSide?: [Vector, Vector],
/**
* Index of the separation side (reference) from the collider's perspective
*/
sideId?: number,
/**
* Point on collider B (incident point)
*/
point: Vector;
/**
* Local point on collider B (incident point)
*/
localPoint?: Vector;
}
export class SeparatingAxis {
static findBoxBoxSeparation(boxA: Box, boxB: Box): ContactInfo {
let bestSeparation = -Number.MAX_VALUE;
let bestSide: [Vector, Vector] | null = null;
let bestAxis: Vector | null = null;
let bestSideIndex: number = -1;
let bestOtherPoint: Vector | null = null;
for (let i = 0; i < 4; i++){
const side = boxA.getSide(i);
const axis = side[1].sub(side[0]).normal();
const vertB = boxB.getFurthestPoint(axis.negate());
// Separation on side i's axis
const vertSeparation = SeparatingAxis.distanceToPoint(side[0], side[1], vertB, true);
if (vertSeparation > bestSeparation) {
bestSeparation = vertSeparation;
bestSide = side;
bestAxis = axis;
bestSideIndex = i;
bestOtherPoint = vertB
}
}
return {
collider: boxA,
separation: bestSeparation,
axis: bestAxis as Vector,
side: bestSide as [Vector, Vector],
localSide: boxA.getLocalSide(bestSideIndex),
sideId: bestSideIndex,
point: bestOtherPoint as Vector,
localPoint: boxB.getFurthestLocalPoint(bestAxis!.negate())
}
}
static findBoxBoxContact(boxA: Box, boxB: Box): Contact | null {
const separationA = SeparatingAxis.findBoxBoxSeparation(boxA, boxB);
// If there is no overlap from boxA's perspective we can end early
if (separationA.separation > 0) {
return null;
}
const separationB = SeparatingAxis.findBoxBoxSeparation(boxB, boxA);
// If there is no overlap from boxB's perspective exit now
if (separationB.separation > 0) {
return null;
}
// Separations are both negative, we want to pick the least negative (minimal movement)
const separation = separationA.separation > separationB.separation ? separationA : separationB;
// The incident side is the most opposite from the axes of collision on the other shape
const other = separation.collider === boxA ? boxB : boxA;
const incident = other.findSide(separation.axis.negate());
// Clip incident side by the perpendicular lines at each end of the reference side
// https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
const reference = separation.side as [Vector, Vector];
const refDir = reference[1].sub(reference[0]).normalize();
// Find our contact points by clipping the incident by the collision side
const clipRight = SeparatingAxis._clip(incident[0], incident[1], refDir.negate(), -refDir.dot(reference[0]));
let clipLeft: [Vector, Vector] | null = null;
if (clipRight) {
clipLeft = SeparatingAxis._clip(clipRight[0], clipRight[1], refDir, refDir.dot(reference[1]));
}
if (clipLeft) {
// We only want clip points below the reference edge, discard the others
const points = clipLeft.filter(p => {
return SeparatingAxis._below(reference[0], reference[1], p);
});
let normal = separation.axis;
let tangent = normal.perpendicular();
// Point Contact A -> B
if (boxB.xf.pos.sub(boxA.xf.pos).dot(normal) < 0) {
normal = normal.negate();
tangent = normal.perpendicular();
}
// Points are clipped from incident which is the other collider
// Store those as locals
let localPoints: Vector[] = [];
if (separation.collider === boxA) {
localPoints = points.map(p => boxB.xf.inverse(p));
} else {
localPoints = points.map(p => boxA.xf.inverse(p));
}
return new Contact(boxA, boxB, normal, normal.perpendicular(), separation, points, localPoints);
}
return null;
}
static findBoxCircleSeparation(box: Box, circle: Circle): ContactInfo {
const circleDir = circle.xf.pos.sub(box.xf.pos).normalize();
const boxDir = box.xf.pos.sub(circle.xf.pos).normalize();
const circlePoint = circle.xf.pos.add(boxDir.scale(circle.radius));
let bestSeparation = -Number.MAX_VALUE;
let bestSide: [Vector, Vector] | null = null;
let bestAxis: Vector | null = null;
let bestSideIndex: number = -1;
let bestOtherPoint: Vector | null = null;
// Test poly axes against circle point
for (let i = 0; i < 4; i++){
const side = box.getSide(i);
const axis = side[1].sub(side[0]).normal();
const circlePoint = circle.xf.pos.add(axis.negate().scale(circle.radius));
// Separation on side i's axis
const vertSeparation = SeparatingAxis.distanceToPoint(side[0], side[1], circlePoint, true);
if (vertSeparation > bestSeparation) {
bestSeparation = vertSeparation;
bestSide = side;
bestAxis = axis;
bestSideIndex = i;
bestOtherPoint = circlePoint
}
}
// Test the circle -> box axis against each point of the box
let minCircleSeparation = Number.MAX_VALUE;
let minCircleSide = 0;
for (let i = 0; i < 4; i++) {
// project box points on the circle axis
const projection = circleDir.dot(box.points[i]);
const minCircle = circleDir.dot(circle.xf.pos) - circle.radius;
const maxCircle = circleDir.dot(circle.xf.pos) + circle.radius;
const separation = Math.min(minCircle - projection, maxCircle - projection);
if (separation < minCircleSeparation) {
minCircleSeparation = separation;
minCircleSide = i;
}
}
if (minCircleSeparation > bestSeparation) {
let boxPt = box.getFurthestPoint(circleDir);
bestSeparation = minCircleSeparation;
bestSide = box.getSide(minCircleSide);
bestAxis = circle.xf.pos.sub(boxPt).normalize();
bestSideIndex = minCircleSide;
bestOtherPoint = box.getFurthestPoint(circleDir);
}
return {
collider: box,
separation: bestSeparation,
axis: bestAxis as Vector,
side: bestSide as [Vector, Vector],
sideId: bestSideIndex,
point: bestOtherPoint as Vector
}
}
static findCircleBoxContact(circle: Circle, box: Box): Contact | null {
let separation = SeparatingAxis.findBoxCircleSeparation(box, circle);
if (separation.separation > 0) {
return null;
}
// make sure that the minAxis is pointing away from circle
let boxDir = box.xf.pos.sub(circle.xf.pos);
let axis = separation.axis;
axis = axis.dot(boxDir) < 0 ? axis.negate() : axis;
const point = circle.getFurthestPoint(axis);
const normal = axis;
return new Contact(
circle,
box,
normal,
normal.perpendicular(),
separation,
[point],
[circle.xf.inverse(point)]
);
}
static findCircleCircleContact(circleA: Circle, circleB: Circle): Contact | null {
const combinedRadius = circleB.radius + circleA.radius;
const distance = circleB.xf.pos.distance(circleA.xf.pos);
if (distance < combinedRadius) {
const separation = combinedRadius - distance;
// normal points from A -> B
const direction = circleB.xf.pos.sub(circleA.xf.pos);
const normal = direction.normalize();
const tangent = normal.perpendicular();
const point = circleA.xf.pos.add(normal.scale(circleA.radius));
const info: ContactInfo = {
collider: circleA,
separation: separation,
axis: normal,
point: point
}
return new Contact(circleA, circleB, normal, tangent, info, [point]);
}
return null;
}
/**
* Find the perpendicular distance from the line to a point
* https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
* @param point
*/
static distanceToPoint(begin: Vector, end: Vector, point: Vector, signed: boolean = false) {
const x0 = point.x;
const y0 = point.y;
const l = end.distance(begin);
const dy = end.y - begin.y;
const dx = end.x - begin.x;
const distance = (dy * x0 - dx * y0 + end.x * begin.y - end.y * begin.x) / l;
return signed ? distance : Math.abs(distance);
}
/**
* Clips along a line returning a new line given a direction and a length
* ```
* Clip Dir +--------> Clip Size
* Begin *---------|-------------* End
* Clipped line *---------*
* ```
*
**/
private static _clip(begin: Vector, end: Vector, clipDir: Vector, size: number): [Vector, Vector] | null {
let dir = clipDir;
dir = dir.normalize();
const near = dir.dot(begin) - size;
const far = dir.dot(end) - size;
let results = [];
if (near <= 0) {
results.push(begin);
}
if (far <= 0) {
results.push(end);
}
if (near * far < 0) {
const clipTime = near / (near - far);
results.push(begin.add(end.sub(begin).scale(clipTime)));
}
if (results.length !== 2) {
return null;
}
return [results[0], results[1]];
}
private static _below(begin: Vector, end: Vector, point: Vector): boolean {
let above2 = ((end.x - begin.x) * (point.y - begin.y) -
(end.y - begin.y) * (point.x - begin.x))
return above2 >= 0;
}
}