p5.Vector.prototype.mult:x、y 或 z 参数未定义或不是有限数
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);
}
}
我正在尝试复制 Sebastian Lague 的太阳系视频,但使用 p5.js
我收到此错误 p5.Vector.prototype.mult:x、y 或 z 参数未定义或不是有限数 。当我删除相乘的行时,它就会消失,但这使得整个事情不起作用。我查了一下,可能是z值自动为0,但我尝试通过添加手动将其设置为1,但仍然不起作用。
我做了一些调试,发现力实际上发生了变化,但由于行星不移动,它在一段时间后保持不变,什么也没有发生。
我该如何摆脱这个错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
控制台日志
forceDir
。我怀疑向量分量之一是无穷大(或未定义)。Console log
forceDir
. I suspect one of the vector components is infinity (or undefined).