Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AidanNelson
Last active October 3, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AidanNelson/191e6a78a683f44348b7358e1b0fc0f5 to your computer and use it in GitHub Desktop.
Save AidanNelson/191e6a78a683f44348b7358e1b0fc0f5 to your computer and use it in GitHub Desktop.
2017.10.03 -- ICM Week 4
//why are the magnets getting stuck on the walls?
//slicing while looping?
//having an object perform certain functions on itself? maintenance functions?
let magnets = [];
function setup() {
createCanvas(600, 500);
}
function draw() {
background(220, 120, 30);
//iterate through array of magnets (balls) and ask each ball to display, move, and bounce
for (let i = 0; i < magnets.length; i++) {
magnets[i].display();
magnets[i].move();
magnets[i].bounce();
//remove a magnet object if it is close to its 'birthplace' and age is over ten frames (so that they have a chance to exist)
if (magnets[i].isHome() && (frameCount - magnets[i].birth) > 10) {
magnets.splice(i, 1);
//is the following line necessary?
i--;
}
}
//reverse speed every 200 frames
if (frameCount % 200 == 0) {
for (let i = 0; i < magnets.length; i++) {
magnets[i].reverse();
}
}
}
//create a new ball every time the mouse is clicked
function mouseClicked() {
magnets.push(new Magnet(mouseX, mouseY, 25));
}
//constructor function for magnet objects
function Magnet(x, y, d) {
this.x = x;
this.y = y;
this.d = d;
//used to prevent balls from disappearing immediately after being 'born'
this.birth = frameCount;
//used to keep track of where they were 'born' and should disappear
this.birthX = x;
this.birthY = y;
this.xspeed = random(-5, 5);
this.yspeed = random(-5, 5);
this.move = function() {
this.x += this.xspeed;
this.y += this.yspeed;
}
//attempt to add gravity... didn't work...
this.updateSpeed = function() {
this.yspeed += (1 / 60 * 10 * (frameCount - this.birth));
console.log(frameCount - this.birth);
}
//bounces the object against the walls
this.bounce = function() {
if (this.x - this.d / 2 <= 0 || this.x + this.d / 2 >= width) {
this.xspeed *= -1;
}
if (this.y - this.d / 2 < 0 || this.y + this.d / 2 > height) {
this.yspeed *= -1;
}
}
//reverse speed
this.reverse = function() {
this.xspeed *= -1;
this.yspeed *= -1;
}
//displays the magnet object as an ellipse
this.display = function() {
fill(255, 0, 255);
ellipse(this.x, this.y, this.d, this.d);
}
//attempt at a repelling function, but how to access other objects in the array magnets[]? didn't work...
this.repel = function(magnet) {
if (dist(this.x, this.y, magnet.x, magnet.y) < 50) {
this.xspeed = -magnet.xspeed;
this.yspeed = -magnet.yspeed;
}
}
//returns whether the magnet is close (within 10) to home
this.isHome = function() {
if (dist(this.x, this.y, this.birthX, this.birthY) < 10) {
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment