相机和地形碰撞在three.js上

时间:2013-02-18 07:57:29

标签: javascript three.js collision-detection collision collada

我有一个使用sketchup创建的建筑模型,导出为collada文件并使用colladaloader加载到three.js。 除了相机可以穿过墙壁外,一切正常。我该如何防止这种情况?到目前为止,我已尝试过 Raycasting,但我觉得有些不对劲。

这是模型的加载方式

loader.options.convertUpAxis = true;
loader.load( 'model/cityhall.dae', function ( collada ) {

dae = collada.scene;
skin = collada.skins[ 0 ];
model_geometry = collada.scene.children[ 0 ].children[0].geometry;
model_material = collada.scene.children[ 0 ].children[0].material;

dae.scale.x = dae.scale.y = dae.scale.z = 1;
dae.updateMatrix();
scene.add( dae );
dae.traverse(function (child) {
if (child instanceof THREE.Mesh) {
                        objects.push(child);
                    }
                });
            });

raycaster

            rayFloor = new THREE.Raycaster();
            rayFloor.ray.direction.set( 0, -1, 0 );

            rayWall = new THREE.Raycaster();
            rayWall.ray.direction.set(0,0,1);

这是动画

            function animate() {
            requestAnimationFrame( animate );

            controls.isOnObject( false );
                            controls.isWallCollision( false);

            rayFloor.ray.origin.copy( controls.getObject().position );
            rayFloor.ray.origin.x -= 10;

            rayWall.ray.origin.copy( controls.getObject().position );
            rayWall.ray.origin.y -= 8;

            var intersections = rayFloor.intersectObjects( objects );
            var intersections2 = rayWall.intersectObjects( objects );

            if ( intersections.length > 0 ) {
                //console.log('floor' + intersections);

                var distance = intersections[ 0 ].distance;
                if ( distance > 0 && distance < 10 ) {
                    controls.isOnObject( true );
                }

            }

            if ( intersections2.length > 0 ) {
                //console.log('wall' + intersections);

                var distance = intersections2[ 0 ].distance;
                if ( distance > 0 && distance < 10 ) {
                    controls.isWallCollision( true );
                }

            }
            controls.update( Date.now() - time );
            render();

            time = Date.now();
        }
问题是它无法正确检测墙壁和地板。

1 个答案:

答案 0 :(得分:1)

以下是使用Raycaster的碰撞检测工作示例的链接:

http://stemkoski.github.com/Three.js/Collision-Detection.html

希望它有所帮助!