admin管理员组文章数量:1122846
I'm making a 3D physics engine and I need to add balls when I press a button, but whenever I click it, everything goes blank. I know it does add a ball to the array of objects and adds adds a sphere to the mesh, but the canvas just goes blank.
Here's the code for the loop:
initGL()
function loop(){
//do physics math
balls.forEach(ball => {
//draw sphere
ball.draw()
ball.phys()
})
//collisions
for (let i = 0; i < substeps; i++){
balls.forEach(ball => {
ball.collwall()
ball.collballs()
})
}
//update the WebGL vertex array buffer and index array buffer
mesh.update()
runGLFrame()
requestAnimationFrame(loop)
}
requestAnimationFrame(loop)
Event: Just adds a ball at 0,0,0.
balls.push(new Ball(0,0,0,0,0,0,1,0,1,1,1,0.8))
Ball constructor: Initializes the ball object and draws a sphere.
constructor(x,y,z,vx,vy,vz,rad,r,g,b,w,bounce){
//sphere number
this.sn=balls.length
//current pos.
this.p={x,y,z}
//past pos.
this.pp={x:x-vx,y:y-vy,z:z-vz}
//velocity
this.v={x:vx,y:vy,z:vz}
//weight
this.w=w
//bounciness
this.b=bounce
//radius
this.rad=rad
//color
this.c={r,g,b}
//add sphere to mesh, vertices and indices
mesh.addSphere(20,20,x,y,z,rad,r,g,b)
}
It was supposed to create a ball at 0,5,0, but instead it cleared everything. I copied the clear mesh code to the event to try to copy the thing that happens when I initialize it (before the loop and event) but it still doesn't work.
本文标签: javascript3D mesh not working with array of objectsStack Overflow
版权声明:本文标题:javascript - 3D mesh not working with array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287282a1927845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论