-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex.js
57 lines (57 loc) · 1.57 KB
/
vertex.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
class Vertex{
constructor(x,y,r,fixed = false,worldBound = true){
this.x = x;
this.y = y;
this.px = x;
this.py = y;
this.r = r;
this.friction = 1;
this.gravity = 2;
this.fixed = fixed;
this.worldBound = worldBound;
}
worldBoundCheck(){
if(this.fixed){
return;
}
if(this.worldBound){
let velX = (this.x - this.px) * this.friction;
let velY = (this.y - this.py) * this.friction;
if(this.y+this.r > ch){
this.y = ch-this.r;
this.py = (this.y + velY);
}
else if(this.y-this.r < 0){
this.y = this.r;
this.py = (this.y + velY);
}
if(this.x+this.r > cw){
this.x = cw-this.r;
this.px = (this.x + velX);
}
else if(this.x-this.r < 0){
this.x = this.r;
this.px = (this.x + velX);
}
}
}
update(){
if(this.fixed){
return;
}
const velX = (this.x - this.px) * this.friction;
const velY = (this.y - this.py) * this.friction;
this.px = this.x;
this.py = this.y;
this.x += velX;
this.y += velY;
this.y += this.gravity;
}
draw(){
ctx.beginPath();
ctx.fillStyle = "rgb(10,80,80)";
ctx.arc(this.x,this.y,this.r,0,Math.PI*2);
ctx.fill();
ctx.closePath();
}
}