简体   繁体   English

每当凸多面体之间发生碰撞时,cannon-es 就会崩溃

[英]cannon-es crashes whenever there is a collision between Convex polyhedrons

I am trying to implement a dice roller in three.js and cannon-es.我正在尝试在 three.js 和 cannon-es 中实现一个骰子滚筒。 It works perfectly fine if I have only one dice.如果我只有一个骰子,它工作得很好。 It rolls against the ground plane in a reasonable way.它以合理的方式在地平面上滚动。

When I add another dice it also works fine against the ground plane, but it completely breaks down and crashes the simulation as soon as the two CANNON.ConvexPolyhedron objects collide.当我添加另一个骰子时,它在地平面上也能正常工作,但一旦两个CANNON.ConvexPolyhedron对象发生碰撞,它就会完全崩溃并使模拟崩溃。

The error I am getting spammed with after the collision is this碰撞后我收到垃圾邮件的错误是这个

cannon-es.js:920 Uncaught TypeError: Cannot read properties of undefined (reading 'x')
    at Vec3.copy (cannon-es.js:920:21)
    at ConvexPolyhedron.clipFaceAgainstHull (cannon-es.js:2662:24)
    at ConvexPolyhedron.clipAgainstHull (cannon-es.js:2404:12)
    at Narrowphase.convexConvex (cannon-es.js:10916:10)
    at Narrowphase.getContacts (cannon-es.js:10608:33)
    at World.internalStep (cannon-es.js:12649:22)
    at World.step (cannon-es.js:12515:12)
    at updatePhysics (rolling.svelte:154:11)
    at animate (rolling.svelte:142:5)

I am using a STL loader from three.js to load a stl file of a twenty sided dice, and then I am creating a cannon body from that like this我正在使用来自 three.js 的 STL 加载程序来加载一个 stl 文件的二十面骰子,然后我正在创建一个像这样的炮身

  function createConvexHull(mesh: THREE.Mesh) {
    const position = mesh.geometry.attributes.position.array
    const points: CANNON.Vec3[] = []
    const faces: number[][] = []
    for (let i = 0; i < position.length; i += 3) {
      points.push(new CANNON.Vec3(position[i], position[i + 1], position[i + 2]))
    }
    for (let i = 0; i < position.length / 3; i += 3) {
      faces.push([i, i + 1, i + 2])
    }
    const convexGeometry = new CANNON.ConvexPolyhedron({
      vertices: points,
      faces: faces
    })
    const body = new CANNON.Body({ mass: 1 })
    body.addShape(convexGeometry)
    return body
  }

I have little to no idea what is going wrong, as the error message is not saying much, but I've tried computing vertex normals on the ThreeJS mesh, which did nothing.我几乎不知道出了什么问题,因为错误消息并没有说明太多,但我已经尝试在 ThreeJS 网格上计算顶点法线,但什么也没做。

I've also tried merging verticies as I read that others have had issues with that (merged before creating the cannon body)我也尝试过合并顶点,因为我读到其他人对此有疑问(在创建大炮主体之前合并)

geometry = BufferGeometryUtils.mergeVertices(geometry, 0.01)

but again, it did nothing但同样,它什么也没做

I'm just guessing, but you may try defining the normals我只是猜测,但您可以尝试定义法线

new ConvexPolyhedron({ vertices, faces, normals });

I experienced weird behaviors with convex shape collisions (not errors, but like launching one of the colliding objects into the void), that I could resolve by re calculating the normal vectors我遇到了凸形碰撞的奇怪行为(不是错误,而是像将其中一个碰撞物体发射到空隙中),我可以通过重新计算法向量来解决

https://github.com/tomo0613/offroadJS_v2/blob/355b6aabf0446deefffba6d60e24a257836916ea/src/mapModules/baseMapElementComponents.ts#L137 https://github.com/tomo0613/offroadJS_v2/blob/355b6aabf0446deefffba6d60e24a257836916ea/src/mapModules/baseMapElementComponents.ts#L137

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM