-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.js
67 lines (56 loc) · 2.22 KB
/
point.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
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
(function () {
"use strict";
function Point(x, y) {
this.pos = new phys.Vector(x, y);
this.vel = phys.Vector.zero();
this.highlighted = false;
this.dragged = false;
}
Point.radius = 10;
Point.friction = 0.01;
Point.bounceResistance = 0.5;
Point.airResistance = 1.025;
Point.mass = 0.2;
Point.prototype.update = function (context, adjust) {
if (!this.dragged) {
this.pos = this.pos.add(this.vel.multiply(adjust));
this.vel = this.vel.add(new phys.Vector(0, Point.mass).multiply(adjust));
this.vel = this.vel.divide(Point.airResistance);
}
this.collideWithWalls(context);
};
Point.prototype.draw = function (context) {
if (this.highlighted) {
context.fillStyle = '#CB99C9';
}
else {
context.fillStyle = '#38d296';
}
context.beginPath();
context.arc(this.pos.x, this.pos.y, Point.radius / 2, 0, 2 * Math.PI);
context.fill();
};
Point.prototype.collideWithWalls = function (context) {
if (this.pos.x < Point.radius / 2) { // if too far left.
this.pos.x = Point.radius / 2;
this.vel.x = Math.abs(this.vel.x) * (1 - Point.bounceResistance);
this.vel.y *= 1 - Point.friction;
}
if (this.pos.y < Point.radius / 2) { // if too far up.
this.pos.y = Point.radius / 2;
this.vel.y = Math.abs(this.vel.y) * (1 - Point.bounceResistance);
this.vel.x *= 1 - Point.friction;
}
if (this.pos.x > context.canvas.width - Point.radius / 2) { // if too far to the right.
this.pos.x = context.canvas.width - Point.radius / 2;
this.vel.x = Math.abs(this.vel.x) * -1 * (1 - Point.bounceResistance);
this.vel.y *= 1 - Point.friction;
}
if (this.pos.y > context.canvas.height - Point.radius / 2) { // if too far to the bottom.
this.pos.y = context.canvas.height - Point.radius / 2;
this.vel.y = Math.abs(this.vel.y) * -1 * (1 - Point.bounceResistance);
this.vel.x *= 1 - Point.friction;
}
};
phys.Point = Point;
})(window.phys = window.phys || {});