如何在ThreeJs Javascript中测试网格是否碰撞?

时间:2019-10-27 19:23:49

标签: javascript three.js collision-detection

好,所以我试图 Three.js javascript中测试两个立方体是否碰撞。它不起作用...可悲的是。 我看到了另一个这样的问题,但答案对我没有用。这是我的代码(不是我的所有代码)以及失败的冲突检测。 -

var plrHead = new THREE.Mesh(geometry, skin)//this is the players head(the one to detect the collision)
var blocks = [//all the other meshes]
// there is a lot of code that i don't want to show
// basically using - if (oncollide()){}

冲突检测失败

function onCollide() {
  for (var vertexIndex = 0; vertexIndex < plrHead.geometry.vertices.length; vertexIndex++)
    {
        var localVertex = plrHead.geometry.vertices[vertexIndex].clone();
        var globalVertex = localVertex.applyMatrix4( plrHead.matrix );
        var directionVector = globalVertex.sub( plrHead.position );

        var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
        var collisionResults = ray.intersectObjects( blocks );
        if (collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() ) {
            return true
    }
    }
}

是的,如果有人知道如何检测网格之间的碰撞,那就谢谢!

0 个答案:

没有答案