Created
July 29, 2018 19:32
-
-
Save izebit/3b9aa3b0ab0d5d684b357bb67b1da8a0 to your computer and use it in GitHub Desktop.
example-with-attraction-processing.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var G = 1; | |
var Mover = function(m, x, y) { | |
this.mass = m; | |
this.position = new PVector(x, y); | |
this.velocity = new PVector(0, 0); | |
this.acceleration = new PVector(0, 0); | |
}; | |
Mover.prototype.applyForce = function(force) { | |
var f = PVector.div(force, this.mass); | |
this.acceleration.add(f); | |
}; | |
Mover.prototype.update = function() { | |
this.velocity.add(this.acceleration); | |
this.position.add(this.velocity); | |
this.acceleration.mult(0); | |
}; | |
Mover.prototype.display = function() { | |
stroke(0); | |
strokeWeight(2); | |
fill(255, 255, 255, 127); | |
ellipse(this.position.x, this.position.y, this.mass*16, this.mass*16); | |
}; | |
Mover.prototype.calculateAttraction = function(m) { | |
var force = PVector.sub(this.position, m.position); | |
var distance = force.mag(); | |
distance = constrain(distance, 5.0, 25.0); | |
force.normalize(); | |
var strength = (G * this.mass * m.mass) / (distance * distance); | |
force.mult(strength); | |
return force; | |
}; | |
var movers = []; | |
for (var i = 0; i < 5; i++) { | |
movers[i] = new Mover(random(0.1, 2), random(width), random(height)); | |
} | |
draw = function() { | |
background(50, 50, 50); | |
for (var i = 0; i < movers.length; i++) { | |
for (var j = 0; j < movers.length; j++) { | |
if (i !== j) { | |
var force = movers[j].calculateAttraction(movers[i]); | |
movers[i].applyForce(force); | |
} | |
} | |
movers[i].update(); | |
movers[i].display(); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment