-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
28 lines (25 loc) · 814 Bytes
/
Particle.js
File metadata and controls
28 lines (25 loc) · 814 Bytes
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
var gravity = require('./gravitationalAcceleration.js')
var Particle = function(p, v, life) {
this.pos = p
this.vel = v
this.life = life
}
Particle.prototype.update = function(dt, p, v, life, plaents, d) {
this.gravity(dt, planets, d)
this.life -= 0.1
if (this.life < 0) {
this.life = life
this.pos = p
this.vel = v
}
}
Particle.prototype.gravity = function(dt, planets, d) {
var a = planets.map(i => gravity(this.pos, dt, i, d))
.reduce( (a, b) => [ a[0] + b[0], a[1] + b[1] ] )
this.vel = [ this.vel[0] + a[0] * dt, this.vel[1] + a[1] * dt ]
this.pos = [ this.pos[0] + this.vel[0]*dt, this.pos[1] + this.vel[1]*dt ]
}
Particle.prototype.draw = function(ctx, t, size, image) {
ctx.drawImage( image, this.pos[0] - size/2, this.pos[1] - size/2, size, size )
}
module.exports = Particle