Example #1
0
        public void Raycast_Against_RotatedBox_Test(
            [Values(0f, 10f, -20f, 30f, 40f)] float startX,
            [Values(0f, -10f, 30f, -50f, 70f)] float startY)
        {
            // Simulate the physics.
            SimulatePhysics();

            var aabb = RotatedBox.Aabb;

            Assert.IsTrue(startX < aabb.Min.x);

            var start            = new float2(startX, startY);
            var expectedPosition = new float2(aabb.Min.x + RayEpsilon, 0f);

            // Set-up the query.
            var input = new RaycastInput
            {
                Start = start,
                End   = expectedPosition,

                Filter = CollisionFilter.Default
            };

            // Perform the query.
            var results = PhysicsWorld.CastRay(input, out RaycastHit hit);

            Assert.IsTrue(results);
            Assert.IsTrue(hit.PhysicsBodyIndex != PhysicsBody.Constants.InvalidBodyIndex, "PhysicsBody Index is Invalid.");
            Assert.AreEqual(hit.Entity, RotatedBox.Entity, "Entity is invalid.");
            PhysicsAssert.AreEqual(expectedPosition, hit.Position, QueryEpsilon, "Hit position is incorrect.");
        }
Example #2
0
        public void RotatedColliderCast_Against_RotatedBox_Test(
            [Values(0f, 10f, -20f, 30f, 40f)] float startX)
        {
            // Simulate the physics.
            SimulatePhysics();

            var aabb = RotatedBox.Aabb;

            Assert.IsTrue(startX < aabb.Min.x);

            var start            = new float2(startX, 0f);
            var expectedPosition = new float2(aabb.Min.x + ColliderEpsilon, 0f);

            // Set-up the query.
            var geometry = new BoxGeometry {
                Size = new float2(BoxSize)
            };

            using (var colliderBlob = PhysicsBoxCollider.Create(geometry, CollisionFilter.Default, PhysicsMaterial.Default))
            {
                var input = new ColliderCastInput
                {
                    Start = start,
                    End   = expectedPosition,

                    Rotation = float2x2.Rotate(math.radians(BoxRotation)),
                    Collider = colliderBlob
                };

                // Perform the query.
                var results = PhysicsWorld.CastCollider(input, out ColliderCastHit hit);
                Assert.IsTrue(results);
                Assert.IsTrue(hit.PhysicsBodyIndex != PhysicsBody.Constants.InvalidBodyIndex, "PhysicsBody Index is Invalid.");
                Assert.AreEqual(hit.Entity, RotatedBox.Entity, "Entity is invalid.");
                PhysicsAssert.AreEqual(expectedPosition, hit.Position, QueryEpsilon, "Hit position is incorrect.");
            }
        }
Example #3
0
        public void MassProperties_BuiltFromChildren_MatchesExpected()
        {
            void TestCompoundBox(PhysicsTransform transform)
            {
                // Create a unit box
                var boxCollider = PhysicsBoxCollider.Create(new BoxGeometry
                {
                    Size        = new float2(1f),
                    Center      = transform.Translation,
                    Angle       = 0f,
                    BevelRadius = 0.0f
                });

                // Create a compound of mini boxes, matching the volume of the single box
                var miniBox = PhysicsBoxCollider.Create(new BoxGeometry
                {
                    Size        = new float2(0.5f),
                    Center      = float2.zero,
                    Angle       = 0f,
                    BevelRadius = 0.0f
                });

                const uint UserData      = 0xDEADBEEF;
                const int  ChildrenCount = 4;

                var childrenTransforms = new NativeArray <PhysicsTransform>(ChildrenCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory)
                {
                    [0] = new PhysicsTransform(new float2(-0.25f, -0.25f), float2x2.identity),
                    [1] = new PhysicsTransform(new float2(0.25f, -0.25f), float2x2.identity),
                    [2] = new PhysicsTransform(new float2(0.25f, 0.25f), float2x2.identity),
                    [3] = new PhysicsTransform(new float2(-0.25f, 0.25f), float2x2.identity),
                };

                var children = new NativeArray <PhysicsCompoundCollider.ColliderBlobInstance>(ChildrenCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

                for (var i = 0; i < ChildrenCount; ++i)
                {
                    children[i] = new PhysicsCompoundCollider.ColliderBlobInstance
                    {
                        Collider          = miniBox,
                        CompoundFromChild = PhysicsMath.mul(transform, childrenTransforms[i])
                    };
                }

                var colliderBlob = PhysicsCompoundCollider.Create(children, UserData);

                childrenTransforms.Dispose();
                children.Dispose();

                ref var collider = ref colliderBlob.GetColliderRef <PhysicsCompoundCollider>();

                Assert.AreEqual(ColliderType.Compound, collider.ColliderType);
                Assert.AreEqual(CollisionType.Composite, collider.CollisionType);
                Assert.AreEqual(UserData, collider.UserData);

                var boxMassProperties      = boxCollider.Value.MassProperties;
                var compoundMassProperties = colliderBlob.Value.MassProperties;

                Assert.AreEqual(boxMassProperties.Area, compoundMassProperties.Area, 1e-3f, "Area incorrect.");
                Assert.AreEqual(boxMassProperties.AngularExpansionFactor, compoundMassProperties.AngularExpansionFactor, 1e-3f, "AngularExpansionFactor incorrect.");
                PhysicsAssert.AreEqual(boxMassProperties.MassDistribution.LocalCenterOfMass, compoundMassProperties.MassDistribution.LocalCenterOfMass, 1e-3f, "LocalCenterOfMass incorrect.");
                Assert.AreEqual(boxMassProperties.MassDistribution.InverseInertia, compoundMassProperties.MassDistribution.InverseInertia, 1e-3f, "InverseInertia incorrect.");

                boxCollider.Dispose();
                colliderBlob.Dispose();
            }
Example #4
0
        public void FrictionCombinePolicyTest()
        {
            var mat1 = new PhysicsMaterial();
            var mat2 = new PhysicsMaterial();

            // GeometricMean Tests
            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.GeometricMean;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.GeometricMean;

            mat1.Friction = 1.0f;
            mat2.Friction = 0.0f;
            var combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);

            PhysicsAssert.AreEqual(combinedFriction, 0.0f);

            mat1.Friction    = 0.5f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            mat1.Friction    = 1.0f;
            mat2.Friction    = 0.25f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            // Minimum Tests
            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Minimum;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Minimum;
            mat1.Friction    = 1.0f;
            mat2.Friction    = 0.0f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.0f);

            mat1.Friction    = 0.5f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            mat1.Friction    = 1.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            // Maximum Tests
            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum;
            mat1.Friction    = 1.0f;
            mat2.Friction    = 0.0f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 1.0f);

            mat1.Friction    = 0.5f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 2.0f);

            // ArithmeticMean Tests
            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.ArithmeticMean;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.ArithmeticMean;
            mat1.Friction    = 1.0f;
            mat2.Friction    = 0.0f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            mat1.Friction    = 0.25f;
            mat2.Friction    = 0.75f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);

            // Mixed CombinePolicy Tests - Note that max(CombinePolicy of both materials) is used
            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.GeometricMean;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.ArithmeticMean; // this policy should be used
            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);
            //switch order
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat2, mat1);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);

            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.GeometricMean;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum; // this policy should be used
            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 2.0f);
            //switch order
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat2, mat1);
            PhysicsAssert.AreEqual(combinedFriction, 2.0f);

            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.GeometricMean;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Minimum; // this policy should be used
            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);
            //switch order
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat2, mat1);
            PhysicsAssert.AreEqual(combinedFriction, 0.5f);

            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Minimum;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum; // this policy should be used
            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 2.0f);
            //switch order
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat2, mat1);
            PhysicsAssert.AreEqual(combinedFriction, 2.0f);

            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Minimum;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.ArithmeticMean; // this policy should be used
            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);
            //switch order
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat2, mat1);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);

            mat1.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.Maximum;
            mat2.FrictionCombinePolicy = PhysicsMaterial.CombinePolicy.ArithmeticMean; // this policy should be used
            mat1.Friction    = 2.0f;
            mat2.Friction    = 0.5f;
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat1, mat2);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);
            //switch order
            combinedFriction = PhysicsMaterial.GetCombinedFriction(mat2, mat1);
            PhysicsAssert.AreEqual(combinedFriction, 1.25f);
        }