光线投射作为碰撞检测器:不准确
Raycasting as collision detector: not accurate
我有一个场景有多种随机形式(比如三角形、梯形,还有更多自定义设计),我正在尝试编写碰撞检测代码。这些形状都是二维的,位于 Y=0
上
由于形状比圆形和矩形更复杂,我决定使用光线投射来检查碰撞。
var raycastCollision = function () {
var originPoint = activeElement.position.clone();
var vertices = activeElement.geometry.vertices;
//loop
for (var vertexIndex = 0; vertexIndex < vertices.length; vertexIndex++) {
var localVertex = vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4(activeElement.matrix);
var directionVector = globalVertex.sub(activeElement.position);
var ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize(),true);
ray.ray.direction.y = 0;
var collisionResults = ray.intersectObjects(elements);
debug["ray" + vertexIndex] = ray;
if (collisionResults.length > 0 && collisionResults[0].object != activeElement && collisionResults[0].distance < directionVector.length()) {
debug["raycast detection"] = "HIT";
break;
}
}
}
ActiveElement
是当前选中的形状,elements
是场景中所有形状的列表。
我遇到的问题是它只在某些情况下检测到 "hits",我还不能确定是什么情况。但有一件事是肯定的:它通常不会在应该检测到命中时检测到命中。
谁能检测出我代码中的错误?
编辑:"no hit" 和 "hit" 情况的示例图片
由于我的旧答案不正确,我将其删除。
我在测试场景中尝试了您的功能,以下解决方案有效:
https://jsfiddle.net/qzL9L38a/
我猜你的问题是平行面。
对于球体,以下工作:
var raycastCollision = function () {
var originPoint = spheres[0].position.clone();
var vertices = spheres[0].geometry.vertices;
//loop
for (var vertexIndex = 0; vertexIndex < vertices.length; vertexIndex++) {
var localVertex = vertices[vertexIndex]; // no need to clone if applyMatrix4 won'T change localVertex.
var globalVertex = localVertex.applyMatrix4(spheres[0].matrix);
var directionVector = globalVertex.sub(originPoint);
var ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize(),true);
var collisionResults = ray.intersectObjects(spheres);
collisionResults = collisionResults.filter(function(element)
{
return element.distance < directionVector.length();
});
if (collisionResults.length > 0 ) {
console.log('HIT: '+collisionResults);
break;
}
}
}
我有一个场景有多种随机形式(比如三角形、梯形,还有更多自定义设计),我正在尝试编写碰撞检测代码。这些形状都是二维的,位于 Y=0
由于形状比圆形和矩形更复杂,我决定使用光线投射来检查碰撞。
var raycastCollision = function () {
var originPoint = activeElement.position.clone();
var vertices = activeElement.geometry.vertices;
//loop
for (var vertexIndex = 0; vertexIndex < vertices.length; vertexIndex++) {
var localVertex = vertices[vertexIndex].clone();
var globalVertex = localVertex.applyMatrix4(activeElement.matrix);
var directionVector = globalVertex.sub(activeElement.position);
var ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize(),true);
ray.ray.direction.y = 0;
var collisionResults = ray.intersectObjects(elements);
debug["ray" + vertexIndex] = ray;
if (collisionResults.length > 0 && collisionResults[0].object != activeElement && collisionResults[0].distance < directionVector.length()) {
debug["raycast detection"] = "HIT";
break;
}
}
}
ActiveElement
是当前选中的形状,elements
是场景中所有形状的列表。
我遇到的问题是它只在某些情况下检测到 "hits",我还不能确定是什么情况。但有一件事是肯定的:它通常不会在应该检测到命中时检测到命中。
谁能检测出我代码中的错误?
编辑:"no hit" 和 "hit" 情况的示例图片
由于我的旧答案不正确,我将其删除。
我在测试场景中尝试了您的功能,以下解决方案有效:
https://jsfiddle.net/qzL9L38a/
我猜你的问题是平行面。
对于球体,以下工作:
var raycastCollision = function () {
var originPoint = spheres[0].position.clone();
var vertices = spheres[0].geometry.vertices;
//loop
for (var vertexIndex = 0; vertexIndex < vertices.length; vertexIndex++) {
var localVertex = vertices[vertexIndex]; // no need to clone if applyMatrix4 won'T change localVertex.
var globalVertex = localVertex.applyMatrix4(spheres[0].matrix);
var directionVector = globalVertex.sub(originPoint);
var ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize(),true);
var collisionResults = ray.intersectObjects(spheres);
collisionResults = collisionResults.filter(function(element)
{
return element.distance < directionVector.length();
});
if (collisionResults.length > 0 ) {
console.log('HIT: '+collisionResults);
break;
}
}
}