【发布时间】:2015-03-23 17:44:00
【问题描述】:
我正在通过包含我已加载为网格的 obj 的场景移动相机,并且我想检测相机是否与我的 obj 的任何墙壁发生碰撞。
我的代码基于这个 threejs 示例: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
【问题讨论】:
标签: javascript three.js webgl collision-detection raycasting