我将相机移动到包含我作为网格加载的对象的场景中,我想检测相机是否与我的对象的任何墙壁相撞。
我已根据这个三个例子http://threejs.org/examples/misc_controls_pointerlock.html创建了我的代码,并尝试在此处应用您的示例:http://stemkoski.github.io/Three.js/Collision-Detection.html - 但我似乎无法发生冲突。
我的例子是here
且相关的javascript为here:
如果有人能指出我如何发现碰撞的正确方向,我将不胜感激。这是相关的代码:
var objects = [];
var oLoader = new THREE.OBJLoader();
//oLoader.load('models/chair.obj', function(object, materials) {
oLoader.load('models/model-for-threejs.obj', function(object, materials) {
// var material = new THREE.MeshFaceMaterial(materials);
var material = new THREE.MeshLambertMaterial({ color: 0x000000 });
//var material = new THREE.MeshBasicMaterial({wireframe: true});
object.traverse( function(child) {
if (child instanceof THREE.Mesh) {
//objects.push(child); //MH - not sure if the object needs to be added here or not, but if I do, this really bogs down things down
}
});
object.position.x = 0;
object.position.y = 12;
object.position.z = 0;
scene.add(object);
objects.push(object);
});
}
var curPos = controls.getObject().position; //gives the position of my camera
raycaster.ray.origin.copy( curPos );
//raycaster.ray.origin.y -= 10;
raycaster.ray.origin.z +=10; //guessing here, but since I'm moving in 2d space (z / x), maybe I should be moving my ray ahead in z space?
var intersections = raycaster.intersectObjects( objects ); //
var isOnObject = intersections.length > 0;
if (isOnObject){ console.log('collide' }; //MH - nothing happening here
答案 0 :(得分:2)
Raycaster的交叉对象使用带有子节点的Object3D,并且有一个递归标记。
https://github.com/mrdoob/three.js/blob/master/src/core/Raycaster.js#L33
所以看起来应该是这样的:
var intersections = raycaster.intersectObjects( yourRootObject3D, true );