Beispiel #1
0
        private void HorizontalAdd()
        {
            float[] a = new[]
            {
                1f,
                0f,
                1f,
                2f,
                100f,
                0.1f,
                1234f,
                678.234f,
                10f,
                1f,
                0f,
                1f,
                2f,
                100f,
                0.1f,
                1234f,
                678.234f,
            };

            const float r = 4042.668f;

            Assert.That(NumericsHelpers.HorizontalAdd(a), Is.Approximately(r));
        }
        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)));
        }
Beispiel #3
0
        private void EqualsApprox(ReadOnlySpan <float> a, ReadOnlySpan <float> b, double tolerance = .00001)
        {
            Assert.That(a.Length, Is.EqualTo(b.Length));

            for (var i = 0; i < a.Length; i++)
            {
                Assert.That(b[i], Is.Approximately(a[i], tolerance));
            }
        }
        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 TestDirectionToVec(float x, float y, Direction dir, Angle _)
        {
            var control    = new Vector2(x, y).Normalized;
            var controlInt = new Vector2i((int)x, (int)y);
            var val        = dir.ToVec();
            var intVec     = dir.ToIntVec();

            Assert.That(val, Is.Approximately(control));
            Assert.That(intVec, Is.EqualTo(controlInt));
        }
        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.That(results.Collided);
            Assert.That(results.Normal, Is.Approximately(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.Approximately(new Vector2(0.75f, 0)));
        }
Beispiel #7
0
        public void ORectCircle_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, false, 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 TestVecToAngle(float x, float y, Direction dir, Angle angle)
        {
            var target = new Vector2(x, y).Normalized;

            Assert.That(target.ToWorldAngle().Reduced(), Is.Approximately(new Angle(angle)));
        }
Beispiel #9
0
        public void Constructor_DefaultZoom_isTwo()
        {
            var eye = new Eye();

            Assert.That(eye.Zoom, Is.Approximately(Vector2.One * 2f));
        }
        public async Task TestNestedParentVelocities()
        {
            var server = StartServer(new ServerIntegrationOptions {
                ExtraPrototypes = Prototypes
            });

            await server.WaitIdleAsync();

            var entityManager = server.ResolveDependency <IEntityManager>();
            var mapManager    = server.ResolveDependency <IMapManager>();
            var physicsSys    = server.ResolveDependency <IEntitySystemManager>().GetEntitySystem <SharedPhysicsSystem>();

            await server.WaitAssertion(() =>
            {
                var mapId = mapManager.CreateMap();
                var grid  = mapManager.CreateGrid(mapId);

                Assert.That(entityManager.TryGetComponent <PhysicsComponent>(grid.GridEntityId, out var gridPhysics));
                gridPhysics.BodyType = BodyType.Dynamic;

                Vector2 offset1 = new(2, 0);
                var dummy1      = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(grid.GridEntityId, offset1));
                Assert.That(entityManager.TryGetComponent(dummy1, out PhysicsComponent body1));
                Assert.That(entityManager.TryGetComponent(dummy1, out TransformComponent xform1));
                xform1.AttachParent(grid.GridEntityId);

                // create another entity attached to the dummy1
                Vector2 offset2 = new(-1, 0);
                var dummy2      = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(dummy1, offset2));
                Assert.That(entityManager.TryGetComponent(dummy2, out PhysicsComponent body2));
                Assert.That(entityManager.TryGetComponent(dummy2, out TransformComponent xform2));
                xform2.AttachParent(dummy1);

                Assert.That(xform2.WorldPosition, Is.Approximately(new Vector2(1, 0), 1e-6));

                gridPhysics.LinearVelocity  = new Vector2(1, 0);
                gridPhysics.AngularVelocity = 1;

                // check that dummy2 properly gets the velocities from its grand-parent
                var linearVelocity  = physicsSys.GetMapLinearVelocity(dummy2, body2);
                var angularVelocity = physicsSys.GetMapAngularVelocity(dummy2, body2);
                var velocities      = physicsSys.GetMapVelocities(dummy2, body2);
                Assert.That(linearVelocity, Is.Approximately(Vector2.One, 1e-6));
                Assert.That(angularVelocity, Is.Approximately(1f, 1e-6));
                Assert.That(velocities.Item1, Is.Approximately(linearVelocity, 1e-6));
                Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6));

                // check that if we make move in the opposite direction, but spin in the same direction, then dummy2 is
                // (for this moment in time) stationary, but still rotating.
                body1.LinearVelocity  = -gridPhysics.LinearVelocity;
                body1.AngularVelocity = gridPhysics.AngularVelocity;
                linearVelocity        = physicsSys.GetMapLinearVelocity(dummy2, body2);
                angularVelocity       = physicsSys.GetMapAngularVelocity(dummy2, body2);
                velocities            = physicsSys.GetMapVelocities(dummy2, body2);
                Assert.That(linearVelocity, Is.Approximately(Vector2.Zero, 1e-6));
                Assert.That(angularVelocity, Is.Approximately(2f, 1e-6));
                Assert.That(velocities.Item1, Is.Approximately(linearVelocity, 1e-6));
                Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6));

                // but not if we update the local position:
                xform2.WorldPosition = Vector2.Zero;
                linearVelocity       = physicsSys.GetMapLinearVelocity(dummy2, body2);
                angularVelocity      = physicsSys.GetMapAngularVelocity(dummy2, body2);
                velocities           = physicsSys.GetMapVelocities(dummy2, body2);
                Assert.That(linearVelocity, Is.Approximately(new Vector2(0, -2), 1e-6));
                Assert.That(angularVelocity, Is.Approximately(2f, 1e-6));
                Assert.That(velocities.Item1, Is.Approximately(linearVelocity, 1e-6));
                Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6));
            });
        }
        public async Task TestMapVelocities()
        {
            var server = StartServer(new ServerIntegrationOptions {
                ExtraPrototypes = Prototypes
            });

            await server.WaitIdleAsync();

            var entityManager = server.ResolveDependency <IEntityManager>();
            var mapManager    = server.ResolveDependency <IMapManager>();
            var physicsSys    = server.ResolveDependency <IEntitySystemManager>().GetEntitySystem <SharedPhysicsSystem>();

            await server.WaitAssertion(() =>
            {
                var mapId = mapManager.CreateMap();
                var grid  = mapManager.CreateGrid(mapId);
                var grid2 = mapManager.CreateGrid(mapId);

                Assert.That(entityManager.TryGetComponent <PhysicsComponent>(grid.GridEntityId, out var gridPhysics));
                gridPhysics.BodyType = BodyType.Dynamic;

                Vector2 offset = new(3, 4);
                Vector2 expectedFinalVelocity = new Vector2(-4, 3) * 2 + Vector2.One;

                var dummy = entityManager.SpawnEntity(DummyEntity, new EntityCoordinates(grid.GridEntityId, offset));
                Assert.That(entityManager.TryGetComponent(dummy, out PhysicsComponent body));
                Assert.That(entityManager.TryGetComponent(dummy, out TransformComponent xform));
                xform.AttachParent(grid.GridEntityId);

                // Test Linear Velocities
                gridPhysics.LinearVelocity = Vector2.One;
                Assert.That(body.LinearVelocity, Is.Approximately(Vector2.Zero, 1e-6));
                Assert.That(body.AngularVelocity, Is.Approximately(0f, 1e-6));

                var linearVelocity  = physicsSys.GetMapLinearVelocity(dummy, body);
                var angularVelocity = physicsSys.GetMapAngularVelocity(dummy, body);
                var velocities      = physicsSys.GetMapVelocities(dummy, body);
                Assert.That(linearVelocity, Is.Approximately(Vector2.One, 1e-6));
                Assert.That(angularVelocity, Is.Approximately(0f, 1e-6));
                Assert.That(velocities.Item1, Is.Approximately(linearVelocity, 1e-6));
                Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6));

                // Add angular velocity
                gridPhysics.AngularVelocity = 2;
                Assert.That(body.LinearVelocity, Is.EqualTo(Vector2.Zero));
                Assert.That(body.AngularVelocity, Is.EqualTo(0f));

                linearVelocity  = physicsSys.GetMapLinearVelocity(dummy, body);
                angularVelocity = physicsSys.GetMapAngularVelocity(dummy, body);
                velocities      = physicsSys.GetMapVelocities(dummy, body);
                Assert.That(linearVelocity, Is.Approximately(expectedFinalVelocity, 1e-6));
                Assert.That(angularVelocity, Is.Approximately(2f, 1e-6));
                Assert.That(velocities.Item1, Is.Approximately(linearVelocity, 1e-6));
                Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6));

                // Check that velocity does not change when changing parent
                xform.AttachParent(grid2.GridEntityId);
                linearVelocity  = physicsSys.GetMapLinearVelocity(dummy, body);
                angularVelocity = physicsSys.GetMapAngularVelocity(dummy, body);
                velocities      = physicsSys.GetMapVelocities(dummy, body);
                Assert.That(linearVelocity, Is.Approximately(expectedFinalVelocity, 1e-6));
                Assert.That(angularVelocity, Is.Approximately(2f, 1e-6));
                Assert.That(velocities.Item1, Is.Approximately(linearVelocity, 1e-6));
                Assert.That(velocities.Item2, Is.Approximately(angularVelocity, 1e-6));
            });
        }