updateVelocity(allBodies, timeStep){
for (let otherBody of allBodies) {
if(otherBody != this){
let sqrDst = (p5.Vector.sub(otherBody.pos, this.pos).mag());
let forceDir = (p5.Vector.sub(otherBody.pos, this.pos).normalize());
let force = p5.Vector.mult(forceDir, gravity * this.mass * otherBody.mass / sqrDst);
let acceleration = p5.Vector.div(force, this.mass);
this.currentVelocity.add(acceleration * timeStep);
}
}
}
updatePosition(timeStep){
this.pos.add(this.currentVelocity * timeStep);
ellipse(this.pos.x, this.pos.y, this.radius);
}
}
I am trying to replicate Sebastian Lague's solar system video but with p5.js
I am getting this error p5.Vector.prototype.mult: x, y, or z arguments are either undefined or not a finite number. When I remove the line where i multiply it disapears but that makes the whole thing not work. I looked it up and it could be that the z value is automatically 0, but i tried setting it manually to 1 by adding and it still didnt work.
I did some debugging and found out that the force actually changes but as the planets dont move it stays the same after a while and nothing happens.
How do i get rid of this error?
Console log forceDir. I suspect one of the vector components is infinity (or undefined).