Example #1
0
        public World()
        {
            m_triangles = new List<Triangle>();
            m_walls = new List<Segment>();
            m_intersections = new List<Point>();
            m_renderer = new TriangleRenderer();
            m_SRenderer = new SegmentRenderer();
            m_players = new List<Player>();

            m_camera = new Camera();

            //Creation des joueurs
            m_player = new Player();

            m_camera.Bounds = new Box2(0, 0, 1000, 1000);
            m_camera.Target = m_player;

            Player.SetPosition(new Vector2(200, 200));

            m_random = new Random();

            //for (var i = 0; i < 20; i++)
            //{
            //    var x = m_random.Next(0, 500);
            //    var y = m_random.Next(0, 500);

            //    var p = new Player();
            //    p.SetPosition(new Vector2(x, y));
            //    m_players.Add(p);
            //}

            var p = new Player();
            p.SetPosition(new Vector2(400, 100));
            p.SetPositionNext(new Vector2(400, 400));
            m_players.Add(p);

            for (var i = 0; i < 10; i++)
            {
                var x = m_random.Next(0, 500);
                var y = m_random.Next(0, 500);
                var sx = m_random.Next(10, 50);
                var sy = m_random.Next(10, 50);

                m_walls.Add(new Segment()
                {
                    P1 = new Vector2(x, y),
                    P2 = new Vector2(x + sx, y),
                    Color = new Vector4(0.5f, 0.2f, 0, 1)
                });

                m_walls.Add(new Segment()
                {
                    P1 = new Vector2(x + sx, y),
                    P2 = new Vector2(x + sx, y + sy),
                    Color = new Vector4(0.5f, 0.2f, 0, 1)
                });

                m_walls.Add(new Segment()
                {
                    P1 = new Vector2(x + sx, y + sy),
                    P2 = new Vector2(x, y + sy),
                    Color = new Vector4(0.5f, 0.2f, 0, 1)
                });

                m_walls.Add(new Segment()
                {
                    P1 = new Vector2(x, y + sy),
                    P2 = new Vector2(x, y),
                    Color = new Vector4(0.5f, 0.2f, 0, 1)
                });
            }

            //m_walls.Add(new Segment()
            //{
            //    P1 = new Vector3(300, 300, 0),
            //    P2 = new Vector3(300, 400, 0),
            //    Color = new Vector4(0.5f, 0.2f, 0, 1)
            //});

            //m_walls.Add(new Segment()
            //{
            //    P1 = new Vector3(300, 400, 0),
            //    P2 = new Vector3(400, 400, 0),
            //    Color = new Vector4(0.5f, 0.2f, 0, 1)
            //});

            //m_walls.Add(new Segment()
            //{
            //    P1 = new Vector3(400, 400, 0),
            //    P2 = new Vector3(400, 300, 0),
            //    Color = new Vector4(0.5f, 0.2f, 0, 1)
            //});

            //m_walls.Add(new Segment()
            //{
            //    P1 = new Vector3(400, 300, 0),
            //    P2 = new Vector3(300, 300, 0),
            //    Color = new Vector4(0.5f, 0.2f, 0, 1)
            //});

            s_instance = this;

            //Triangles();

            m_time = Time.Instance;
            m_time.AddAction(new Granite.Time.Action(Update, 0.1));
            //m_time.AddAction(new Granite.Time.Action(UpdatePlayers, 2));
            m_time.Start();
        }
Example #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);
                    }
                }
            }
        }