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
|
import { Collision } from '../model/data/collision';
import { Circle } from '../math/circle';
import { Collidable } from '../math/collision';
import { LineSegment, Movement } from '../math/line';
import { Point } from '../math/point';
import { vec2 } from 'gl-matrix';
export function mkCollision(collision: Collision[]): Collidable[] {
const ret: Collidable[] = [];
for (const c of collision) {
switch (c.type) {
case 'polygon':
if (!c.vertices.length)
continue;
let prev = c.vertices[c.vertices.length - 1];
for (const v of c.vertices) {
ret.push(LineSegment.fromPoints(vec2.clone(prev), vec2.clone(v)));
prev = v;
}
for (const v of c.vertices) {
ret.push(new Point(vec2.clone(v)));
prev = v;
}
break;
case 'circle':
ret.push(new Circle(vec2.clone(c.center), c.radius));
break;
}
}
return ret;
}
export interface CollidableGroup {
getTranslation(): vec2|null;
getCollidables(): Collidable[];
}
export function collide(collision: CollidableGroup, out: vec2, move: Movement, radius: number): boolean {
const t = collision.getTranslation();
if (t)
move = move.translate(vec2.negate(vec2.create(), t));
for (const c of collision.getCollidables()) {
if (!c.collide(out, move, radius))
continue;
if (vec2.squaredDistance(move.src, out) >= vec2.squaredDistance(move.src, move.dest))
continue;
if (t)
vec2.add(out, out, t);
return true;
}
return false;
}
|