So, I have an interval halving test that works with AABBs, which does a good job of correcting the positioning of objects, so they don't penetrate past each other too far. Now, all I need to do is find which face the objects collided on, and perform the correct collision response for the direction the objects collided at.
For testing intersection I'm testing the absolute value of the objects against their half-widths.
public bool Intersect(AABB3D a, AABB3D b)
{
if (Math.Abs(a.Center.x - b.Center.x) > (a.HalfWidth + b.HalfWidth))
return false;
if (Math.Abs(a.Center.y - b.Center.y) > (a.HalfHeight + b.HalfHeight))
return false;
if (Math.Abs(a.Center.z - b.Center.z) > (a.HalfDepth + b.HalfDepth))
return false;
return true;
}
When I'm attempting to find which precomputed normal the collision happened on I'm running code like this for each face:
Vector3 bottomVector = b.Bottom.Normal + b.DistanceToBottom;
float top = planeIntersection.ClosestVector3ToPlane(bottomVector, b1.Top);
public float ClosestVector3ToPlane(Vector3 q, Plane p)
{
return (Vector3.Dot(p.Normal, q) - p.D) / Vector3.Dot(p.Normal, p.Normal); //q - t * p.Normal;
}
Then I assign the respective normal like so:
private void normalCollision(float top, float bottom, float left, float right, AABB3D movingBox, AABB3D b0)
{
if(Math.Abs(top) > Math.Abs(bottom))
{
Debug.Log("Top: " + top);
b0.NormalCollision[1] = movingBox.Top.Normal;
}
else if(Math.Abs(bottom) > Math.Abs(top))
{
Debug.Log("Bottom: " + bottom);
b0.NormalCollision[1] = movingBox.Bottom.Normal;
}
if (Math.Abs(right) > Math.Abs(left))
{
Debug.Log("Right: " + right);
b0.NormalCollision[0] = movingBox.Right.Normal;
}
else if (Math.Abs(left) > Math.Abs(right))
{
Debug.Log("Left: " + left);
b0.NormalCollision[0] = movingBox.Left.Normal;
}
}
Outside of the Interval halved test the appropriate response is assigned.
The one thing I noticed when debugging is towards the center of the platform the left and right normals still get assigned; So, my solution needs some work. How could I go about figuring out which face collided with? SAT doesn't seem ideal here since the left and right faces could be penetrating the platform ever so slightly. Thanks.