4

我正在生成这样的地形几何:

     var geometry = new THREE.PlaneGeometry(100, 100, 100 , 100 );
     for (var i = 0, l = geometry.vertices.length; i < l; i++) {
          geometry.vertices[i].z  = Math.random() / 2;
     }
     geometry.computeFaceNormals();
     floor = new THREE.Mesh(geometry, floorMaterial);

并有另一个应该在地板表面上行走的 object3d。我怎样才能知道我的物体必须上升或下降多少?也许与raycaster。

[编辑]第一部分很好解决,但第二部分是raycaster返回的距离不正确,它以某种方式为每个面翻转。检查示例: http: //myhtmltest.net76.net/raycast.html

4

1 回答 1

3

正如您已经猜到的那样,THREE.Raycaster 应该确实可以工作。

在更新循环中,您需要向下投射光线

var raycaster = new THREE.Raycaster();
raycaster.set(yourMovingObject.position, THREE.Vector3(0, -1, 0));

之后,您需要确定物体需要停止“下落”的距离地面的距离

var distance = 40; //set to your own measurements

最后但并非最不重要的一点是,检查十字路口

var velocity = new THREE.Vector3();

var intersects = raycaster.intersectObject(floor); //use intersectObjects() to check the intersection on multiple

//new position is higher so you need to move you object upwards
if (distance > intersects[0].distance) {        
    yourMovingObject.position.y += (distance - intersects[0].distance) - 1; // the -1 is a fix for a shake effect I had
}

//gravity and prevent falling through floor
if (distance >= intersects[0].distance && velocity.y <= 0) {
    velocity.y = 0;
} else if (distance <= intersects[0].distance && velocity.y === 0) {
    velocity.y -= delta ;
}

yourMovingObject.translateY(velocity.y);

像这样的东西应该可以工作,但是您必须根据自己的环境对其进行调整。

于 2013-07-03T09:37:35.233 回答