-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMover.js
More file actions
74 lines (64 loc) · 2.07 KB
/
Mover.js
File metadata and controls
74 lines (64 loc) · 2.07 KB
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
68
69
70
71
72
73
74
class Mover {
constructor(x, y, m, grav) {
this.position = createVector(x, y);
this.velocity = createVector();
this.acceleration = createVector();
this.topSpeed = 5;
this.mass = m;
this.radius = sqrt(this.mass) * 10;
this.gravity = p5.Vector.mult(grav, this.mass);
}
applyForce(force) {
this.acceleration.add(p5.Vector.div(force, this.mass));
}
show() {
stroke(0);
strokeWeight(2);
fill(127, 127);
circle(this.position.x, this.position.y, this.radius*2);
}
update() {
if (this.contactEdge()) {
//print(`Contact with edge!`);
this.friction = this.velocity.copy();
this.friction.mult(-1);
this.friction.setMag(coefOfFriction);
this.applyForce(this.friction);
}
this.weight = p5.Vector.mult(this.gravity, this.mass);
this.applyForce(this.weight);
this.velocity.add(this.acceleration);
this.velocity.limit(this.topSpeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
collision() {
if (this.position.x >= (width - this.radius)) {
this.velocity.x *= -1;
this.position.x = width - this.radius;
}
if (this.position.x <= (0 + this.radius)) {
this.velocity.x *= -1;
this.position.x = 0 + this.radius;
}
if (this.position.y >= (height - this.radius)) {
this.velocity.y *= -1;
this.position.y = height - this.radius;
}
if (this.position.y <= (0 + this.radius)) {
this.velocity.y *= -1;
this.position.y = 0 + this.radius;
}
}
contactEdge() {
return (this.position.y > height - this.radius - 1);
}
bounceEdges() {
let bounce = -0.5;
if (this.position.y > height - this.radius - 1) {
//this.position.y = height - this.radius;
this.velocity.y *= bounce;
//print(`Bounced!`);
}
}
}