private static _Collision BoxToSphereCollision(_ColliderBox box, _ColliderSphere sphere, bool boxMe) { /** * Se utiliza Inverse Transform para transformar todas las posiciones y * direcciones a un sistema de coordenadas local donde es más sencillo * calcular los choques en una dimensión */ Vector3 bPos = box.Transform.position; Vector3 sPos = InverseTransformPointUnscaled(box.Transform, sphere.Transform.position); // Si no está fuera de un borde con el cual chequear significa que está adentro, por lo que // esa coordenada no se utilizará para el calculo. Vector3 testPoint = sPos; if (sPos.x < -box.BoxSize.x / 2) { testPoint.x = -box.BoxSize.x / 2; } else if (sPos.x > box.BoxSize.x / 2) { testPoint.x = box.BoxSize.x / 2; } if (sPos.y < -box.BoxSize.y / 2) { testPoint.y = -box.BoxSize.y / 2; } else if (sPos.y > box.BoxSize.y / 2) { testPoint.y = box.BoxSize.y / 2; } if (sPos.z < -box.BoxSize.z / 2) { testPoint.z = -box.BoxSize.z / 2; } else if (sPos.z > box.BoxSize.z / 2) { testPoint.z = box.BoxSize.z / 2; } float distance = (sPos - testPoint).magnitude; if (distance <= sphere.Radius) { Vector3 extentPointBox = testPoint; Vector3 extentPointSphere = sPos + (testPoint - sPos).normalized * sphere.Extent; Vector3 collisionPoint = TransformPointUnscaled(box.Transform, (extentPointBox + extentPointSphere) / 2); Vector3 normalUnit = box.Transform.TransformDirection(sPos - testPoint).normalized; return(new _Collision(collisionPoint, (extentPointBox - extentPointSphere).magnitude, normalUnit, boxMe ? (_Collider)sphere : (_Collider)box)); } else { return(null); } }
public static _Collision SphereToBoxCollision(_ColliderSphere me, _ColliderBox other) { return(BoxToSphereCollision(other, me, false)); }
public static _Collision BoxToSphereCollision(_ColliderBox me, _ColliderSphere other) { return(BoxToSphereCollision(me, other, true)); }