Sphere-Triangle collision help

Started by
3 comments, last by xissburg 17 years, 10 months ago
here is the case that is failing:

________ Sphere
        |
 wall   |
        |
When the sphere hits a wall at that angle and place, it always fails, all other cases it work great. Here's the code I'm using, can anyone see anything wrong?


bool m3dSphereTriangleTest(tSphere *sphere, tTriangle *tri, M3DVec3f &move)
{

	M3DVec3f vecToCenter;
	m3dVecSub(vecToCenter, sphere->center, tri->m_verts[0]);

	float disToPlane = m3dVecDotProduct(vecToCenter, tri->m_normal);

	if ((disToPlane <= 0) || (disToPlane > sphere->rad))
		return false;

	M3DVec3f projVector = tri->m_normal;
	m3dVecScale(projVector, -disToPlane);
        // projected point on the triangle's plane
	M3DVec3f point;
	m3dVecAdd(point, sphere->center, projVector);
        // point in triangle test
	M3DVec3f edge0, edge1, edge2, normal0, normal1, normal2;
	m3dVecSub(edge0, tri->m_verts[1], tri->m_verts[0]);
	m3dVecSub(edge1, tri->m_verts[2], tri->m_verts[1]);
	m3dVecSub(edge2, tri->m_verts[0], tri->m_verts[2]);

	m3dVecCrossProduct(normal0, edge0, tri->m_normal);
	m3dVecNormalize(normal0);

	m3dVecCrossProduct(normal1, edge1, tri->m_normal);
	m3dVecNormalize(normal1);

	m3dVecCrossProduct(normal2, edge2, tri->m_normal);
	m3dVecNormalize(normal2);

	M3DVec3f testVec0, testVec1, testVec2;
	m3dVecSub(testVec0, point, tri->m_verts[0]);
	m3dVecSub(testVec1, point, tri->m_verts[1]);
	m3dVecSub(testVec2, point, tri->m_verts[2]);
 
	if (m3dVecDotProduct(testVec0, normal0) > 0)
		return false;

	if (m3dVecDotProduct(testVec1, normal1) > 0)
		return false;

	if (m3dVecDotProduct(testVec2, normal2) > 0)
		return false;

        // collision detected, set correction vector
	M3DVec3f moveVec = tri->m_normal;
	m3dVecScale(moveVec, sphere->rad - disToPlane);

	move = moveVec;

	return true;
}


Advertisement
You're missing a step. If the sphere is intersecting the plane of the triangle, but the projection of the sphere center onto the triangle plane doesn't fall within the triangle, that doesn't mean the sphere isn't intersecting the triangle. In this case you need to find the closest point on the perimeter of the triangle to the sphere center and test that difference vector as well.

Let me know if you need any further details.
Quote:Original post by jyk
You're missing a step. If the sphere is intersecting the plane of the triangle, but the projection of the sphere center onto the triangle plane doesn't fall within the triangle, that doesn't mean the sphere isn't intersecting the triangle. In this case you need to find the closest point on the perimeter of the triangle to the sphere center and test that difference vector as well.

Let me know if you need any further details.


How exactly would I find the closest point to the perimeter of the triangle? Do you mean closest vertex to the sphere center?
Quote:Original post by GamerYZ
How exactly would I find the closest point to the perimeter of the triangle? Do you mean closest vertex to the sphere center?
By perimeter I mean the three edges of the triangle (which also includes the three vertices). There are some nice methods for finding the closest point on a triangle to a point all in one go, but to get it up and running quickly I'd just perform three 'closest point on line segment to point' tests, one for each edge, and choose the one with the minimum squared distance.
as I usually say here...try looking at the Soft Surfer algorithms ...
.

This topic is closed to new replies.

Advertisement