-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.js
52 lines (44 loc) · 1.31 KB
/
ball.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
import { Vector } from "./vector.js"
import { drawCircle } from "./utils.js"
let canvas = document.querySelector("canvas")
class Ball {
constructor(x, y) {
this.pos = new Vector(x,y);
this.vel = new Vector(0,0);
this.gravity = new Vector(0,0.1);
this.r = 15;
this.friction = 0.7;
this.m = 1;
}
updateVelocity(){
this.vel = this.vel.add(this.gravity);
this.pos = this.pos.add(this.vel)
}
checkWallCollision(){
if(this.pos.y > canvas.height - this.r){
this.pos.y = canvas.height - this.r;
this.vel.y *= -1 * this.friction;
}
else if(this.pos.y < this.r){
this.pos.y = this.r;
this.vel.y *= -1 * this.friction;
}
if(this.pos.x > canvas.width - this.r){
this.pos.x = canvas.width - this.r;
this.vel.x *= -1 * this.friction;
}
else if(this.pos.x < this.r){
this.pos.x = this.r;
this.vel.x *= -1 * this.friction;
}
}
update() {
this.updateVelocity();
this.checkWallCollision();
this.draw();
}
draw() {
drawCircle(this.pos.x,this.pos.y,this.r, "rgb(255,255,255)")
}
}
export { Ball }