private static double TransformToAxis(CollisionBox box, Vector3d axis) { return (box.HalfSize.x * Vector3d.AbsDot(axis, box.GetAxis(0)) + box.HalfSize.y * Vector3d.AbsDot(axis, box.GetAxis(1)) + box.HalfSize.z * Vector3d.AbsDot(axis, box.GetAxis(2))); }
/// <summary> /// This function checks if the two boxes overlap /// along the given axis.The final parameter toCentre /// is used to pass in the vector between the boxes centre /// points, to avoid having to recalculate it each time. /// </summary> private static bool OverlapOnAxis(CollisionBox one, CollisionBox two, Vector3d axis, Vector3d toCentre) { // Project the half-size of one onto axis double oneProject = TransformToAxis(one, axis); double twoProject = TransformToAxis(two, axis); // Project this onto the axis double distance = Vector3d.AbsDot(toCentre, axis); // Check for overlap return(distance < oneProject + twoProject); }
/// <summary> /// This function checks if the two boxes overlap /// along the given axis, returning the ammount of overlap. /// The final parameter toCentre /// is used to pass in the vector between the boxes centre /// points, to avoid having to recalculate it each time. /// </summary> private static double PenetrationOnAxis(CollisionBox one, CollisionBox two, Vector3d axis, Vector3d toCentre) { // Project the half-size of one onto axis double oneProject = TransformToAxis(one, axis); double twoProject = TransformToAxis(two, axis); // Project this onto the axis double distance = Vector3d.AbsDot(toCentre, axis); // Return the overlap (i.e. positive indicates // overlap, negative indicates separation). return(oneProject + twoProject - distance); }