-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparticles.js
84 lines (66 loc) · 2.4 KB
/
particles.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
const INFINITY = Number.MAX_SAFE_INTEGER;
class Particle {
constructor(position, velocity, radius) {
this.position = position;
this.velocity = velocity;
this.radius = radius;
this.mass = 1;
this.collisions = 0;
}
update = (dt) => {
this.position.add(p5.Vector.mult(this.velocity, dt));
};
timeToParticle = (that) => {
if (this == that) return INFINITY;
var dp = p5.Vector.sub(that.position, this.position);
var dv = p5.Vector.sub(that.velocity, this.velocity);
var dvdr = dp.x * dv.x + dp.y * dv.y;
if (dvdr > 0) return INFINITY;
var dvdv = dv.x * dv.x + dv.y * dv.y;
if (dvdv == 0) return INFINITY;
var drdr = dp.x * dp.x + dp.y * dp.y;
var sigma = this.radius + that.radius;
var dist = dvdr * dvdr - dvdv * (drdr - sigma * sigma);
if (dist < 0) return INFINITY;
return -(dvdr + Math.sqrt(dist)) / dvdv;
}
timeToVWall = (width) => {
if (this.velocity.x > 0) {
return (width - this.position.x - this.radius) / this.velocity.x;
} else if(this.velocity.x < 0) {
return (this.radius - this.position.x) / this.velocity.x;
} else {
return INFINITY
}
}
timeToHWall = (height) => {
if (this.velocity.y > 0) {
return (height - this.position.y - this.radius) / this.velocity.y;
} else if(this.velocity.y < 0) {
return (this.radius - this.position.y) / this.velocity.y;
} else {
return INFINITY
}
}
hitParticle = (that) => {
var dp = p5.Vector.sub(that.position, this.position);
var dv = p5.Vector.sub(that.velocity, this.velocity);
var dvdr = dp.x * dv.x + dp.y * dv.y;
var sigma = this.radius + that.radius;
var imp = 2 * this.mass * that.mass * dvdr / ((this.mass + that.mass) * sigma);
var imp_vector = p5.Vector.mult(dp, imp / sigma);
this.velocity.add(p5.Vector.div(imp_vector, this.mass));
that.velocity.sub(p5.Vector.div(imp_vector, that.mass));
this.collisions++;
that.collisions++;
}
hitVWall = () => {
this.velocity.x *= -1;
this.collisions++;
}
hitHWall = () => {
this.velocity.y *= -1;
this.collisions++;
}
}
module.exports = Particle;