Example #1
0
        /// <summary>
        /// Determine whether an AABB and AABB collide
        /// </summary>
        /// <param name="w1">This Collision data's world object</param>
        /// <param name="a1">AABB which to check</param>
        /// <param name="w2">Other collision data's world object</param>
        /// <param name="o">AABB which to check</param>
        /// <returns>Whether a collision occurred</returns>
        internal static bool DetectCollision(WorldObject w1, Collision_AABB a1, WorldObject w2, Collision_AABB o)
        {
            Vector2 TL1 = a1.TL + w1.Pos;
            Vector2 BR1 = a1.BR + w1.Pos;
            Vector2 TL2 = o.TL + w2.Pos;
            Vector2 BR2 = o.BR + w2.Pos;

            ///check rect vs rect really just axis aligned box check (simpler)
            bool outsideX = BR1.X <TL2.X || TL1.X> BR2.X;
            bool outsideY = BR1.Y <TL2.Y || TL1.Y> BR2.Y;

            return(!(outsideY || outsideX));
        }
        /// <summary>
        /// Determine whether an AABB and Collision circle collide
        /// </summary>
        /// <param name="w1">This Collision data's world object</param>
        /// <param name="a1">Collision AABB</param>
        /// <param name="w2">Other collision data's world object</param>
        /// <param name="o">Collision circle which to check</param>
        /// <returns>Whether a collision occurred</returns>
        internal static bool detectCollision(WorldObject w1, Collision_AABB a1, WorldObject w2, Collision_BoundingCircle o)
        {
            Vector2 centerPoint = o.centerPointOffset + w2.Pos;
            Vector2 topLeftPoint = a1.topLeftPointOffset + w1.Pos;
            Vector2 bottomRightPoint = a1.bottomRightPointOffset + w1.Pos;

            int regionCode = 0;

            if (centerPoint.X < topLeftPoint.X)
                regionCode += 1; // 0001
            if (centerPoint.X > bottomRightPoint.X)
                regionCode += 2; // 0010
            if (centerPoint.Y > topLeftPoint.Y)
                regionCode += 4; // 0100
            if (centerPoint.Y < bottomRightPoint.Y)
                regionCode += 8;

            float radius = o.radius;
            switch (regionCode)
            {
                case 0: //0000
                    return true;
                case 1: //0001
                    if (Math.Abs(topLeftPoint.X - centerPoint.X) <= radius)
                        return true;
                    break;
                case 2: //0010
                    if (Math.Abs(centerPoint.X - bottomRightPoint.X) <= radius)
                        return true;
                    break;
                case 4: //0100
                    if (Math.Abs(centerPoint.Y - topLeftPoint.Y) <= radius)
                        return true;
                    break;
                case 8: //1000
                    if (Math.Abs(bottomRightPoint.Y - centerPoint.Y) <= radius)
                        return true;
                    break;
                case 5: //0101
                case 9: //1001
                    if (Collision.distanceSquared(centerPoint, topLeftPoint) <= radius * radius)
                        return true;
                    break;
                case 6: //0110
                case 10: //1010
                    if (Collision.distanceSquared(centerPoint, bottomRightPoint) <= radius * radius)
                        return true;
                    break;
            }

            return false;
        }
 /// <summary>
 /// Determine if AABB and BoundingCircle collide
 /// </summary>
 /// /// <param name="w1">This Collision data's world object</param>
 /// <param name="c1">Cricle to check</param>
 /// <param name="w2">Other collision data's world object</param>
 /// <param name="o">AABB which to check</param>
 /// <returns>Whether a collision occurred</returns>
 internal static bool detectCollision(WorldObject w1, Collision_BoundingCircle c1, WorldObject w2, Collision_AABB o)
 {
     return detectCollision(w2, o, w1, c1);
 }
        /// <summary>
        /// Determine whether an AABB and AABB collide
        /// </summary>
        /// <param name="w1">This Collision data's world object</param>
        /// <param name="a1">AABB which to check</param>
        /// <param name="w2">Other collision data's world object</param>
        /// <param name="o">AABB which to check</param>
        /// <returns>Whether a collision occurred</returns>
        internal static bool detectCollision(WorldObject w1, Collision_AABB a1, WorldObject w2, Collision_AABB o)
        {
            Vector2 TL1 = a1.topLeftPointOffset + w1.Pos;
            Vector2 BR1 = a1.bottomRightPointOffset + w1.Pos;
            Vector2 TL2 = o.topLeftPointOffset + w2.Pos;
            Vector2 BR2 = o.bottomRightPointOffset + w2.Pos;

            ///check rect vs rect really just axis aligned box check (simpler)
            bool outsideX = BR1.X < TL2.X || TL1.X > BR2.X;
            bool outsideY = BR1.Y < TL2.Y || TL1.Y > BR2.Y;
            return !(outsideY || outsideX);
        }
Example #5
0
        /// <summary>
        /// Loads the animations from a file.
        /// </summary>
        /// <param name="filename">Name of animfile</param>
        /// <param name="contentSubfolder">Folder where content is stored</param>
        private void LoadAnimation(string filename, string contentSubfolder)
        {
            filename = String.Format("Content/{0}/{1}", contentSubfolder, filename);

            if (!File.Exists(filename))
            {
                throw new Exception(String.Format("Animation file {0} does not exist.", filename));
            }
            XDocument doc = XDocument.Load(filename);
            foreach (var frame in doc.Descendants("Frame"))
            {
                SpriteFrame sf = new SpriteFrame();

                string[] sp = frame.Attribute("TLPos").Value.Split(',');
                sf.StartPos = new Vector2(float.Parse(sp[0]), float.Parse(sp[1]));

                ///image
                string file = frame.Attribute("SpriteSheet").Value;
                sf.Image = This.Game.Content.Load<Texture2D>(String.Format("{0}/{1}", contentSubfolder, file));

                /** sets frame delay */
                sf.Pause = int.Parse(frame.Attribute("FrameDelay").Value);

                //Image's width and height
                sf.Width = int.Parse(frame.Attribute("Width").Value);
                sf.Height = int.Parse(frame.Attribute("Height").Value);

                var point = frame.Attribute("AnimationPeg").Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                float pegX = float.Parse(point.First());
                float pegY = float.Parse(point.Last());

                /** Set the animation Peg*/
                sf.AnimationPeg = new Vector2(pegX + (float)sf.Width / 2, pegY + (float)sf.Height / 2);

                Frames.Add(sf);
            }
            //add collision data
            int idCount = 0;
            foreach (var collision in doc.Descendants("Collision"))
            {
                if (collision.Attribute("Type").Value == "Circle")
                {
                    string[] pt = collision.Attribute("Pos").Value.Split(',');

                    var col = new Collision_BoundingCircle(
                        idCount++,
                        new Vector2(float.Parse(pt[0]), float.Parse(pt[1])),
                        float.Parse(collision.Attribute("Radius").Value));

                    CollisionData.Add(col);
                    objects.Add(new Background_Collision(col));
                }
                else if (collision.Attribute("Type").Value == "Rectangle")
                {
                    string[] tl = collision.Attribute("TLPos").Value.Split(',');
                    float tlx = float.Parse(tl[0]);
                    float tly = float.Parse(tl[1]);
                    string[] br = collision.Attribute("BRPos").Value.Split(',');
                    float brx = float.Parse(br[0]);
                    float bry = float.Parse(br[1]);

                    var col = new Collision_AABB(
                        idCount++,
                        new Vector2(tlx, tly),
                        new Vector2(brx, bry)
                        );
                    CollisionData.Add(col);
                    objects.Add(new Background_Collision(col));
                }
                else if (collision.Attribute("Type").Value == "OBB")
                {
                    string[] c1 = collision.Attribute("Corner1").Value.Split(',');
                    float c1x = float.Parse(c1[0]);
                    float c1y = float.Parse(c1[1]);
                    string[] c2 = collision.Attribute("Corner2").Value.Split(',');
                    float c2x = float.Parse(c2[0]);
                    float c2y = float.Parse(c2[1]);
                    float thickness = float.Parse(collision.Attribute("Thickness").Value.ToString());
                    var col = new Collision_OBB(
                        idCount++,
                        new Vector2(c1x, c1y),
                        new Vector2(c2x, c2y),
                        thickness
                        );
                    CollisionData.Add(col);
                    objects.Add(new Background_Collision(col));
                }
            }
        }
Example #6
0
        /// <summary>
        /// Loads the animations from a file.
        /// </summary>
        /// <param name="filename">Name of animfile</param>
        /// <param name="contentSubfolder">Folder where content is stored</param>
        private void LoadAnimation(string filename, string contentSubfolder)
        {
            filename = String.Format("Content/{0}/{1}", contentSubfolder, filename);

            if (!File.Exists(filename))
            {
                throw new Exception(String.Format("Animation file {0} does not exist.", filename));
            }
            XDocument doc = XDocument.Load(filename);

            foreach (var frame in doc.Descendants("Frame"))
            {
                SpriteFrame sf = new SpriteFrame();

                string[] sp = frame.Attribute("TLPos").Value.Split(',');
                sf.StartPos = new Vector2(float.Parse(sp[0]), float.Parse(sp[1]));

                ///image
                string file = frame.Attribute("SpriteSheet").Value;
                sf.Image = This.Game.Content.Load <Texture2D>(String.Format("{0}/{1}", contentSubfolder, file));

                /** sets frame delay */
                sf.Pause = int.Parse(frame.Attribute("FrameDelay").Value);

                //Image's width and height
                sf.Width  = int.Parse(frame.Attribute("Width").Value);
                sf.Height = int.Parse(frame.Attribute("Height").Value);


                var   point = frame.Attribute("AnimationPeg").Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                float pegX  = float.Parse(point.First());
                float pegY  = float.Parse(point.Last());

                /** Set the animation Peg*/
                sf.AnimationPeg = new Vector2(pegX + (float)sf.Width / 2, pegY + (float)sf.Height / 2);

                Frames.Add(sf);
            }
            //add collision data
            int idCount = 0;

            foreach (var collision in doc.Descendants("Collision"))
            {
                if (collision.Attribute("Type").Value == "Circle")
                {
                    string[] pt = collision.Attribute("Pos").Value.Split(',');

                    var col = new Collision_BoundingCircle(
                        idCount++,
                        new Vector2(float.Parse(pt[0]), float.Parse(pt[1])),
                        float.Parse(collision.Attribute("Radius").Value));

                    CollisionData.Add(col);
                    objects.Add(new Background_Collision(col));
                }
                else if (collision.Attribute("Type").Value == "Rectangle")
                {
                    string[] tl  = collision.Attribute("TLPos").Value.Split(',');
                    float    tlx = float.Parse(tl[0]);
                    float    tly = float.Parse(tl[1]);
                    string[] br  = collision.Attribute("BRPos").Value.Split(',');
                    float    brx = float.Parse(br[0]);
                    float    bry = float.Parse(br[1]);

                    var col = new Collision_AABB(
                        idCount++,
                        new Vector2(tlx, tly),
                        new Vector2(brx, bry)
                        );
                    CollisionData.Add(col);
                    objects.Add(new Background_Collision(col));
                }
                else if (collision.Attribute("Type").Value == "OBB")
                {
                    string[] c1        = collision.Attribute("Corner1").Value.Split(',');
                    float    c1x       = float.Parse(c1[0]);
                    float    c1y       = float.Parse(c1[1]);
                    string[] c2        = collision.Attribute("Corner2").Value.Split(',');
                    float    c2x       = float.Parse(c2[0]);
                    float    c2y       = float.Parse(c2[1]);
                    float    thickness = float.Parse(collision.Attribute("Thickness").Value.ToString());
                    var      col       = new Collision_OBB(
                        idCount++,
                        new Vector2(c1x, c1y),
                        new Vector2(c2x, c2y),
                        thickness
                        );
                    CollisionData.Add(col);
                    objects.Add(new Background_Collision(col));
                }
            }
        }
Example #7
0
 /// <summary>
 /// Detects OBB on OBB collision
 /// </summary>
 /// <param name="w1">This Collision data's world object</param>
 /// <param name="c1">AABB to check</param>
 /// <param name="w2">Other collision data's world object</param>
 /// <param name="o">OBB circle which to check</param>
 /// <returns>Whether a collision occurred</returns>
 internal static bool DetectCollision(WorldObject w1, Collision_AABB c1, WorldObject w2, Collision_OBB o)
 {
     return(DetectCollision(w2, o, w1, c1));
 }
Example #8
0
        /// <summary>
        /// Detects OBB on OBB collision
        /// </summary>
        /// <param name="w1">This Collision data's world object</param>
        /// <param name="c1">OBB to check</param>
        /// <param name="w2">Other collision data's world object</param>
        /// <param name="o">AABB circle which to check</param>
        /// <returns>Whether a collision occurred</returns>
        internal static bool DetectCollision(WorldObject w1, Collision_OBB c1, WorldObject w2, Collision_AABB o)
        {
            //so, we've got to project the faces onto eachother and look for intersections
            //these values are values along the projected axis.
            Vector2 Corner1 = new Vector2(o.TL.X, o.BR.Y);
            var     temp    = c1.xAxis;
            var     dot     = 1;//Vector2.Dot(temp, temp);
            var     a       = (Vector2.Dot((c1.Corner1 + w1.Pos), temp) / dot);
            var     b       = (Vector2.Dot((c1.Corner2 + w1.Pos), temp) / dot);
            var     c       = (Vector2.Dot((Corner1 + w2.Pos), temp) / dot);
            var     d       = (Vector2.Dot((o.TL + w2.Pos), temp) / dot);

            if (
                !(//check if they intersect, if they don't we're not colliding.
                    a <= b ?
                    //is c or d between a b?
                    (a <= c && c <= b) || (a <= d && d <= b) :
                    (b <= c && c <= a) || (b <= d && d <= a)
                    )
                )
            {
                return(false);
            }
            temp = c1.yAxis;
            dot  = 1;//Vector2.Dot(temp, temp);
            a    = (Vector2.Dot((c1.Corner1 + w1.Pos), temp) / dot);
            b    = (Vector2.Dot((c1.Corner3 + w1.Pos), temp) / dot);
            c    = (Vector2.Dot((Corner1 + w2.Pos), temp) / dot);
            d    = (Vector2.Dot((o.BR + w2.Pos), temp) / dot);
            if (
                !(//check if they intersect, if they don't we're not colliding.
                    a <= b ?
                    //is c or d between a b?
                    (a <= c && c <= b) || (a <= d && d <= b) :
                    (b <= c && c <= a) || (b <= d && d <= a)
                    )
                )
            {
                return(false);
            }
            return(true);
        }
Example #9
0
        /// <summary>
        /// Determine whether an AABB and Collision circle collide
        /// </summary>
        /// <param name="w1">This Collision data's world object</param>
        /// <param name="a1">Collision AABB</param>
        /// <param name="w2">Other collision data's world object</param>
        /// <param name="o">Collision circle which to check</param>
        /// <returns>Whether a collision occurred</returns>
        internal static bool DetectCollision(WorldObject w1, Collision_AABB a1, WorldObject w2, Collision_BoundingCircle o)
        {
            Vector2 centerPoint      = o.Center + w2.Pos;
            Vector2 topLeftPoint     = a1.TL + w1.Pos;
            Vector2 bottomRightPoint = a1.BR + w1.Pos;

            int regionCode = 0;

            if (centerPoint.X < topLeftPoint.X)
            {
                regionCode += 1; // 0001
            }
            if (centerPoint.X > bottomRightPoint.X)
            {
                regionCode += 2; // 0010
            }
            if (centerPoint.Y < topLeftPoint.Y)
            {
                regionCode += 4; // 0100
            }
            if (centerPoint.Y > bottomRightPoint.Y)
            {
                regionCode += 8;
            }

            float radius = o.Radius;

            switch (regionCode)
            {
            case 0:     //0000
                return(true);

            case 1:     //0001
                if (Math.Abs(topLeftPoint.X - centerPoint.X) <= radius)
                {
                    return(true);
                }
                break;

            case 2:     //0010
                if (Math.Abs(centerPoint.X - bottomRightPoint.X) <= radius)
                {
                    return(true);
                }
                break;

            case 4:     //0100
                if (Math.Abs(centerPoint.Y - topLeftPoint.Y) <= radius)
                {
                    return(true);
                }
                break;

            case 8:     //1000
                if (Math.Abs(bottomRightPoint.Y - centerPoint.Y) <= radius)
                {
                    return(true);
                }
                break;

            case 5:     //0101
                if (Collision.DistanceSquared(centerPoint, topLeftPoint) <= radius * radius)
                {
                    return(true);
                }
                break;

            case 9:     //1001
                if (Collision.DistanceSquared(centerPoint, new Vector2(topLeftPoint.X, bottomRightPoint.Y)) <= radius * radius)
                {
                    return(true);
                }
                break;

            case 6:     //0110
                if (Collision.DistanceSquared(centerPoint, new Vector2(bottomRightPoint.X, topLeftPoint.Y)) <= radius * radius)
                {
                    return(true);
                }
                break;

            case 10:     //1010
                if (Collision.DistanceSquared(centerPoint, bottomRightPoint) <= radius * radius)
                {
                    return(true);
                }
                break;
            }


            return(false);
        }
Example #10
0
 /// <summary>
 /// Determine if AABB and BoundingCircle collide
 /// </summary>
 /// /// <param name="w1">This Collision data's world object</param>
 /// <param name="c1">Cricle to check</param>
 /// <param name="w2">Other collision data's world object</param>
 /// <param name="o">AABB which to check</param>
 /// <returns>Whether a collision occurred</returns>
 internal static bool DetectCollision(WorldObject w1, Collision_BoundingCircle c1, WorldObject w2, Collision_AABB o)
 {
     return(DetectCollision(w2, o, w1, c1));
 }
Example #11
0
 /// <summary>
 /// Detects OBB on OBB collision
 /// </summary>
 /// <param name="w1">This Collision data's world object</param>
 /// <param name="c1">AABB to check</param>
 /// <param name="w2">Other collision data's world object</param>
 /// <param name="o">OBB circle which to check</param>
 /// <returns>Whether a collision occurred</returns>
 internal static bool DetectCollision(WorldObject w1, Collision_AABB c1, WorldObject w2, Collision_OBB o)
 {
     return DetectCollision(w2, o, w1, c1);
 }
Example #12
0
 /// <summary>
 /// Detects OBB on OBB collision
 /// </summary>
 /// <param name="w1">This Collision data's world object</param>
 /// <param name="c1">OBB to check</param>
 /// <param name="w2">Other collision data's world object</param>
 /// <param name="o">AABB circle which to check</param>
 /// <returns>Whether a collision occurred</returns>
 internal static bool DetectCollision(WorldObject w1, Collision_OBB c1, WorldObject w2, Collision_AABB o)
 {
     //so, we've got to project the faces onto eachother and look for intersections
     //these values are values along the projected axis.
     Vector2 Corner1 = new Vector2(o.TL.X,o.BR.Y);
     var temp = c1.xAxis;
     var dot = 1;//Vector2.Dot(temp, temp);
     var a = (Vector2.Dot((c1.Corner1 + w1.Pos), temp) / dot);
     var b = (Vector2.Dot((c1.Corner2 + w1.Pos), temp) / dot);
     var c = (Vector2.Dot((Corner1 + w2.Pos), temp) / dot);
     var d = (Vector2.Dot((o.TL + w2.Pos), temp) / dot);
     if (
         !(//check if they intersect, if they don't we're not colliding.
             a <= b ?
         //is c or d between a b?
             (a <= c && c <= b) || (a <= d && d <= b) :
             (b <= c && c <= a) || (b <= d && d <= a)
             )
         )
         return false;
     temp = c1.yAxis;
     dot = 1;//Vector2.Dot(temp, temp);
     a = (Vector2.Dot((c1.Corner1 + w1.Pos), temp) / dot);
     b = (Vector2.Dot((c1.Corner3 + w1.Pos), temp) / dot);
     c = (Vector2.Dot((Corner1 + w2.Pos), temp) / dot);
     d = (Vector2.Dot((o.BR + w2.Pos), temp) / dot);
     if (
         !(//check if they intersect, if they don't we're not colliding.
            a <= b ?
         //is c or d between a b?
            (a <= c && c <= b) || (a <= d && d <= b) :
            (b <= c && c <= a) || (b <= d && d <= a)
            )
         )
         return false;
     return true;
 }