-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.js
123 lines (115 loc) · 2.82 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class Ball {
constructor(r, pos, vel, acc) {
this.pos = pos;
this.vel = vel;
this.acc = acc;
this.r = r;
this.friction = 1;
this.bounce_friction = 1;
this.collided = false;
}
draw() {
fill(150, 150);
if(!this.collided) {
noStroke();
} else {
stroke(255, 0, 0);
this.collided = false;
}
ellipse(this.pos.x, this.pos.y, this.r * 2, this.r * 2);
let f = this.acc.copy();
// f.normalize();
// f.mult(this.acc.mag());
f.mult(200);
f.add(this.pos);
stroke(230, 0, 0, 100);
line(this.pos.x, this.pos.y, f.x, f.y);
let d = this.vel.copy();
// d.normalize();
// d.mult(this.vel.mag());
d.mult(10);
d.add(this.pos);
stroke(0, 0, 230, 100);
line(this.pos.x, this.pos.y, d.x, d.y);
// console.log(Math.round(this.vel.x * 100)/100 + ' ' + Math.round(this.vel.y*100)/100);
}
accel() {
let target = attractor.copy();
target.sub(this.pos);
target.normalize();
target.mult(0.1);
this.acc = target;
if(this.pos.dist(attractor) < settingsObject.effectField) {
this.friction = 1;
} else {
this.friction = 0.9;
}
}
touch() {
let avgVel = new p5.Vector(0, 0);
let collisions = 0;
for (let i = 0; i < balls.length; i++) {
if (balls[i] === this) continue;
let d = this.pos.dist(balls[i].pos);
if (d > 0 && d <= (this.r + balls[i].r) * 1.5) {
avgVel.add(this.pos.copy().sub(balls[i].pos).normalize());
collisions += 1;
}
}
// avgVel.add(this.vel);
// collisions += 1;
if(collisions > 0) {
avgVel.div(collisions);
this.vel.add(avgVel);
this.vel.mult(this.bounce_friction);
this.collided = true;
}
// let targetD = this.r + balls[i].r;
// let diff = (targetD - d) / 2;
// this.vel.sub(balls[i].vel);
// balls[i].vel.add(this.vel);
// this.vel.mult(this.bounce_friction);
// balls[i].vel.mult(balls[i].bounce_friction);
// let dPos = this.vel.copy().normalize().mult(diff);
// this.pos.add(dPos);
// let bPos = balls[i].vel.copy().normalize().mult(diff);
// balls[i].pos.add(bPos);
// }
}
update() {
if (settingsObject.useGlobal) {
this.acc.x = settingsObject.forces_x;
this.acc.y = settingsObject.forces_y;
} else {
this.accel();
}
this.vel.add(this.acc);
this.vel.mult(this.friction);
this.pos.add(this.vel);
if(settingsObject.collide) {
this.touch();
}
if (settingsObject.useGlobal) {
if (this.pos.x + this.r > width) {
this.pos.x = width - this.r;
this.vel.mult([-1, 1]);
this.vel.mult(this.bounce_friction);
}
if (this.pos.x + this.r < 0) {
this.pos.x = this.r;
this.vel.mult([-1, 1]);
this.vel.mult(this.bounce_friction);
}
if (this.pos.y + this.r > height) {
this.pos.y = height - this.r;
this.vel.mult([1, -1]);
this.vel.mult(this.bounce_friction);
}
if (this.pos.y + this.r < 0) {
this.pos.y = this.r;
this.vel.mult([1, -1]);
this.vel.mult(this.bounce_friction);
}
}
}
}