예제 #1
0
    // EXAMEN: Colision entre AABB
    public static Tuple <bool, CustomCollision> AABBCollision(CustomBoxCollider2D aabb1, CustomBoxCollider2D aabb2)
    {
        Vector3 aabb1Position = aabb1.Center - new Vector3(aabb1.Width / 2, aabb1.Height / 2);
        Vector3 aabb2Position = aabb2.Center - new Vector3(aabb2.Width / 2, aabb2.Height / 2);

        var collided = (aabb1Position.x <aabb2Position.x + aabb2.Width &&
                                         aabb1Position.x + aabb1.Width> aabb2Position.x &&
                        aabb1Position.y <aabb2Position.y + aabb2.Height &&
                                         aabb1Position.y + aabb1.Height> aabb2Position.y);
        CustomCollision customCollision = new CustomCollision(Vector3.zero, aabb2);

        return(new Tuple <bool, CustomCollision>(collided, customCollision));
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="r1"></param>
    /// <param name="r2"></param>
    /// <returns></returns>
    public static Vector2 IntersectionPointRectOnRect(CustomBoxCollider2D nonstaticCollider, CustomBoxCollider2D staticCollider, bool collidedVertically = true)
    {
        Vector2 intersedctionPoint = Vector2.zero;
        float   yPoint;
        float   xPoint;

        if (collidedVertically)
        {
            xPoint = nonstaticCollider.bounds.topLeft.x;
            return(IntersectionPointColliderVerticalAtXPoint(nonstaticCollider, staticCollider, xPoint));
        }
        else
        {
            yPoint = nonstaticCollider.bounds.bottomRight.y;
            return(IntersectionPointColliderHorizontalAtYPoint(nonstaticCollider, staticCollider, yPoint));
        }
    }
    /// <summary>
    /// Returns the collision point of the of the two collider bounds
    /// </summary>
    /// <param name="r1"></param>
    /// <param name="c1"></param>
    /// <returns></returns>
    public static Vector2 IntersectionPointNonstaticRectOnStaticCircle(CustomBoxCollider2D nonstaticRectCollider, CustomCircleCollider2D staticCircleCollider, bool collidedVertically = true)
    {
        BoundsRect   r1 = nonstaticRectCollider.bounds;
        BoundsCircle c1 = staticCircleCollider.bounds;

        Vector2 centerPointOfCircle = c1.center;
        Vector2 collisionPoint;

        if (collidedVertically)
        {
            if (centerPointOfCircle.x < r1.bottomLeft.x)
            {
                collisionPoint.x = r1.bottomLeft.x;
            }
            else if (centerPointOfCircle.x > r1.bottomRight.x)
            {
                collisionPoint.x = r1.bottomRight.x;
            }
            else
            {
                collisionPoint.x = centerPointOfCircle.x;
            }

            return(IntersectionPointColliderVerticalAtXPoint(nonstaticRectCollider, staticCircleCollider, collisionPoint.x));
        }
        else
        {
            if (centerPointOfCircle.y < r1.bottomLeft.y)
            {
                collisionPoint.y = r1.bottomLeft.y;
            }
            else if (centerPointOfCircle.y > r1.topLeft.y)
            {
                collisionPoint.y = r1.topLeft.y;
            }
            else
            {
                collisionPoint.y = centerPointOfCircle.y;
            }
            return(IntersectionPointColliderHorizontalAtYPoint(nonstaticRectCollider, staticCircleCollider, collisionPoint.y));
        }
    }
예제 #4
0
 public override Tuple <bool, CustomCollision> Collide(CustomBoxCollider2D collider)
 {
     return(Collision.CircleAABBCollision(this, collider));
 }
    public static Vector2 IntersectionPointStaticCapsuleNonStaticRect(CustomCapsuleCollider2D staticCapsule, CustomBoxCollider2D nonstaticRect, bool collidedVertically = true)
    {
        Vector2 rCenter   = nonstaticRect.GetCenter();
        Vector2 capCenter = staticCapsule.GetCenter();

        if (collidedVertically)
        {
            float xPoint = rCenter.x;
            if (nonstaticRect.bounds.topRight.x < rCenter.x)
            {
                xPoint = nonstaticRect.bounds.topRight.x;
            }
            else if (nonstaticRect.bounds.topLeft.x > rCenter.x)
            {
                xPoint = nonstaticRect.bounds.topLeft.x;
            }

            return(IntersectionPointColliderVerticalAtXPoint(nonstaticRect, staticCapsule, xPoint));
        }
        else
        {
            float yPoint = rCenter.y;
            if (nonstaticRect.bounds.topLeft.y < staticCapsule.bounds.rectBounds.bottomLeft.y)
            {
                yPoint = nonstaticRect.bounds.topLeft.y;
            }
            else if (nonstaticRect.bounds.bottomLeft.y > staticCapsule.bounds.rectBounds.topLeft.y)
            {
                yPoint = nonstaticRect.bounds.bottomLeft.y;
            }

            return(IntersectionPointColliderHorizontalAtYPoint(nonstaticRect, staticCapsule, yPoint));
        }
    }
예제 #6
0
    // EXAMEN: Colision entre AABB y circulo + Normal
    public static Tuple <bool, CustomCollision> CircleAABBCollision(CustomSphereCollider2D circle, CustomBoxCollider2D aabb)
    {
        Vector3 dir = aabb.Center - circle.Center;

        dir  = dir.normalized;
        dir *= circle.Radius;
        Vector3 pos = circle.Center + dir;

        var aabbCenter = aabb.Center - new Vector3(aabb.Width / 2, aabb.Height / 2);
        var collided   = (aabbCenter.x <pos.x &&
                                        aabbCenter.x + aabb.Width> pos.x &&
                          aabbCenter.y <pos.y &&
                                        aabbCenter.y + aabb.Height> pos.y);

        Vector3 normal = Vector3.zero;

        Vector3 max = aabb.Center + new Vector3(aabb.Width / 2, aabb.Height / 2);
        Vector3 min = aabb.Center + new Vector3(-aabb.Width / 2, -aabb.Height / 2);

        if (max.x > circle.PreviousCenter.x && min.x < circle.PreviousCenter.x)
        {
            if (circle.PreviousCenter.x > aabb.Center.x)
            {
                normal = Vector2.up;
            }
            else
            {
                normal = Vector2.down;
            }
        }

        if (max.y > circle.PreviousCenter.y && min.y < circle.PreviousCenter.y)
        {
            if (circle.PreviousCenter.y > aabb.Center.y)
            {
                normal = Vector3.left;
            }
            else
            {
                normal = Vector3.right;
            }
        }

        Vector3 dirVector      = aabb.Center - circle.PreviousCenter;
        Vector3 closestPoint   = dirVector.normalized * circle.Radius;
        Vector3 collisionPoint = circle.PreviousCenter + closestPoint;

        CustomCollision customCollision = new CustomCollision(circle.PreviousCenter + closestPoint, aabb, normal == Vector3.zero ? circle.PreviousCenter - collisionPoint : normal);

        return(new Tuple <bool, CustomCollision>(collided, customCollision));
    }
예제 #7
0
 public abstract Tuple <bool, CustomCollision> Collide(CustomBoxCollider2D collider);