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?

1 Answer

Console log forceDir. I suspect one of the vector components is infinity (or undefined).

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy