0

我创建了这个函数,它在我的渲染循环中调用以检测碰撞并移动玩家/相机(这是第一人称游戏)使用命名为 CubeGeometry 的碰撞检测,该 CubeGeometrypCube被移动以匹配每一帧的相机:

// Player movements
function pMovements() {
    mPlayer.colBottom = false;

    pCube.position.x = mPlayer.yawObject.position.x + 50; // The cube is placed +50 so we can see/debug it.
    pCube.position.y = mPlayer.yawObject.position.y - 10;
    pCube.position.z = mPlayer.yawObject.position.z;

    // -- COLLISION DETECTION START --
    var originPoint = pCube.position.clone();

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

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

            // Bottom vertices
            if (vertexIndex == 2 || vertexIndex == 3 || vertexIndex == 6 || vertexIndex == 7) {
                mPlayer.colBottom = true;
                mPlayer.velocity.y = Math.max( 0, mPlayer.velocity.y ); // Stop falling
            }
        }
    }
    // -- COLLISION DETECTION END --

    var delta = (Date.now() - time) * 0.1;

    mPlayer.velocity.x += (-mPlayer.velocity.x) * 0.08 * delta; // walking
    mPlayer.velocity.z += (-mPlayer.velocity.z) * 0.08 * delta; // walking
    if (mPlayer.colBottom == false) {
        mPlayer.velocity.y -= 0.1 * delta; // falling
    }

    if (mPlayer.moveForward) mPlayer.velocity.z -= mPlayer.speed * delta;
    if (mPlayer.moveBack) mPlayer.velocity.z += mPlayer.speed * delta;
    if (mPlayer.moveLeft) mPlayer.velocity.x -= mPlayer.speed * delta;
    if (mPlayer.moveRight) mPlayer.velocity.x += mPlayer.speed * delta;

    mPlayer.yawObject.translateX(mPlayer.velocity.x);
    mPlayer.yawObject.translateY(mPlayer.velocity.y); 
    mPlayer.yawObject.translateZ(mPlayer.velocity.z);

    if (mPlayer.yawObject.position.y < -2000) {
        // Player has fallen out of bounds :( so re-initialise the players position
        mPlayer.velocity.y = 0;
        mPlayer.yawObject.position.y = 100;
        mPlayer.yawObject.position.x = 0;
        mPlayer.yawObject.position.z = 0;

        mPlayer.yawObject.rotation.y = 0;
        mPlayer.pitchObject.rotation.x = 0;
    }

    if (mPlayer.moveDown) {
        mPlayer.yawObject.position.y -= 1;
    }
    if (mPlayer.moveUp) {
        mPlayer.yawObject.position.y += 1;
    }
}

单击此处查看演示。
WASD 移动。跳跃的空间(有点)。黑色立方体/矩形反映了相机在 x 轴上的位置 +50。在立方体上检测到碰撞。

基本上我对此有两个问题。我应该使用立方体的顶点还是面来检测碰撞?如果对象比立方体小,则不会检测到碰撞,因为它不会碰到任何顶点。那么我应该为面孔重写它吗?

其次,当检测到碰撞时,如何防止立方体掉得太远。如果您查看演示,当立方体从某物上掉下来时,它会在停止之前继续下落一段时间。我认为与此有关,mPlayer.velocity.y但我无法修复它。即使是跳跃也会使立方体沉入地板深处。

4

1 回答 1

1

为了增加碰撞检测的“分辨率”,您可以向立方体添加更多顶点,例如,当您声明 pCube 尝试时:

pCube = new THREE.CubeGeometry(100,100,100, 5,5,5);

并且您的其余代码可以保持不变。

对于较小的物体“在”为碰撞检测创建的​​射线之间“滑动”,通常如果您使用此方法但让小物体创建射线,那么您将更准确地检测碰撞。

于 2013-03-28T03:52:15.373 回答