Ejemplo n.º 1
0
 //returns true if this is overlapping b
 public bool Overlaps(AABB b)
 {
     Vector2 T = b.Position - Position;//vector from A to B
     return
     Math.Abs(T.X) <= (Extent.X + b.Extent.X)
     && Math.Abs(T.Y) <= (Extent.Y + b.Extent.Y);
 }
Ejemplo n.º 2
0
        public void CollisionTest2(Player otherPlayer)
        {
            GetSegments();

            if (LocationNext != Location)
            {
                var crossed = false;

                //On test d'abord si au moins deux segment se croisent
                for (var i = 0; i < GetSegments().Length; i++ )
                {
                    crossed = Collision.IntersectSegmentSegment(
                        GetSegments()[i].P1, GetSegments()[i].P2,
                        otherPlayer.GetSegments()[i].P1, otherPlayer.GetSegments()[i].P2) != null;

                    if (crossed)
                    {
                        break;
                    }
                }

                //Si c'est le cas on fait un test plus précis
                if (crossed)
                {
                    var a0 = new AABB(Location.Center, new Vector2(Location.Size.X / 2, Location.Size.Y / 2));
                    var a1 = new AABB(LocationNext.Center, new Vector2(LocationNext.Size.X / 2, LocationNext.Size.Y / 2));

                    var b0 = new AABB(otherPlayer.Location.Center, new Vector2(otherPlayer.Location.Size.X / 2, otherPlayer.Location.Size.Y / 2));
                    var b1 = new AABB(otherPlayer.LocationNext.Center, new Vector2(otherPlayer.LocationNext.Size.X / 2, otherPlayer.LocationNext.Size.Y / 2));

                    float u0 = 0;
                    float u1 = 0;

                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    var collide = AABBCollision.Detection(a0, a1, b0, b1, ref u0, ref u1);
                    sw.Stop();

                    if (collide)
                    {
                        Console.WriteLine("Collide : " + sw.Elapsed);
                        //SetPositionFinal(Location.Position);
                        var v = new Vector2(LocationNext.Position.X - Location.Position.X, LocationNext.Position.Y - Location.Position.Y);
                        var pos = Location.Position + v * u0;
                        SetPositionFinal(pos);

                        var v2 = new Vector2(otherPlayer.LocationNext.Position.X - otherPlayer.Location.Position.X, otherPlayer.LocationNext.Position.Y - otherPlayer.Location.Position.Y);
                        var pos2 = otherPlayer.Location.Position + v2 * u0;
                        otherPlayer.SetPositionFinal(pos2);
                    }
                    else
                    {
                        otherPlayer.SetPositionFinal(otherPlayer.LocationNext.Position);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static bool Detection(AABB a0, AABB a1, AABB b0, AABB b1, ref float u0, ref float u1)
        {
            Vector2 va = a1.Position - a0.Position;
            Vector2 vb = b1.Position - b0.Position;

            Vector2 v = vb - va;

            float u_0x = 0;
            float u_0y = 0;
            float u_1x = 1;
            float u_1y = 1;

            if (a0.Overlaps(b0))
            {
                u0 = u1 = 0;
                return true;
            }

            //X
            if (a0.Max(0) < b0.Min(0) && v.X < 0)
            {
                u_0x = (a0.Max(0) - b0.Min(0)) / v.X;
            }
            else if (b0.Max(0) < a0.Min(0) && v.X > 0)
            {
                u_0x = (a0.Min(0) - b0.Max(0)) / v.X;
            }

            if (b0.Max(0) > a0.Min(0) && v.X < 0)
            {
                u_1x = (a0.Min(0) - b0.Max(0)) / v.X;
            }
            else if (a0.Max(0) > b0.Min(0) && v.X > 0)
            {
                u_1x = (a0.Max(0) - b0.Min(0)) / v.X;
            }

            //Y
            if (a0.Max(1) < b0.Min(1) && v.Y < 0)
            {
                u_0y = (a0.Max(1) - b0.Min(1)) / v.Y;
            }
            else if (b0.Max(1) < a0.Min(1) && v.Y > 0)
            {
                u_0y = (a0.Min(1) - b0.Max(1)) / v.Y;
            }

            if (b0.Max(1) > a0.Min(1) && v.Y < 0)
            {
                u_1y = (a0.Min(1) - b0.Max(1)) / v.Y;
            }
            else if (a0.Max(1) > b0.Min(1) && v.Y > 0)
            {
                u_1y = (a0.Max(1) - b0.Min(1)) / v.Y;
            }

            //possible first time of overlap
            u0 = Math.Max(u_0x, u_0y);

            //possible last time of overlap
            u1 = Math.Min(u_1x, u_1y);

            //they could have only collided if
            //the first time of overlap occurred
            //before the last time of overlap
            return u0 <= u1;
        }