public void WithEntityId()
        {
            var entityManager = IoCManager.Resolve <IEntityManager>();
            var mapManager    = IoCManager.Resolve <IMapManager>();

            var mapId   = mapManager.CreateMap();
            var mapEnt  = mapManager.GetMapEntity(mapId);
            var grid    = mapManager.CreateGrid(mapId);
            var gridEnt = entityManager.GetEntity(grid.GridEntityId);
            var newEnt  = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero));

            Assert.That(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.Zero));

            newEnt.Transform.LocalPosition = Vector2.One;

            Assert.That(newEnt.Transform.Coordinates.Position, Is.EqualTo(Vector2.One));
            Assert.That(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.One));

            gridEnt.Transform.LocalPosition = Vector2.One;

            Assert.That(newEnt.Transform.Coordinates.Position, Is.EqualTo(Vector2.One));
            Assert.That(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(new Vector2(2, 2)));

            var newEntTwo = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(newEnt.Uid, Vector2.Zero));

            Assert.That(newEntTwo.Transform.Coordinates.Position, Is.EqualTo(Vector2.Zero));
            Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position));
            Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(gridEnt).Position, Is.EqualTo(newEnt.Transform.Coordinates.Position));

            newEntTwo.Transform.LocalPosition = -Vector2.One;

            Assert.That(newEntTwo.Transform.Coordinates.Position, Is.EqualTo(-Vector2.One));
            Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.One));
            Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(gridEnt).Position, Is.EqualTo(Vector2.Zero));
        }
        public void CircleCircle_SamePos()
        {
            var circle = new Circle(new Vector2(0, 0), 0.5f);

            CollisionSolver.CalculateCollisionFeatures(in circle, in circle, 1, out var results);

            Assert.That(results.Collided, Is.EqualTo(false));
        }
        public void CircleCircle_NotCollide()
        {
            var a = new Circle(new Vector2(0, 0), 0.5f);
            var b = new Circle(new Vector2(1, 1), 0.5f);

            CollisionSolver.CalculateCollisionFeatures(in a, in b, 1, out var results);

            Assert.That(results.Collided, Is.EqualTo(false));
        }
        public void ClosestPoint()
        {
            var obb        = new OrientedRectangle(Vector2.One, Vector2.One * 0.5f, MathF.PI / 2);
            var worldPoint = new Vector2(13, -13);

            var closestPoint = obb.ClosestPointWorld(worldPoint);

            Assert.That(closestPoint, Is.Approximately(new Vector2(1.5f, 0.5f)));
        }
        public void TransformPoint()
        {
            var obb        = new OrientedRectangle(Vector2.One, Vector2.Zero, MathF.PI / 2);
            var worldPoint = new Vector2(2, 0);

            var localPoint = obb.TransformPoint(worldPoint);

            Assert.That(localPoint, Is.Approximately(new Vector2(1, 3)));
        }
        public void RectCircle_NotCollide()
        {
            var a = new OrientedRectangle(Vector2.Zero, Vector2.One * 0.5f, MathF.PI / 4);
            var b = new Circle(new Vector2(1, 1), 0.5f);

            CollisionSolver.CalculateCollisionFeatures(in a, in b, 1, out var results);

            Assert.That(results.Collided, Is.EqualTo(false));
        }
        public void EntityCoordinates_Map()
        {
            var mapManager = IoCManager.Resolve <IMapManager>();

            var mapId     = mapManager.CreateMap();
            var mapEntity = mapManager.CreateNewMapEntity(mapId);

            Assert.That(mapEntity.Transform.ParentUid.IsValid(), Is.False);
            Assert.That(mapEntity.Transform.Coordinates.EntityId, Is.EqualTo(mapEntity.Uid));
        }
        public void TransformPointRoundTrip()
        {
            var obb        = new OrientedRectangle(new Vector2(3, 5), Vector2.Zero, MathF.PI / 4);
            var worldPoint = new Vector2(11, 13);

            var localPoint = obb.InverseTransformPoint(worldPoint);
            var result     = obb.TransformPoint(localPoint);

            Assert.That(result, Is.EqualTo(worldPoint));
        }
        public void NoParent_OffsetZero()
        {
            var mapManager = IoCManager.Resolve <IMapManager>();

            var mapId     = mapManager.CreateMap();
            var mapEntity = mapManager.CreateNewMapEntity(mapId);

            Assert.That(mapEntity.Transform.Coordinates.Position, Is.EqualTo(Vector2.Zero));

            mapEntity.Transform.LocalPosition = Vector2.One;
            Assert.That(mapEntity.Transform.Coordinates.Position, Is.EqualTo(Vector2.Zero));
        }
        public void GetMapId_Map()
        {
            var entityManager = IoCManager.Resolve <IEntityManager>();
            var mapManager    = IoCManager.Resolve <IMapManager>();

            var mapId  = mapManager.CreateMap();
            var mapEnt = mapManager.CreateNewMapEntity(mapId);
            var newEnt = entityManager.CreateEntityUninitialized("dummy", new MapCoordinates(Vector2.Zero, mapId));

            Assert.That(mapEnt.Transform.Coordinates.GetMapId(entityManager), Is.EqualTo(mapId));
            Assert.That(newEnt.Transform.Coordinates.GetMapId(entityManager), Is.EqualTo(mapId));
        }
        public void GetMapId_Grid()
        {
            var entityManager = IoCManager.Resolve <IEntityManager>();
            var mapManager    = IoCManager.Resolve <IMapManager>();

            var mapId   = mapManager.CreateMap();
            var grid    = mapManager.CreateGrid(mapId);
            var gridEnt = entityManager.GetEntity(grid.GridEntityId);
            var newEnt  = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero));

            Assert.That(gridEnt.Transform.Coordinates.GetMapId(entityManager), Is.EqualTo(mapId));
            Assert.That(newEnt.Transform.Coordinates.GetMapId(entityManager), Is.EqualTo(mapId));
        }
        public void BoxBox_Collide()
        {
            var a = new AlignedRectangle(new Vector2(1, 0), new Vector2(0.5f, 0.5f));
            var b = new AlignedRectangle(new Vector2(0.25f, 0.15f), new Vector2(0.5f, 0.5f));

            CollisionSolver.CalculateCollisionFeatures(in a, in b, 1, out var results);

            Assert.That(results.Collided, Is.True);
            Assert.That(results.Normal, Is.EqualTo(new Vector2(-1, 0)));
            Assert.That(results.Penetration, Is.EqualTo(0.25f / 2));
            Assert.That(results.Contacts, Is.Not.Null);
            Assert.That(results.Contacts.Length, Is.EqualTo(1));
            Assert.That(results.Contacts[0], Is.EqualTo(new Vector2(0.5f, 0.15f)));
        }
        public void RectCircle_Collide()
        {
            var a = new OrientedRectangle(Vector2.Zero, new Vector2(3, 1), MathF.PI / 2);
            var b = new Circle(new Vector2(1.5f, 0), 1f);

            CollisionSolver.CalculateCollisionFeatures(in a, in b, 1, out var results);

            Assert.AreEqual(true, results.Collided);
            Assert.That(results.Normal, Is.Approximately(Vector2.UnitX));
            Assert.AreEqual(0.5f / 2, results.Penetration);
            Assert.IsNotNull(results.Contacts);
            Assert.AreEqual(1, results.Contacts.Length);
            Assert.That(results.Contacts[0], Is.Approximately(new Vector2(0.75f, 0)));
        }
        public void CircleCircle_Collide()
        {
            var a = new Circle(new Vector2(0, 0), 1f);
            var b = new Circle(new Vector2(1.5f, 0), 1f);

            CollisionSolver.CalculateCollisionFeatures(in a, in b, 1, out var results);

            Assert.That(results.Collided);
            Assert.That(results.Normal, Is.EqualTo(Vector2.UnitX));
            Assert.That(results.Penetration, Is.EqualTo(0.5f / 2));
            Assert.That(results.Contacts, Is.Not.Null);
            Assert.That(results.Contacts.Length, Is.EqualTo(1));
            Assert.That(results.Contacts[0], Is.EqualTo(new Vector2(0.75f, 0)));
        }
        public void ToMap_MoveGrid(float x1, float y1, float x2, float y2)
        {
            var gridPos = new Vector2(x1, y1);
            var entPos  = new Vector2(x2, y2);

            var entityManager = IoCManager.Resolve <IEntityManager>();
            var mapManager    = IoCManager.Resolve <IMapManager>();

            var mapId   = mapManager.CreateMap();
            var grid    = mapManager.CreateGrid(mapId);
            var gridEnt = entityManager.GetEntity(grid.GridEntityId);
            var newEnt  = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, entPos));

            Assert.That(newEnt.Transform.Coordinates.ToMap(entityManager), Is.EqualTo(new MapCoordinates(entPos, mapId)));

            gridEnt.Transform.LocalPosition += gridPos;

            Assert.That(newEnt.Transform.Coordinates.ToMap(entityManager), Is.EqualTo(new MapCoordinates(entPos + gridPos, mapId)));
        }
        public void GetParent()
        {
            var entityManager = IoCManager.Resolve <IEntityManager>();
            var mapManager    = IoCManager.Resolve <IMapManager>();

            var mapId   = mapManager.CreateMap();
            var grid    = mapManager.CreateGrid(mapId);
            var mapEnt  = mapManager.GetMapEntity(mapId);
            var gridEnt = entityManager.GetEntity(grid.GridEntityId);
            var newEnt  = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero));

            Assert.That(mapEnt.Transform.Coordinates.GetParent(entityManager), Is.EqualTo(mapEnt));
            Assert.That(gridEnt.Transform.Coordinates.GetParent(entityManager), Is.EqualTo(mapEnt));
            Assert.That(newEnt.Transform.Coordinates.GetParent(entityManager), Is.EqualTo(gridEnt));

            // Reparenting the entity should return correct results.
            newEnt.Transform.AttachParent(mapEnt);

            Assert.That(newEnt.Transform.Coordinates.GetParent(entityManager), Is.EqualTo(mapEnt));
        }
        public void TryGetParent()
        {
            var entityManager = IoCManager.Resolve <IEntityManager>();
            var mapManager    = IoCManager.Resolve <IMapManager>();

            var mapId   = mapManager.CreateMap();
            var grid    = mapManager.CreateGrid(mapId);
            var mapEnt  = mapManager.GetMapEntity(mapId);
            var gridEnt = entityManager.GetEntity(grid.GridEntityId);
            var newEnt  = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero));

            Assert.That(mapEnt.Transform.Coordinates.TryGetParent(entityManager, out var mapEntParent), Is.EqualTo(true));
            Assert.That(mapEntParent, Is.EqualTo(mapEnt));

            Assert.That(gridEnt.Transform.Coordinates.TryGetParent(entityManager, out var gridEntParent), Is.EqualTo(true));
            Assert.That(gridEntParent, Is.EqualTo(mapEnt));

            Assert.That(newEnt.Transform.Coordinates.TryGetParent(entityManager, out var newEntParent), Is.EqualTo(true));
            Assert.That(newEntParent, Is.EqualTo(gridEnt));

            // Reparenting the entity should return correct results.
            newEnt.Transform.AttachParent(mapEnt);

            Assert.That(newEnt.Transform.Coordinates.TryGetParent(entityManager, out newEntParent), Is.EqualTo(true));
            Assert.That(newEntParent, Is.EqualTo(mapEnt));

            // Deleting the parent should make TryGetParent return false.
            mapEnt.Delete();

            // These shouldn't be valid anymore.
            Assert.That(newEnt.Transform.Coordinates.IsValid(entityManager), Is.False);
            Assert.That(gridEnt.Transform.Coordinates.IsValid(entityManager), Is.False);

            Assert.That(newEnt.Transform.Coordinates.TryGetParent(entityManager, out newEntParent), Is.EqualTo(false));
            Assert.That(newEntParent, Is.Null);

            Assert.That(gridEnt.Transform.Coordinates.TryGetParent(entityManager, out gridEntParent), Is.EqualTo(false));
            Assert.That(gridEntParent, Is.Null);
        }