Hi guys,
I’m playing around with the simple physics engine from this video: https://www.youtube.com/watch?v=BjoM9oKOAKY
What I wanted to do is to have two particles that just move in one direction. To do this I create two particles with different starting angles, say 0 and 90 and update them so that they should move on a straight line. If I create each particle on it’s own, it works just fine. If I create and update both however, they both start moving in the 45 degree direction. This baffles me, since the update function inside the particle object should be isolated from each other right? Here is the code:
var particles = [];
var flowfield;
function setup() {
createCanvas(400, 400);
sta_pt = createVector(height * 0.2, width * 0.8);
bg_col = color(3, 219, 252);
colorMode(HSB, 255);
flowfield = new Array(2);
for (var i = 0; i < 2; i++) {
if (i == 0) {
ang = 0;
parti = new Particle();
} else {
ang = 90;
parti = new Particle();
}
particles[i] = parti;
var v = p5.Vector.fromAngle(ang, 1);
flowfield[i] = v;
}
background(bg_col);
frameRate(5);
}
function draw() {
for (var i = 0; i < particles.length; i++) {
particles[i].applyForceArr(flowfield, i);
particles[i].update();
particles[i].show();
}
}
And heres the particle class:
function Particle() {
this.pos = sta_pt;
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.maxspeed = 4;
this.h = 0;
this.prevPos = this.pos.copy();
this.update = function() {
this.vel.add(this.acc);
this.vel.limit(this.maxspeed);
this.pos.add(this.vel);
this.acc.mult(0);
};
this.applyForceArr = function(force, index) {
print(force[index]);
this.acc.add(force[index]);
};
this.show = function() {
stroke(0);
strokeWeight(1);
line(this.pos.x, this.pos.y, this.prevPos.x, this.prevPos.y);
this.updatePrev();
};
this.updatePrev = function() {
this.prevPos.x = this.pos.x;
this.prevPos.y = this.pos.y;
}
}