Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Three.js 2D对象与raycaster的碰撞_Three.js_2d_Collision - Fatal编程技术网

Three.js 2D对象与raycaster的碰撞

Three.js 2D对象与raycaster的碰撞,three.js,2d,collision,Three.js,2d,Collision,我试图检测平面上两个2D对象(例如,两个三点圆几何)的碰撞 我的问题:是否有推荐的方法来检测2D objet碰撞?我在下面尝试了两次,但都不好。如果有人以前有这方面的经验,请你帮忙好吗?先谢谢你 这是我的第一次尝试: //'player' is the Circle that I'm controlling. In the scene, there is another position-fixed circle, called 'target'. // I hope to cast a ray

我试图检测平面上两个2D对象(例如,两个三点圆几何)的碰撞

我的问题:是否有推荐的方法来检测2D objet碰撞?我在下面尝试了两次,但都不好。如果有人以前有这方面的经验,请你帮忙好吗?先谢谢你

这是我的第一次尝试:

//'player' is the Circle that I'm controlling. In the scene, there is another position-fixed circle, called 'target'.
// I hope to cast a ray from player to the target and detect collision.
// 'mouse' is a normalized direction THREE.Vector3()

raycaster.set(player.position, mouse);
var intersects = raycaster.intersectObjects( scene.children );
for ( var i = 0; i < intersects.length; i++ ) {
    console.log("TEST");
    intersects[ i ].object.material.color.set( 0xff0000 );
}
var dis = distance(target.position, player.position);
if (dis < 1) {
target.material.color.set(0xff0000);
}
/“player”是我控制的圈子。在场景中,有另一个位置固定的圆,称为“目标”。
//我希望从玩家身上投射光线到目标上,并检测碰撞。
//“鼠标”是一个标准化的方向3.Vector3()
raycaster.set(player.position,鼠标);
var intersects=raycaster.intersectObjects(scene.children);
对于(变量i=0;i
显然,这不起作用。我已经意识到raycaster似乎无法处理2D对象。(我用3D对象进行了测试,效果非常好)

第二次尝试来了:

//'player' is the Circle that I'm controlling. In the scene, there is another position-fixed circle, called 'target'.
// I hope to cast a ray from player to the target and detect collision.
// 'mouse' is a normalized direction THREE.Vector3()

raycaster.set(player.position, mouse);
var intersects = raycaster.intersectObjects( scene.children );
for ( var i = 0; i < intersects.length; i++ ) {
    console.log("TEST");
    intersects[ i ].object.material.color.set( 0xff0000 );
}
var dis = distance(target.position, player.position);
if (dis < 1) {
target.material.color.set(0xff0000);
}
var dis=距离(target.position,player.position);
如果(dis<1){
target.material.color.set(0xff0000);
}
此方法有效,我可以检测两个圆圈之间的碰撞:玩家和目标但是,它会导致真正的糟糕性能,因为每次渲染时绝对不需要检查数千个目标()


如果有人有任何想法或任何推荐阅读,请您提出建议?我尝试在线搜索,但大多数教程和示例都是针对3D碰撞的。提前谢谢。

我知道这是一篇老文章,但也许其他人需要

我推荐你建议的第二种方法,但是使用圆的子集

在一张等比地图中,你有1000000个障碍物,当然,如果你检查每个障碍物的碰撞情况,你会在几年后结束,但是如果你在地图上有一个网格,并将这些圆圈放在与位置相对应的单元格中,你可以只比较玩家周围的圆圈,只比较玩家占据的单元格的圆圈

在thas中,如果你对这个圆做一个简单的碰撞验证,速度很快