private bool RayIntersectsShapeBox(Vector3 pos1RayProj, Vector3 pos2RayProj, Vector3 shapeBoxMax)
{
// Skip if ray can't intersect bounding box;
Vector3 rayBoxProjMin = Vector3.Min(pos1RayProj, pos2RayProj);
Vector3 rayBoxProjMax = Vector3.Max(pos1RayProj, pos2RayProj);
if (
rayBoxProjMin.X > shapeBoxMax.X || rayBoxProjMin.Y > shapeBoxMax.Y || rayBoxProjMin.Z > shapeBoxMax.Z ||
rayBoxProjMax.X < -shapeBoxMax.X || rayBoxProjMax.Y < -shapeBoxMax.Y || rayBoxProjMax.Z < -shapeBoxMax.Z
)
return false;
// Check if ray intersect any bounding box side
int sign = 0;
float dist = 0.0f;
Vector3 posProj = Vector3.Zero;
Vector3 vecRayProj = pos2RayProj - pos1RayProj;
// Check both X sides unless ray is parallell to them
if (Math.Abs(vecRayProj.X) > m_floatToleranceInCastRay)
{
for (sign = -1; sign <= 1; sign += 2)
{
dist = ((float)sign * shapeBoxMax.X - pos1RayProj.X) / vecRayProj.X;
posProj = pos1RayProj + vecRayProj * dist;
if (Math.Abs(posProj.Y) <= shapeBoxMax.Y && Math.Abs(posProj.Z) <= shapeBoxMax.Z)
return true;
}
}
// Check both Y sides unless ray is parallell to them
if (Math.Abs(vecRayProj.Y) > m_floatToleranceInCastRay)
{
for (sign = -1; sign <= 1; sign += 2)
{
dist = ((float)sign * shapeBoxMax.Y - pos1RayProj.Y) / vecRayProj.Y;
posProj = pos1RayProj + vecRayProj * dist;
if (Math.Abs(posProj.X) <= shapeBoxMax.X && Math.Abs(posProj.Z) <= shapeBoxMax.Z)
return true;
}
}
// Check both Z sides unless ray is parallell to them
if (Math.Abs(vecRayProj.Z) > m_floatToleranceInCastRay)
{
for (sign = -1; sign <= 1; sign += 2)
{
dist = ((float)sign * shapeBoxMax.Z - pos1RayProj.Z) / vecRayProj.Z;
posProj = pos1RayProj + vecRayProj * dist;
if (Math.Abs(posProj.X) <= shapeBoxMax.X && Math.Abs(posProj.Y) <= shapeBoxMax.Y)
return true;
}
}
// No hits on bounding box so return false
return false;
}