美文网首页
碰撞检测

碰撞检测

作者: 不决书 | 来源:发表于2019-10-14 20:56 被阅读0次
18.png

利用射线进行碰撞检测,关键代码如下:

for (var vertexIndex = 0; vertexIndex < MovingCube.geometry.vertices.length; vertexIndex++)
    {  
//获取cube上的每个顶点
 var localVertex = MovingCube.geometry.vertices[vertexIndex].clone();  
    //转换到世界坐标
 var globalVertex = localVertex.applyMatrix4( MovingCube.matrix );
   //中心位置到顶点作为射线的方向
 var directionVector = globalVertex.sub( MovingCube.position );
 
//中心点是射线的起点
 var ray = new THREE.Raycaster( originPoint, directionVector.clone().normalize() );
//判断collidableMeshList中的物体是否被射线击中
 var collisionResults = ray.intersectObjects( collidableMeshList );
//如果存在,并且距离小于顶点到中心点的长度,就认为碰撞了
 if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length() )
    appendText(" Hit ");
    }

FYI:

https://stemkoski.github.io/Three.js/Collision-Detection.html

相关文章

网友评论

      本文标题:碰撞检测

      本文链接:https://www.haomeiwen.com/subject/xhssuctx.html