-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.js
38 lines (34 loc) · 792 Bytes
/
physics.js
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
/* global Vector */
var G = new Vector(0, 1);
var friction = 0.97;
function Physics(position, width, height, bounds) {
this.position = position;
this.velocity = new Vector(0, 0);
this.width = width;
this.height = height;
this.bounds = bounds;
}
Physics.prototype.update = function(dt) {
this.position.add(this.velocity);
this.velocity.add(G);
var r = this.bounds.intersects(
this.position,
this.velocity
);
while (r) {
this.position.set(r.intersectPosition);
this.velocity.set(r.reflectionVelocity.mul(friction));
r = this.bounds.intersects(
this.position,
this.velocity,
r.intersectVector
);
}
};
Physics.prototype.getNewPosition = function() {
this.update();
return new Vector(
Math.round(this.position.x),
Math.round(this.position.y)
);
};