示例#1
0
    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);
        }
    }
示例#2
0
    void Start()
    {
        lineRenderer   = gameObject.AddComponent <LineRenderer>();
        colliderSphere = GetComponent <_ColliderSphere>();
        rigidbody      = GetComponent <_Rigidbody>();

        lineRenderer.startWidth = 0;
        lineRenderer.endWidth   = 0.2f;
        lineRenderer.enabled    = false;
        lineRenderer.material   = lineMaterial;
    }
示例#3
0
    // ---> Collision Detection

    public static _Collision SphereToSphereCollision(_ColliderSphere me, _ColliderSphere other)
    {
        Vector3 separation        = me.Transform.position - other.Transform.position;
        float   collisionDistance = me.Radius + other.Radius;

        if (separation.magnitude <= collisionDistance)
        {
            Vector3 extentPointMe    = Vector3.MoveTowards(me.Transform.position, other.Transform.position, me.Extent);
            Vector3 extentPointOther = Vector3.MoveTowards(other.Transform.position, me.Transform.position, other.Extent);

            Vector3 collisionPoint = (extentPointMe + extentPointOther) / 2;
            Vector3 normalUnit     = separation.normalized;

            return(new _Collision(collisionPoint, (extentPointMe - extentPointOther).magnitude, normalUnit, other));
        }
        else
        {
            return(null);
        }
    }
示例#4
0
 public static _Collision SphereToBoxCollision(_ColliderSphere me, _ColliderBox other)
 {
     return(BoxToSphereCollision(other, me, false));
 }
示例#5
0
 public static _Collision BoxToSphereCollision(_ColliderBox me, _ColliderSphere other)
 {
     return(BoxToSphereCollision(me, other, true));
 }