Projectile Collision Detection
by John Carroll · in Torque Game Engine Advanced · 04/11/2007 (1:35 pm) · 0 replies
Hi, I'm not sure this qualifies as a bug in the strictest sense, but if static objects have faces that are too small, they are discarded by the addToHull routine in tsmesh.cpp. Here is what we did to fix this problem:
This is the original code:
And this is the fixed code:
This will enable collision detection for much smaller shapes.
This is the original code:
bool TSMesh::addToHull(U32 idx0, U32 idx1, U32 idx2)
{
Point3F normal;
mCross(verts[idx2]-verts[idx0],verts[idx1]-verts[idx0],&normal);
if (mDot(normal,normal)<0.001f)
{
mCross(verts[idx0]-verts[idx1],verts[idx2]-verts[idx1],&normal);
if (mDot(normal,normal)<0.001f)
{
mCross(verts[idx1]-verts[idx2],verts[idx0]-verts[idx2],&normal);
if (mDot(normal,normal)<0.001f)
return false;
}
}
normal.normalize();
F32 k = mDot(normal,verts[idx0]);
for (S32 i=0; i<planeNormals.size(); i++)
{
if (mDot(planeNormals[i],normal)>0.99f && mFabs(k-planeConstants[i])<0.01f)
// this is a repeat...
return false;
}
// new plane, add it to the list...
planeNormals.push_back(normal);
planeConstants.push_back(k);
return true;
}And this is the fixed code:
bool TSMesh::addToHull(U32 idx0, U32 idx1, U32 idx2)
{
// calculate the normal of this triangle... remember, we lose precision
// when we subtract two large numbers that are very close to each other,
// so depending on how we calculate the normal, we could get a
// different result. so, we will calculate the normal three different
// ways and take the one that gives us the largest vector before we
// normalize.
Point3F normal1, normal2, normal3;
mCross(verts[idx2]-verts[idx0],verts[idx1]-verts[idx0],&normal1);
mCross(verts[idx0]-verts[idx1],verts[idx2]-verts[idx1],&normal2);
mCross(verts[idx1]-verts[idx2],verts[idx0]-verts[idx2],&normal3);
Point3F normal = normal1;
F32 greatestMagSquared = mDot(normal1, normal1);
F32 magSquared = mDot(normal2, normal2);
if (magSquared > greatestMagSquared) {
normal = normal2;
greatestMagSquared = magSquared;
}
magSquared = mDot(normal3, normal3);
if (magSquared > greatestMagSquared) {
normal = normal3;
greatestMagSquared = magSquared;
}
if (mDot(normal, normal) < 0.00000001f)
return false;
normal.normalize();
F32 k = mDot(normal,verts[idx0]);
for (S32 i=0; i<planeNormals.size(); i++)
{
if (mDot(planeNormals[i],normal)>0.99f && mFabs(k-planeConstants[i])<0.01f)
// this is a repeat...
return false;
}
// new plane, add it to the list...
planeNormals.push_back(normal);
planeConstants.push_back(k);
return true;
}This will enable collision detection for much smaller shapes.
About the author
Working on using Torque as an Image Generator for Tactical Simulations under laser and live fire conditions, for Law Enforcement/Military applications.