-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.js
39 lines (38 loc) · 1.02 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
class Ball{
constructor(r) {
this.location = createVector(random(r,width-r), random(r,height-r));
this.velocity = p5.Vector.random2D();
this.velocity.setMag(random(2, 4));
this.acceleration = createVector();
this.mass = 1;
this.maxForce = 0.3;
this.maxSpeed = 1.5;
this.radius = r;
}
applyForce(force) {
var f = p5.Vector.div(force,this.mass);
this.acceleration.add(f);
}
edges(){
if (this.location.x > width){
this.location.x = width;
this.velocity.x *= -1;
}else if (this.location.x < 0) {
this.location.x = 0;
this.velocity.x *= -1;
}
else if (this.location.y < 0) {
this.location.y = 0;
this.velocity.y *= -1;
} else if (this.location.y > height) {
this.location.y = height;
this.velocity.y *= -1;
}
}
update() {
this.location.add(this.velocity);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.acceleration.mult(0);
}
}