1

我正在做简单的碰撞检测,虽然我可以使用 THREE.ray 轻松检测上/下物体,但是当它们旋转时,我很难找到相机(或任何物体)的前/后的物体?我尝试用投影仪投射光线,并使用辅助箭头显示该光线是如何射出的。一旦我开始围绕 Y 轴旋转相机,光线会指向相反的方向,或者只是表现得很奇怪......

ray = new THREE.Ray();
projector = new THREE.Projector();
vector = projector.projectVector( coll.getObject().matrix.getPosition().clone(), camera );
ray.direction = vector.normalize();
ray.origin = coll.getObject().matrix.getPosition().clone();
helper.setDirection(ray.direction.clone());
helper.position = ray.origin.clone();
4

1 回答 1

0

我最终在连接到相机的对象周围设置了一个立方体,并通过添加的顶点拍摄光线,如下所示:

vertices = coll.getObject().geometry.vertices;
        rad = coll.getObject().boundRadius+1;
        var directions = {
            "up": [4,1],
            "down": [6,2],
            "front": [3,4],
            "back": [7,0],
            "left": [5,6],
            "right": [1,2],
        };
        var collisions = {
            "up": {},
            "down": {},
            "front": {},
            "back": {},
            "left": {},
            "right": {},
        };
        for (key in directions){
            (directions[key].length > 1) ? localVertex =  vertices[directions[key][0]].clone().addSelf(vertices[directions[key][1]].clone()) : localVertex = vertices[directions[key][0]].clone();  
            globalVertex = coll.getObject().matrix.multiplyVector3(localVertex);
            directionVector = globalVertex.subSelf( coll.getObject().position );
            ray = new THREE.Ray( coll.getObject().position.clone(), directionVector.clone().normalize(), 0, 1000 );
            intersects = ray.intersectObjects(obj, true);
            if (intersects.length > 0) {
                distance = intersects[ 0 ].distance;
                if (distance >= 0 && distance <= rad) {
                    collisions[key] = intersects[ 0 ];              
                } else {                        
                    collisions[key] = false;
                }
            } else {
                collisions[key] = false;
                ++falseCount;
            }       
        }                   
    }
    return (falseCount !== 6) ? collisions : false;
于 2012-11-07T09:05:29.917 回答