/
line.ts
78 lines (66 loc) · 1.46 KB
/
line.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
import { Vector as SATVector } from "sat";
import { BodyOptions, BodyType, Vector } from "../model";
import { Polygon } from "./polygon";
/**
* collider - line
*/
export class Line extends Polygon {
/**
* line type
*/
readonly type: BodyType.Line = BodyType.Line;
/**
* line is convex
*/
readonly isConvex = true;
/**
* collider - line from start to end
*/
constructor(start: Vector, end: Vector, options?: BodyOptions) {
super(
start,
[
{ x: 0, y: 0 },
{ x: end.x - start.x, y: end.y - start.y },
],
options,
);
if (this.calcPoints.length === 1 || !end) {
console.error({ start, end });
throw new Error("No end point for line provided");
}
}
get start(): Vector {
return {
x: this.x + this.calcPoints[0].x,
y: this.y + this.calcPoints[0].y,
};
}
set start({ x, y }: Vector) {
this.x = x;
this.y = y;
}
get end(): Vector {
return {
x: this.x + this.calcPoints[1].x,
y: this.y + this.calcPoints[1].y,
};
}
set end({ x, y }: Vector) {
this.points[1].x = x - this.start.x;
this.points[1].y = y - this.start.y;
this.setPoints(this.points);
}
getCentroid(): SATVector {
return new SATVector(
(this.end.x - this.start.x) / 2,
(this.end.y - this.start.y) / 2,
);
}
/**
* do not attempt to use Polygon.updateIsConvex()
*/
protected updateIsConvex(): void {
return;
}
}