Exemple #1
0
        /// <summary>
        /// Test and return the status of colliding input primitive with the bounds of the camera window
        /// </summary>
        /// <param name="prim">Primitive to be tested</param>
        /// <returns>Camera Window Collusion Stats: inside or collided with one of the bounds</returns>
        public static CameraWindowCollisionStatus CollidedWithCameraWindow(TexturedPrimitive prim)
        {
            Vector2 min = CameraWindowLowerLeftPosition;
            Vector2 max = CameraWindowUpperRightPosition;

            if (prim.MaxBound.Y > max.Y)
                return CameraWindowCollisionStatus.CollideTop;
            if (prim.MinBound.X < min.X)
                return CameraWindowCollisionStatus.CollideLeft;
            if (prim.MaxBound.X > max.X)
                return CameraWindowCollisionStatus.CollideRight;
            if (prim.MinBound.Y < min.Y)
                return CameraWindowCollisionStatus.CollideBottom;

            return CameraWindowCollisionStatus.InsideWindow;
        }
 /// <summary>
 /// If two circular TexturedPrimitive collides (simple radius check)
 /// </summary>
 /// <param name="otherPrim">Primitive testing for collision</param>
 /// <returns>True: collides</returns>
 public bool PrimitivesTouches(TexturedPrimitive otherPrim)
 {
     Vector2 v = mPosition - otherPrim.Position;
     float dist = v.Length();
     return (dist < ((mSize.X / 2f) + (otherPrim.mSize.X / 2f)));
 }
        //Collision code
        public bool PrimitiveCollision(TexturedPrimitive aOtherPrimitive)
        {
            Vector2 vector = mPosition - aOtherPrimitive.Position;
            float dist = vector.Length();
            //TODO: Get this to work

            //Determines the collision shapes of the primitives
            switch(m_CollisionType)
            {
                case CollisionType.CollisionRectangle:
                    switch(aOtherPrimitive.MyCollisionType)
                    {
                        case CollisionType.CollisionRectangle:
                            return Collision2Rectangles(aOtherPrimitive);
                        case CollisionType.CollisionCircle:
                            return CollisionCircleRectangle(aOtherPrimitive);
                        default:
                            return Collision2Rectangles(aOtherPrimitive);
                    }
                case CollisionType.CollisionCircle:
                    switch (aOtherPrimitive.MyCollisionType)
                    {
                        case CollisionType.CollisionRectangle:
                            return CollisionCircleRectangle(aOtherPrimitive);
                        case CollisionType.CollisionCircle:
                            return Collision2Circles(aOtherPrimitive);
                        default:
                            return CollisionCircleRectangle(aOtherPrimitive);
                    }
                default:
                    switch (aOtherPrimitive.MyCollisionType)
                    {
                        case CollisionType.CollisionRectangle:
                            return Collision2Rectangles(aOtherPrimitive);
                        case CollisionType.CollisionCircle:
                            return CollisionCircleRectangle(aOtherPrimitive);
                        default:
                            return Collision2Rectangles(aOtherPrimitive);
                    }
            }
        }
        public bool CollisionCircleRectangle(TexturedPrimitive aOtherPrimitive)
        {
            //The distance between the two(2) primitives
            Vector2 distance = mPosition - aOtherPrimitive.mPosition;

            //Initial check to see if a collision is possible
            if (Math.Abs(distance.X) > (int)((aOtherPrimitive.mSize.X / 2) + (mSize.X / 2)) || Math.Abs(distance.Y) > (int)((aOtherPrimitive.mSize.Y / 2) + (mSize.Y / 2)))
            {
                //No collision
                return false;
            }

            //From here on is to check for a near miss around one of the corners of the rectangle

            //Circle's position and radius
            Vector2 circlePosition;
            float circleRadius;

            //The corners of the rectangle
            Vector2[] corners = new Vector2[4];

            //Determine which primitive is the circle
            if (m_CollisionType == CollisionType.CollisionCircle)
            {
                //Get the circle's position and radius
                circlePosition = mPosition;
                circleRadius = (float)mSize.X / 2f;

                //Find the positions of the corners
                //Calculate the positions of the corners based on the image size and the origin

                //Bottom left is the simplest to find
                corners[0] = aOtherPrimitive.mPosition - new Vector2(aOtherPrimitive.mSize.X, aOtherPrimitive.mSize.Y);
                //Bottom right
                corners[1] = aOtherPrimitive.mPosition - new Vector2(0, aOtherPrimitive.mSize.Y);
                //Top left
                corners[2] = aOtherPrimitive.mPosition - new Vector2(aOtherPrimitive.mSize.X, 0);
                //Top right
                corners[3] = aOtherPrimitive.mPosition;

            }
            else
            {
                //Get the circle's position and radius
                circlePosition = aOtherPrimitive.mPosition;
                circleRadius = (float)aOtherPrimitive.mSize.X / 2f;

                //Find the positions of the corners
                //Calculate the positions of the corners based on the image size and the origin

                //Bottom left is the simplest to find
                corners[0] = mPosition - new Vector2(mSize.X, mSize.Y);
                //Bottom right
                corners[1] = mPosition - new Vector2(0, mSize.Y);
                //Top left
                corners[2] = mPosition - new Vector2(mSize.X, 0);
                //Top right
                corners[3] = mPosition;
            }

            //Check for collisions with the corners
            if ((corners[0] - circlePosition).Length() < circleRadius ||
                (corners[1] - circlePosition).Length() < circleRadius ||
                (corners[2] - circlePosition).Length() < circleRadius ||
                (corners[3] - circlePosition).Length() < circleRadius)
            {
                //The circle is colliding directly with a corner
                return true;
            }

            //To make things easier on my mind I'm going to sort the corners by distance from the circle
            //After this point assume that the corners got swapped around inside the array
            //Note to Joel: These braces are me attempting to manage memory in C# lol
            {
                Vector2 sorting;

                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if((corners[0] - circlePosition).Length() > (corners[1] - circlePosition).Length())
                        {
                            //This will sort the array so that the closest corner is corners[0] and the farthest is corners[3]
                            sorting = corners[0];
                            corners[0] = corners[1];
                            corners[1] = sorting;
                        }
                    }
                }
            }

            if (corners[0].X == corners[1].X)
            {
                //If the circle is farther from the 2nd and 3rd closest corners the the closest corner is
                //then it is a near miss
                if ((corners[0] - (corners[1] + new Vector2(circleRadius, 0))).Length() < (circlePosition - corners[1]).Length() &&
                    (corners[0] - (corners[2] + new Vector2(0, circleRadius))).Length() < (circlePosition - corners[2]).Length())
                {
                    return false;
                }
            }
            else
            {
                //If the circle is farther from the 2nd and 3rd closest corners the the closest corner is
                //then it is a near miss
                if ((corners[0] - (corners[1] + new Vector2(0, circleRadius))).Length() < (circlePosition - corners[1]).Length() &&
                    (corners[0] - (corners[2] + new Vector2(circleRadius, 0))).Length() < (circlePosition - corners[2]).Length())
                {
                    return false;
                }
            }

            //If it isn't a near miss then it is a hit
            return true;
        }
        public bool Collision2Rectangles(TexturedPrimitive aOtherPrimitive)
        {
            Vector2 distance = mPosition - aOtherPrimitive.mPosition;

            distance.X = Math.Abs(distance.X);
            distance.Y = Math.Abs(distance.Y);

            if (distance.X > (float)((mSize.X / 2) + (aOtherPrimitive.mSize.X / 2)) || distance.Y > (float)((mSize.Y / 2) + (aOtherPrimitive.mSize.Y / 2)))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public bool Collision2Circles(TexturedPrimitive aOtherPrimitive)
        {
            //This function determines the distance between two(2) circles
            //and uses their radii to determine if the circles are colliding

            //Distance holds the difference in position. Using .length given the actual distance
            Vector2 distance = mPosition - aOtherPrimitive.mPosition;

            //Compares the distance between the primitives to the sum of their radii
            if (distance.Length() < ((mSize.X / 2) + (aOtherPrimitive.mSize.X / 2)))
            {
                return true;
            }
            return false;
        }