-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircle.ts
101 lines (85 loc) · 2.66 KB
/
circle.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
import { Box } from "./box";
import { Collider } from "./collider";
import { Contact } from "./contact";
import { Line } from "./line";
import { SeparatingAxis } from "./separating-axis";
import { Vector } from "./vector";
export class Circle extends Collider {
constructor(public radius: number, pos: Vector) {
super();
this.xf.pos = pos;
this.mass = 1;
}
get inverseMass() {
return this.static ? 0 : 1 / this.mass;
}
get inertia() {
return (this.mass * this.radius * this.radius) / 2;
}
get inverseInertia() {
return this.static ? 0 : 1 / this.inertia;
}
/**
* Returns a contact in the direction from `this` -> `other`
* @param other
* @returns
*/
collide(other: Circle | Line | Box, contact?: Contact): Contact | null {
if (other instanceof Circle) {
return SeparatingAxis.findCircleCircleContact(this, other);
}
if (other instanceof Box) {
return SeparatingAxis.findCircleBoxContact(this, other)
}
if (other instanceof Line) {
return other.collide(this);
}
return null;
}
/**
* Apply impulse at a point
* @param point
* @param impulse
* @returns
*/
applyImpulse(point: Vector, impulse: Vector) {
if (this.static) {
return;
}
const distanceFromCenter = point.sub(this.xf.pos);
this.m.vel = this.m.vel.add(impulse.scale(this.inverseMass));
this.m.angularVelocity += this.inverseInertia * distanceFromCenter.cross(impulse);
}
/**
* Find the point on the shape furthest in the direction specified
*/
getFurthestPoint(direction: Vector): Vector {
const dir = direction.normalize();
return this.xf.pos.add(dir.scale(this.radius));
}
getFurthestLocalPoint(direction: Vector): Vector {
const dir = direction.normalize();
return dir.scale(this.radius);
}
draw(ctx: CanvasRenderingContext2D, flags?: any) {
ctx.beginPath();
ctx.fillStyle = 'blue';
ctx.arc(this.xf.pos.x, this.xf.pos.y, this.radius, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
if (flags["Debug"]) {
ctx.fillStyle = 'yellow';
ctx.fillText('id: ' + this.id, this.xf.pos.x, this.xf.pos.y);
}
ctx.save();
ctx.translate(this.xf.pos.x, this.xf.pos.y);
ctx.rotate(this.xf.rotation);
ctx.beginPath()
ctx.strokeStyle = 'black';
ctx.moveTo(0, 0);
ctx.lineTo(0 + this.radius, 0);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
}