我正在尝试检测C ++和OpenGL中的球体和三角形之间的碰撞,但是遇到了麻烦。我从《实时碰撞检测》一书中获得了这种方法,但是这会引起很多错误标记,并且某些方法无法正常工作。如何准确检测球体和三角形之间的碰撞?
这是我在代码中感到疲倦的地方,该函数采用球体的位置,半径和3个三角形顶点:
bool CollisionHelper::isSphereIntersectingTriangle(glm::vec3 sphere, float radius, glm::vec3 tri1, glm::vec3 tri2, glm::vec3 tri3)
{
float dist1 = glm::sqrt((sphere.x - tri1.x) * (sphere.x - tri1.x) + (sphere.y - tri1.y) * (sphere.y - tri1.y) + (sphere.z - tri1.z) * (sphere.z - tri1.z));
float dist2 = glm::sqrt((sphere.x - tri2.x) * (sphere.x - tri2.x) + (sphere.y - tri2.y) * (sphere.y - tri2.y) + (sphere.z - tri2.z) * (sphere.z - tri2.z));
float dist3 = glm::sqrt((sphere.x - tri3.x) * (sphere.x - tri3.x) + (sphere.y - tri3.y) * (sphere.y - tri3.y) + (sphere.z - tri3.z) * (sphere.z - tri3.z));
float closestDist = glm::min(glm::min(dist1, dist2), dist3);
glm::vec3 v;
if (closestDist == dist1)
v = tri1 - sphere;
else if (closestDist == dist2)
v = tri2 - sphere;
else (closestDist == dist3)
v = tri3 - sphere;
return glm::dot(v, v) <= radius * radius;
}
答案 0 :(得分:1)
这是我要执行的测试。 首先,我将坐标转换为球体的中心为(0,0,0)。 最容易检查的是其中一个角是否在内部。 接下来是检查边缘之一是否在切割。 最后,我将测试球体是否通过平面而不切割边缘。 为此,我将计算由三角形给出的平面到原点的正交距离。这也给出了平面法线开始的点。如果它比半径短并且在三角形周长之内,请完成。
修改
这里有一些python代码可以澄清
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1, projection='3d' )
# Make data
u = np.linspace( 0, .5 * np.pi, 15 )
v = np.linspace( 0, .5 * np.pi, 15 )
### radius is 1 but problem can be scaled
R = 1
x = R * np.outer(np.cos(u), np.sin(v) )
y = R * np.outer(np.sin(u), np.sin(v) )
z = R * np.outer(np.ones(np.size(u) ), np.cos(v) )
"""
Random Test
"""
# ~scale = 3
# ~A = np.array( (
# ~scale * np.random.random(),
# ~scale * np.random.random(),
# ~scale * np.random.random() )
# ~)
# ~B = np.array( (
# ~scale * np.random.random(),
# ~scale * np.random.random(),
# ~scale * np.random.random() )
# ~)
# ~C = np.array( (
# ~scale * np.random.random(),
# ~scale * np.random.random(),
# ~scale * np.random.random() )
# ~)
"""
TestCases
"""
if 0: #definitive outside
A = np.array( [ 1.3, 0.4, 0.6 ] )
B = np.array( [ 1.3, 1.4, 0.6 ] )
C = np.array( [ 1.3, 0.4, 1.6 ] )
if 0: # outside but plane normal inside
A = np.array( [ 1.3, 0.4, 0.6 ] )
B = np.array( [ 0.7, 1.4, 0.6 ] )
C = np.array( [ 1.8, 0.4, 1.0 ] )
if 0: # cutting edge
A = np.array( [ 1.1, 0.0, 0.1 ] )
B = np.array( [ 0.1, 1.4, -0.2 ] )
C = np.array( [ 1.8, 0.4, 1.0 ] )
if 1: # cutting plane
A = np.array( [ 1.4, 0.0, 0.1 ] )
B = np.array( [ 0.1, 1.4, -0.2 ] )
C = np.array( [ -0.03, 0.1, 2.0 ] )
"""
Most simple check:
is one of the vertices indside
"""
print np.linalg.norm( A ), np.linalg.norm( A ) < R
print np.linalg.norm( B ), np.linalg.norm( B ) < R
print np.linalg.norm( C ), np.linalg.norm( C ) < R
"""
checking if one edge cuts the sphere
this uses simple derivatives of the distance function
"""
for F,G in [ ( B, A ), (C, B), (A, C)]:
a = F - G
s = -np.dot( a, G )/ np.dot( a, a )
print "s: ", s, s > 0 and s < 1
d = np.linalg.norm( G + s * a )
print "d: ", d, d < R
### if both are true, it is cutting
print "---------"
"""
checking if the sphere cuts the area
e.g in the extreme case of (but not restricted to) a sphere
passing through
"""
a = B - A
c = C - A
aa = np.dot( a, a)
cc = np.dot( c, c)
ac = np.dot( a, c)
aA = np.dot( a, A)
cA = np.dot( c, A)
MI = np.array( [
np.array([ cc, -ac ] ),
np.array([ -ac, aa ] )
])
MI /= ( aa * cc - ac**2 ) ### div by det
st = np.dot( MI, [ -aA, -cA ] )
s=st[0]
t=st[1]
P = A + s * a + t * c
"""
If this is larger than R we can stop here
if otherwise we detect if P inside triangle by repeating the stuff
above with respect to B
"""
a2 = A - B
c2 = C - B
aa2 = np.dot( a2, a2 )
cc2 = np.dot( c2, c2 )
ac2 = np.dot( a2, c2 )
aB2 = np.dot( a2, B )
cB2 = np.dot( c2, B )
MI2 = np.array( [
np.array([ +cc2, -ac2 ] ),
np.array([ -ac2, +aa2 ] )
])
MI2 /= ( aa2 * cc2 - ac2**2 )
uv = np.dot( MI2, [ -aB2, -cB2 ] )
u = uv[0]
v = uv[1]
P2 = B + u * a2 + v * c2
print "must be identical"
print P, np.linalg.norm( P ) < R
print P2, np.linalg.norm( P2 ) < R
print "is inside if all 4 are positive"
print s, t, u, v
### finally some plotting
verts = [ [ A, B, C ] ]
srf = Poly3DCollection( verts, alpha=.9, facecolor='#800000' )
ax.plot_wireframe( x, y, z, color='b' )
ax.plot( [ 0, P[0] ], [ 0, P[1] ], [ 0, P[2] ] )
ax.add_collection3d(srf)
ax.set_xlim( [-0.5, 2 ] )
ax.set_ylim( [-0.5, 2 ] )
ax.set_zlim( [-0.5, 2 ] )
plt.show()