Exemple #1
0
        /**
         * Check if agent intersect with shape
         *
         * Agent will automatically detect which shape is used to calculate collision
         **/
        public virtual CollisionResult IntersectWithShape(ConvexShape shape)
        {
            switch (shape.ShapeId)
            {
            case ConvexShapeID.Rectangle:
                ConvexRect rectShape = shape as ConvexRect;
                if (rectShape == null)
                {
                                        #if DEBUG
                    Debug.LogError("Unable to down cast ConvexShape to ConvexRect");
                                        #endif
                }
                return(ContactWithRectangle(rectShape));

            case ConvexShapeID.Circle:
                ConvexCircle circleShape = shape as ConvexCircle;
                if (circleShape == null)
                {
                                        #if DEBUG
                    Debug.LogError("Unable to down cast ConvexShape to ConvexCircle");
                                        #endif
                }
                return(ContactWIthCircle(circleShape));

            case ConvexShapeID.Unknow:
                                #if DEBUG
                Debug.LogError("Unknow convex shape");
                                #endif
                break;
            }

            return(CollisionResult.None);
        }
Exemple #2
0
        protected override CollisionResult ContactWIthCircle(ConvexCircle otherCircle)
        {
            bool collision = true;

            Vector2 p = otherCircle.Center - _center;

            //This circle projection
            float circleProj    = Vector2.Dot(p.normalized, _center);
            float circleProjMin = circleProj - _radius;
            float circleProjMax = circleProj + _radius;

            //Another circle projection
            float otherCircleProj    = Vector2.Dot(p.normalized, otherCircle.Center);
            float otherCircleProjMin = otherCircleProj - otherCircle.Radius;
            float otherCircleProjMax = otherCircleProj + otherCircle.Radius;

            if (circleProjMin > otherCircleProjMax || otherCircleProjMin > circleProjMax)
            {
                collision = false;
            }

            //Check if circle fit inside another circle
            if (collision == true)
            {
                if (circleProjMin > otherCircleProjMin && circleProjMax < otherCircleProjMax)
                {
                    return(CollisionResult.Fit);
                }

                return(CollisionResult.Overlap);
            }

            return(CollisionResult.None);
        }
Exemple #3
0
        protected override CollisionResult ContactWithCircle(ConvexCircle otherCircle)
        {
            //We use circle collide rect to chcek
            //reduce duplicate code because code is all most the same only
            //rectangle inside circle need to be checked
            CollisionResult result = otherCircle.IntersectWithShape(this);

            //Check rectangle is inside circle when overlap
            if (result == CollisionResult.Overlap)
            {
                /**
                 * For each corner of recntangle
                 * we check if corner inside circle
                 *
                 * Return fit if all corner insdie circle
                 **/
                Vector2[] corners = AllCorners;

                IEnumerator ie = corners.GetEnumerator();
                while (ie.MoveNext())
                {
                    //projection axis from corner to circle center
                    Vector2 p = otherCircle.Center - (Vector2)ie.Current;

                    //corner projection
                    float cornerP = Vector2.Dot(p.normalized, (Vector2)ie.Current);

                    //circle center projection
                    float circleP = Vector2.Dot(p.normalized, otherCircle.Center);


                    //if corner projection is outside of circle return overlap
                    if ((circleP - otherCircle.Radius) > cornerP || (circleP + otherCircle.Radius) < cornerP)
                    {
                        return(result);
                    }
                }

                return(CollisionResult.Fit);
            }

            //If circle fit in this rectangle return overlap
            //as from rectangle poit of view rectangle overlap circle
            if (result == CollisionResult.Fit)
            {
                return(CollisionResult.Overlap);
            }

            return(result);
        }
Exemple #4
0
 /**
  * Subclass must override
  **/
 protected abstract CollisionResult ContactWIthCircle(ConvexCircle otherCircle);
Exemple #5
0
 protected override CollisionResult ContactWithCircle(ConvexCircle otherCircle)
 {
     return(otherCircle.IntersectWithShape(this));
 }