-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollider.ts
65 lines (51 loc) · 1.61 KB
/
collider.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
import { Contact } from "./contact";
import { Motion, Transform } from "./transform";
import { Vector } from "./vector";
export abstract class Collider {
private static _ID = 0;
public id = Collider._ID++;
public static = false;
public mass = 1;
public bounciness = 0.1;
public friction = .99;
public xf = new Transform();
public m = new Motion();
get inverseMass() {
return this.static ? 0 : 1 / this.mass;
}
get inertia() {
return this.mass;
}
get inverseInertia() {
return this.static ? 0 : 1 / this.inertia;
}
/**
* 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);
}
applyLinearImpulse(impulse: Vector) {
if (this.static) {
return;
}
this.m.vel = this.m.vel.add(impulse.scale(this.inverseMass));
}
applyAngularImpulse(point: Vector, impulse: Vector) {
if (this.static) {
return;
}
const distanceFromCenter = point.sub(this.xf.pos);
this.m.angularVelocity += this.inverseInertia * distanceFromCenter.cross(impulse);
}
abstract collide(collider: Collider, contact?: Contact): Contact | null;
abstract draw(ctx: CanvasRenderingContext2D, flags: any): void;
}