我在THREE.js库中遇到了碰撞检测问题。下面我找到了一个指向我的示例的链接(使用箭头键移动一个正文对象):
http://jsfiddle.net/flyenet/uJLUU/7/
在这个例子中,我想创建自己的几何图形(用于“浮雕”和“体”)并检测它们之间的碰撞。当身体的顶点位于浮雕内部时,左上角的红色方块具有不透明度1,否则它是半透明的。
身体几何:
var up = new THREE.CylinderGeometry(1, 1, 0.2, 3, 1),
down = new THREE.CylinderGeometry(1, 1, 0.2, 3, 1);
for (var i = 0, l = up.vertices.length; i < l; i++) {
up.vertices[i].x *= 1.75;
up.vertices[i].z = up.vertices[i].z * 0.5;
down.vertices[i].x *= -0.7;
down.vertices[i].z = -down.vertices[i].z * 2.3 + 1;
}
THREE.GeometryUtils.merge(up, down);
救济几何:
var geometry = new THREE.CubeGeometry(1, 32, 32, 1, 16, 16);
geometry.applyMatrix(new THREE.Matrix4().makeRotationZ(Math.PI / 2));
for (var i = 0, l = geometry.vertices.length; i < l; i++) {
var x = Math.floor(i / 17),
y = Math.floor(i / 289),
z = i % 17;
geometry.vertices[i].y = y === 0 ? (x - 8) * (x - 8) * 0.1 : -10;
}
检测碰撞情况的功能:
function test() {
var originPoint = body.position.clone();
for (var vi = 0, vl = body.geometry.vertices.length; vi < vl; vi++) {
var localVertex = body.geometry.vertices[vi].clone(),
globalVertex = body.matrix.multiplyVector3(localVertex),
directionVector = globalVertex.subSelf(body.position),
ray = new THREE.Raycaster(originPoint, directionVector.clone().normalize()),
results = ray.intersectObjects([relief]);
if (results.length > 0 && results[0].distance < directionVector.length()) return true;
}
return false;
}
它有效,但有时候,身体位于浮雕的边框上,红色正方形是半透明的(而不是透明度1)。它看起来像这样:
这意味着,碰撞检测没有发射。为什么?我应该在代码中更改什么来检测任何碰撞情况?