Ejemplo n.º 1
0
        public void TestCalculateAabbTransformedTriangle()
        {
            float3[] vertices =
            {
                new float3(-1.8f, 2.4f, 4.6f),
                new float3(1.4f,  1.6f, 1.6f),
                new float3(0.2f,  1.2f, 3.6f)
            };

            float3     translation = new float3(3.4f, 2.5f, -1.1f);
            quaternion rotation    = quaternion.AxisAngle(math.normalize(new float3(1.1f, 10.1f, -3.4f)), 78.0f);

            var  collider = PolygonCollider.CreateTriangle(vertices[0], vertices[1], vertices[2]);
            Aabb aabb     = collider.Value.CalculateAabb(new RigidTransform(rotation, translation));

            for (int i = 0; i < 3; ++i)
            {
                vertices[i] = translation + math.mul(rotation, vertices[i]);
            }

            Aabb expected = new Aabb()
            {
                Min = math.min(math.min(vertices[0], vertices[1]), vertices[2]),
                Max = math.max(math.max(vertices[0], vertices[1]), vertices[2])
            };

            TestUtils.AreEqual(expected.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 2
0
        public void TestCapsuleColliderMassProperties()
        {
            float  radius = 2.3f;
            float  length = 5.5f;
            float3 p0     = new float3(1.1f, 2.2f, 3.4f);
            float3 p1     = p0 + length * math.normalize(new float3(1, 1, 1));

            float hemisphereMass = 0.5f * 4.0f / 3.0f * (float)math.PI * radius * radius * radius;
            float cylinderMass   = (float)math.PI * radius * radius * length;
            float totalMass      = 2.0f * hemisphereMass + cylinderMass;

            hemisphereMass /= totalMass;
            cylinderMass   /= totalMass;

            float  itX = cylinderMass * (length * length / 12.0f + radius * radius / 4.0f) + 2.0f * hemisphereMass * (2.0f * radius * radius / 5.0f + length * length / 4.0f + 3.0f * length * radius / 8.0f);
            float  itY = cylinderMass * radius * radius / 2.0f + 4.0f * hemisphereMass * radius * radius / 5.0f;
            float  itZ = itX;
            float3 expectedInertiaTensor = new float3(itX, itY, itZ);

            var capsuleCollider = CapsuleCollider.Create(new CapsuleGeometry
            {
                Vertex0 = p0,
                Vertex1 = p1,
                Radius  = radius
            });
            float3 inertiaTensor = capsuleCollider.Value.MassProperties.MassDistribution.InertiaTensor;

            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
Ejemplo n.º 3
0
        unsafe public void TestCreateTriangle()
        {
            float3[] vertices =
            {
                new float3(-1.4f, 1.4f, 5.6f),
                new float3(1.4f,  1.4f, 3.6f),
                new float3(0.2f,  1.2f, 5.6f)
            };
            float3 normal = math.normalize(math.cross(vertices[1] - vertices[0], vertices[2] - vertices[0]));

            var collider         = PolygonCollider.CreateTriangle(vertices[0], vertices[1], vertices[2]);
            var triangleCollider = UnsafeUtilityEx.AsRef <PolygonCollider>(collider.GetUnsafePtr());

            Assert.IsTrue(triangleCollider.IsTriangle);
            Assert.IsFalse(triangleCollider.IsQuad);

            TestUtils.AreEqual(triangleCollider.Vertices[0], vertices[0], 1e-3f);
            TestUtils.AreEqual(triangleCollider.Vertices[1], vertices[1], 1e-3f);
            TestUtils.AreEqual(triangleCollider.Vertices[2], vertices[2], 1e-3f);
            Assert.AreEqual(2, triangleCollider.Planes.Length);
            TestUtils.AreEqual(normal, triangleCollider.Planes[0].Normal, 1e-3f);
            TestUtils.AreEqual(-normal, triangleCollider.Planes[1].Normal, 1e-3f);
            Assert.AreEqual(ColliderType.Triangle, triangleCollider.Type);
            Assert.AreEqual(CollisionType.Convex, triangleCollider.CollisionType);
        }
Ejemplo n.º 4
0
        unsafe public void TestCreateQuad()
        {
            float3[] vertices =
            {
                new float3(-4.5f, 0.0f, 1.0f),
                new float3(3.4f,  0.7f, 1.0f),
                new float3(3.4f,  2.7f, 1.0f),
                new float3(-3.4f, 1.2f, 1.0f)
            };
            float3 normal = math.normalize(math.cross(vertices[2] - vertices[1], vertices[0] - vertices[1]));

            var collider     = PolygonCollider.CreateQuad(vertices[0], vertices[1], vertices[2], vertices[3]);
            var quadCollider = UnsafeUtilityEx.AsRef <PolygonCollider>(collider.GetUnsafePtr());

            Assert.IsFalse(quadCollider.IsTriangle);
            Assert.IsTrue(quadCollider.IsQuad);

            TestUtils.AreEqual(quadCollider.Vertices[0], vertices[0], 1e-3f);
            TestUtils.AreEqual(quadCollider.Vertices[1], vertices[1], 1e-3f);
            TestUtils.AreEqual(quadCollider.Vertices[2], vertices[2], 1e-3f);
            TestUtils.AreEqual(quadCollider.Vertices[3], vertices[3], 1e-3f);
            Assert.AreEqual(2, quadCollider.Planes.Length);
            TestUtils.AreEqual(normal, quadCollider.Planes[0].Normal, 1e-3f);
            TestUtils.AreEqual(-normal, quadCollider.Planes[1].Normal, 1e-3f);
            Assert.AreEqual(ColliderType.Quad, quadCollider.Type);
            Assert.AreEqual(CollisionType.Convex, quadCollider.CollisionType);
        }
Ejemplo n.º 5
0
        public void TestCapsuleColliderCalculateAabbTransformed()
        {
            float  radius          = 2.3f;
            float  length          = 5.5f;
            float3 p0              = new float3(1.1f, 2.2f, 3.4f);
            float3 p1              = p0 + length * math.normalize(new float3(1, 1, 1));
            var    capsuleCollider = CapsuleCollider.Create(new CapsuleGeometry
            {
                Vertex0 = p0,
                Vertex1 = p1,
                Radius  = radius
            });

            float3     translation = new float3(-3.4f, 0.5f, 0.0f);
            quaternion rotation    = quaternion.AxisAngle(math.normalize(new float3(0.4f, 0.0f, 150.0f)), 123.0f);

            Aabb   expectedAabb  = new Aabb();
            float3 p0Transformed = math.mul(rotation, p0) + translation;
            float3 p1Transformed = math.mul(rotation, p1) + translation;

            expectedAabb.Min = math.min(p0Transformed, p1Transformed) - new float3(radius);
            expectedAabb.Max = math.max(p0Transformed, p1Transformed) + new float3(radius);

            Aabb aabb = capsuleCollider.Value.CalculateAabb(new RigidTransform(rotation, translation));

            TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 6
0
        public void TestCalculateAabbTransformedQuad()
        {
            float3[] vertices =
            {
                new float3(-4.5f, 0.0f, 1.0f),
                new float3(3.4f,  0.7f, 1.0f),
                new float3(3.4f,  2.7f, 1.0f),
                new float3(-3.4f, 1.2f, 1.0f)
            };

            float3     translation = new float3(-3.4f, -2.5f, -1.1f);
            quaternion rotation    = quaternion.AxisAngle(math.normalize(new float3(11.1f, 10.1f, -3.4f)), 178.0f);

            var  collider = PolygonCollider.CreateQuad(vertices[0], vertices[1], vertices[2], vertices[3]);
            Aabb aabb     = collider.Value.CalculateAabb(new RigidTransform(rotation, translation));

            for (int i = 0; i < 4; ++i)
            {
                vertices[i] = translation + math.mul(rotation, vertices[i]);
            }

            Aabb expected = Aabb.CreateFromPoints(new float3x4(vertices[0], vertices[1], vertices[2], vertices[3]));

            TestUtils.AreEqual(expected.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 7
0
        public void TestAabb()
        {
            float3 v0 = float3(100, 200, 300);
            float3 v1 = float3(200, 300, 400);
            float3 v2 = float3(50, 100, 350);

            Aabb a0; a0.Min = float3.zero; a0.Max = v0;
            Aabb a1; a1.Min = float3.zero; a1.Max = v1;
            Aabb a2; a2.Min = v2; a2.Max = v1;
            Aabb a3; a3.Min = v2; a3.Max = v0;

            Assert.IsTrue(a0.IsValid);
            Assert.IsTrue(a1.IsValid);
            Assert.IsTrue(a2.IsValid);
            Assert.IsFalse(a3.IsValid);

            Assert.IsTrue(a1.Contains(a0));
            Assert.IsFalse(a0.Contains(a1));
            Assert.IsTrue(a1.Contains(a2));
            Assert.IsFalse(a2.Contains(a1));
            Assert.IsFalse(a0.Contains(a2));
            Assert.IsFalse(a2.Contains(a0));

            // Test Expand / Contains
            {
                Aabb   a5; a5.Min = v2; a5.Max = v1;
                float3 testPoint  = float3(v2.x - 1.0f, v1.y + 1.0f, .5f * (v2.z + v1.z));
                Assert.IsFalse(a5.Contains(testPoint));

                a5.Expand(1.5f);
                Assert.IsTrue(a5.Contains(testPoint));
            }

            // Test transform
            {
                Aabb ut; ut.Min = v0; ut.Max = v1;

                // Identity transform should not modify aabb
                Aabb outAabb = Unity.Physics.Math.TransformAabb(RigidTransform.identity, ut);

                TestUtils.AreEqual(ut.Min, outAabb.Min, 1e-3f);

                // Test translation
                outAabb = Unity.Physics.Math.TransformAabb(new RigidTransform(quaternion.identity, float3(100.0f, 0.0f, 0.0f)), ut);

                Assert.AreEqual(outAabb.Min.x, 200);
                Assert.AreEqual(outAabb.Min.y, 200);
                Assert.AreEqual(outAabb.Max.x, 300);
                Assert.AreEqual(outAabb.Max.z, 400);

                // Test rotation
                quaternion rot = quaternion.EulerXYZ(0.0f, 0.0f, k_pi2);
                outAabb = Unity.Physics.Math.TransformAabb(new RigidTransform(rot, float3.zero), ut);

                TestUtils.AreEqual(outAabb.Min, float3(-300.0f, 100.0f, 300.0f), 1e-3f);
                TestUtils.AreEqual(outAabb.Max, float3(-200.0f, 200.0f, 400.0f), 1e-3f);
                TestUtils.AreEqual(outAabb.SurfaceArea, ut.SurfaceArea, 1e-2f);
            }
        }
Ejemplo n.º 8
0
        unsafe public void TestSphereColliderCreate()
        {
            float3 center         = new float3(-8.45f, 9.65f, -0.10f);
            float  radius         = 0.98f;
            var    collider       = SphereCollider.Create(center, radius);
            var    sphereCollider = UnsafeUtilityEx.AsRef <SphereCollider>(collider.GetUnsafePtr());

            TestUtils.AreEqual(center, sphereCollider.Center, 1e-3f);
            TestUtils.AreEqual(radius, sphereCollider.Radius, 1e-3f);
            Assert.AreEqual(ColliderType.Sphere, sphereCollider.Type);
            Assert.AreEqual(CollisionType.Convex, sphereCollider.CollisionType);
        }
Ejemplo n.º 9
0
        public void TestSphereColliderMassProperties()
        {
            float3 center         = new float3(-8.4f, 5.63f, 77.2f);
            float  radius         = 2.3f;
            var    sphereCollider = SphereCollider.Create(center, radius);

            float  inertia = 2.0f / 5.0f * radius * radius;
            float3 expectedInertiaTensor = new float3(inertia, inertia, inertia);
            float3 inertiaTensor         = sphereCollider.Value.MassProperties.MassDistribution.InertiaTensor;

            Debug.Log($"Expected inertia tensor: {expectedInertiaTensor}, was: {inertiaTensor}");
            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
Ejemplo n.º 10
0
        unsafe public void TestCapsuleColliderCreate()
        {
            float3 v0              = new float3(1.45f, 0.34f, -8.65f);
            float3 v1              = new float3(100.45f, -80.34f, -8.65f);
            float  radius          = 1.45f;
            var    collider        = CapsuleCollider.Create(v0, v1, radius);
            var    capsuleCollider = UnsafeUtilityEx.AsRef <CapsuleCollider>(collider.GetUnsafePtr());

            Assert.AreEqual(ColliderType.Capsule, capsuleCollider.Type);
            Assert.AreEqual(CollisionType.Convex, capsuleCollider.CollisionType);
            TestUtils.AreEqual(v0, capsuleCollider.Vertex0);
            TestUtils.AreEqual(v1, capsuleCollider.Vertex1);
            TestUtils.AreEqual(radius, capsuleCollider.Radius);
        }
        public void TestSphereColliderMassProperties()
        {
            float3 center         = new float3(-8.4f, 5.63f, 77.2f);
            float  radius         = 2.3f;
            var    sphereCollider = SphereCollider.Create(new SphereGeometry {
                Center = center, Radius = radius
            });

            float  inertia = 2.0f / 5.0f * radius * radius;
            float3 expectedInertiaTensor = new float3(inertia, inertia, inertia);
            float3 inertiaTensor         = sphereCollider.Value.MassProperties.MassDistribution.InertiaTensor;

            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
Ejemplo n.º 12
0
        public void TestCalculateAabbLocalQuad()
        {
            float3[] quadVertices =
            {
                new float3(-4.5f, 0.0f, 1.0f),
                new float3(3.4f,  0.7f, 1.0f),
                new float3(3.4f,  2.7f, 1.0f),
                new float3(-3.4f, 1.2f, 1.0f)
            };
            var  collider = PolygonCollider.CreateQuad(quadVertices[0], quadVertices[1], quadVertices[2], quadVertices[3]);
            Aabb aabb     = collider.Value.CalculateAabb();
            Aabb expected = Aabb.CreateFromPoints(new float3x4(quadVertices[0], quadVertices[1], quadVertices[2], quadVertices[3]));

            TestUtils.AreEqual(expected.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 13
0
        //[Test] // #TODO: Add test back in once we have implemented this in Physics
        unsafe public void TestMassPropertiesTriangle()
        {
            // constructing the triangle to be parallel to the xy plane so we don't have to figure out the transformations first
            float3[] vertices =
            {
                new float3(-1.1f, -0.4f, 0.0f),
                new float3(0.8f,  -0.1f, 0.0f),
                new float3(-0.2f,  1.3f, 0.0f)
            };

            var collider = PolygonCollider.CreateTriangle(vertices[0], vertices[1], vertices[2]);

            float3 inertiaTensor         = collider.Value.MassProperties.MassDistribution.InertiaTensor;
            float3 expectedInertiaTensor = calcTriangleInertiaTensor(vertices[0], vertices[1], vertices[2]);

            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
Ejemplo n.º 14
0
        public void TestSphereColliderCalculateAabbLocal()
        {
            float3 center         = new float3(-8.4f, 5.63f, -7.2f);
            float  radius         = 2.3f;
            var    sphereCollider = SphereCollider.Create(center, radius);

            Aabb expected = new Aabb();

            expected.Min = center - new float3(radius, radius, radius);
            expected.Max = center + new float3(radius, radius, radius);

            Aabb actual = sphereCollider.Value.CalculateAabb();

            Debug.Log($"Expected aabb: Min: {expected.Min} Max: {expected.Max}, was Min: {actual.Min} Max: {actual.Max}");
            TestUtils.AreEqual(expected.Min, actual.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, actual.Max, 1e-3f);
        }
Ejemplo n.º 15
0
        //[Test] // #TODO: Add test back in once we have implemented this in Physics
        public void TestMassPropertiesQuad()
        {
            float3[] vertices =
            {
                new float3(-1.1f, -0.4f, 0.0f),
                new float3(0.8f,  -0.1f, 0.0f),
                new float3(1.2f,   1.3f, 0.0f),
                new float3(-0.2f,  1.3f, 0.0f)
            };

            var collider = PolygonCollider.CreateQuad(vertices[0], vertices[1], vertices[2], vertices[3]);

            float3 inertiaTensor         = collider.Value.MassProperties.MassDistribution.InertiaTensor;
            float3 expectedInertiaTensor = calcQuadInertiaTensor(vertices[0], vertices[1], vertices[2], vertices[3]);

            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
        unsafe public void TestSphereColliderCreate()
        {
            var sphere = new SphereGeometry
            {
                Center = new float3(-8.45f, 9.65f, -0.10f),
                Radius = 0.98f
            };

            var collider       = SphereCollider.Create(sphere);
            var sphereCollider = UnsafeUtilityEx.AsRef <SphereCollider>(collider.GetUnsafePtr());

            TestUtils.AreEqual(sphere.Center, sphereCollider.Center, 1e-3f);
            TestUtils.AreEqual(sphere.Center, sphereCollider.Geometry.Center, 1e-3f);
            TestUtils.AreEqual(sphere.Radius, sphereCollider.Radius, 1e-3f);
            TestUtils.AreEqual(sphere.Radius, sphereCollider.Geometry.Radius, 1e-3f);
            Assert.AreEqual(ColliderType.Sphere, sphereCollider.Type);
            Assert.AreEqual(CollisionType.Convex, sphereCollider.CollisionType);
        }
        public void TestSphereColliderCalculateAabbLocal()
        {
            float3 center         = new float3(-8.4f, 5.63f, -7.2f);
            float  radius         = 2.3f;
            var    sphereCollider = SphereCollider.Create(new SphereGeometry {
                Center = center, Radius = radius
            });

            Aabb expected = new Aabb();

            expected.Min = center - new float3(radius, radius, radius);
            expected.Max = center + new float3(radius, radius, radius);

            Aabb actual = sphereCollider.Value.CalculateAabb();

            TestUtils.AreEqual(expected.Min, actual.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, actual.Max, 1e-3f);
        }
Ejemplo n.º 18
0
        public void TestBoxColliderMassProperties()
        {
            float3     center       = float3.zero;
            quaternion orientation  = quaternion.identity;
            float3     size         = new float3(1.0f, 250.0f, 2.0f);
            float      convexRadius = 0.25f;
            var        boxCollider  = BoxCollider.Create(center, orientation, size, convexRadius);

            float3 expectedInertiaTensor = 1.0f / 12.0f * new float3(
                size.y * size.y + size.z * size.z,
                size.x * size.x + size.z * size.z,
                size.y * size.y + size.x * size.x);

            MassProperties massProperties = boxCollider.Value.MassProperties;
            float3         inertiaTensor  = massProperties.MassDistribution.InertiaTensor;

            Debug.Log($"Expected Inertia Tensor: {expectedInertiaTensor}, was: {inertiaTensor}");
            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
Ejemplo n.º 19
0
 unsafe public void TestCapsuleColliderCreate()
 {
     var geometry = new CapsuleGeometry
     {
         Vertex0 = new float3(1.45f, 0.34f, -8.65f),
         Vertex1 = new float3(100.45f, -80.34f, -8.65f),
         Radius = 1.45f
     };
     var collider = CapsuleCollider.Create(geometry);
     var capsuleCollider = UnsafeUtility.AsRef<CapsuleCollider>(collider.GetUnsafePtr());
     Assert.AreEqual(ColliderType.Capsule, capsuleCollider.Type);
     Assert.AreEqual(CollisionType.Convex, capsuleCollider.CollisionType);
     TestUtils.AreEqual(geometry.Vertex0, capsuleCollider.Vertex0);
     TestUtils.AreEqual(geometry.Vertex0, capsuleCollider.Geometry.Vertex0);
     TestUtils.AreEqual(geometry.Vertex1, capsuleCollider.Vertex1);
     TestUtils.AreEqual(geometry.Vertex1, capsuleCollider.Geometry.Vertex1);
     TestUtils.AreEqual(geometry.Radius, capsuleCollider.Radius);
     TestUtils.AreEqual(geometry.Radius, capsuleCollider.Geometry.Radius);
 }
Ejemplo n.º 20
0
        public void TestBoxColliderCalculateAabbLocalTranslationAndOrientation()
        {
            float3     center       = new float3(-2.56f, -4.33f, 54.30f);
            quaternion orientation  = quaternion.AxisAngle(math.normalize(new float3(0, 1, 1)), 42.0f);
            float3     size         = new float3(5.0f, 0.25f, 1.3f);
            float      convexRadius = 0.25f;

            Aabb expectedAabb = new Aabb
            {
                Min = new float3(-4.062223f, -6.442692f, 52.3973f),
                Max = new float3(-1.057776f, -2.217308f, 56.2027f)
            };

            var  boxCollider = BoxCollider.Create(center, orientation, size, convexRadius);
            Aabb aabb        = boxCollider.Value.CalculateAabb();

            Debug.Log($"Expeced: Min: {expectedAabb.Min}, Max: {expectedAabb.Max}, Was: Min: {aabb.Min}, Max: {aabb.Max}");
            TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 21
0
        public void TestCapsuleColliderCalculateAabbLocal()
        {
            float  radius          = 2.3f;
            float  length          = 5.5f;
            float3 p0              = new float3(1.1f, 2.2f, 3.4f);
            float3 p1              = p0 + length * math.normalize(new float3(1, 1, 1));
            var    capsuleCollider = CapsuleCollider.Create(p0, p1, radius);

            Aabb expectedAabb = new Aabb();

            expectedAabb.Min = math.min(p0, p1) - new float3(radius);
            expectedAabb.Max = math.max(p0, p1) + new float3(radius);

            Aabb aabb = capsuleCollider.Value.CalculateAabb();

            Debug.Log($"Expected Aabb: Min {expectedAabb.Min}, Max {expectedAabb.Max}");
            Debug.Log($"Actual   Aabb: Min {aabb.Min}, Max {aabb.Max}");
            TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 22
0
        public void TestSphereColliderCalculateAabbTransformed()
        {
            float3 center         = new float3(-3.4f, 0.63f, -17.2f);
            float  radius         = 5.3f;
            var    sphereCollider = SphereCollider.Create(center, radius);

            float3     translation = new float3(8.3f, -0.5f, 170.0f);
            quaternion rotation    = quaternion.AxisAngle(math.normalize(new float3(1.1f, 4.5f, 0.0f)), 146.0f);

            Aabb expected = new Aabb();

            expected.Min = math.mul(rotation, center) + translation - new float3(radius, radius, radius);
            expected.Max = math.mul(rotation, center) + translation + new float3(radius, radius, radius);

            Aabb actual = sphereCollider.Value.CalculateAabb(new RigidTransform(rotation, translation));

            Debug.Log($"Expected aabb: Min: {expected.Min} Max: {expected.Max}, was Min: {actual.Min} Max: {actual.Max}");
            TestUtils.AreEqual(expected.Min, actual.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, actual.Max, 1e-3f);
        }
Ejemplo n.º 23
0
        public void TestCalculateAabbLocalTriangle()
        {
            float3[] vertices =
            {
                new float3(-1.8f, 2.4f, 4.6f),
                new float3(1.4f,  1.6f, 1.6f),
                new float3(0.2f,  1.2f, 3.6f)
            };

            var  collider = PolygonCollider.CreateTriangle(vertices[0], vertices[1], vertices[2]);
            Aabb aabb     = collider.Value.CalculateAabb();

            Aabb expected = new Aabb()
            {
                Min = math.min(math.min(vertices[0], vertices[1]), vertices[2]),
                Max = math.max(math.max(vertices[0], vertices[1]), vertices[2])
            };

            TestUtils.AreEqual(expected.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expected.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 24
0
        public void TestBoxColliderCalculateAabbLocalTranslation()
        {
            // Expected values in this test were generated using CalculateBoxAabbNaive above
            {
                float3     center       = new float3(-0.59f, 0.36f, 0.35f);
                quaternion orientation  = quaternion.identity;
                float3     size         = new float3(2.32f, 10.87f, 16.49f);
                float      convexRadius = 0.25f;

                Aabb expectedAabb = new Aabb
                {
                    Min = new float3(-1.75f, -5.075f, -7.895f),
                    Max = new float3(0.57f, 5.795f, 8.595f)
                };

                var  boxCollider = BoxCollider.Create(center, orientation, size, convexRadius);
                Aabb aabb        = boxCollider.Value.CalculateAabb();
                Debug.Log($"Expected: Min: {expectedAabb.Min}, Max: {expectedAabb.Max}, Was: Min: {aabb.Min}, Max: {aabb.Max}");
                TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
                TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
            }
        }
Ejemplo n.º 25
0
        public void TestConvexColliderCalculateAabbTransformed()
        {
            var points = new NativeArray <float3>(6, Allocator.Temp)
            {
                [0] = new float3(1.45f, 8.67f, 3.45f),
                [1] = new float3(8.75f, 1.23f, 6.44f),
                [2] = new float3(100.34f, 5.33f, -2.55f),
                [3] = new float3(8.76f, 4.56f, -4.54f),
                [4] = new float3(9.75f, -0.45f, -8.99f),
                [5] = new float3(7.66f, 3.44f, 0.0f)
            };

            float      convexRadius = 1.25f;
            float3     translation  = new float3(43.56f, -87.32f, -0.02f);
            quaternion rotation     = quaternion.AxisAngle(math.normalize(new float3(8.45f, -2.34f, 0.82f)), 43.21f);

            float3[] transformedPoints = new float3[points.Length];
            for (int i = 0; i < points.Length; ++i)
            {
                transformedPoints[i] = translation + math.mul(rotation, points[i]);
            }

            Aabb expectedAabb = Aabb.CreateFromPoints(new float3x4(transformedPoints[0], transformedPoints[1], transformedPoints[2], transformedPoints[3]));

            expectedAabb.Include(transformedPoints[4]);
            expectedAabb.Include(transformedPoints[5]);

            // Currently the convex hull is not shrunk, so we have to expand by the convex radius
            expectedAabb.Expand(convexRadius);

            var collider = ConvexCollider.Create(points, convexRadius);

            points.Dispose();

            Aabb actualAabb = collider.Value.CalculateAabb(new RigidTransform(rotation, translation));

            TestUtils.AreEqual(expectedAabb.Min, actualAabb.Min, 1e-3f);
            TestUtils.AreEqual(expectedAabb.Max, actualAabb.Max, 1e-3f);
        }
Ejemplo n.º 26
0
        public void TestBoxColliderCalculateAabbTransformed()
        {
            float3     center       = new float3(2.54f, -4.86f, 6.90f);
            quaternion orientation  = quaternion.AxisAngle(math.normalize(new float3(0.5f, 1, 1)), 42.0f);
            float3     size         = new float3(50.0f, 0.1f, 2.3f);
            float      convexRadius = 0.25f;
            float3     translation  = new float3(-2.5f, 15.0f, -0.01f);
            quaternion rotation     = quaternion.AxisAngle(math.normalize(new float3(4.2f, 0.1f, -3.3f)), 42.1f);

            Aabb expectedAabb = new Aabb
            {
                Min = new float3(-17.75146f, 7.216872f, -11.04677f),
                Max = new float3(11.82336f, 38.96107f, 17.96488f)
            };

            var  boxCollider = BoxCollider.Create(center, orientation, size, convexRadius);
            Aabb aabb        = boxCollider.Value.CalculateAabb(new RigidTransform(rotation, translation));

            Debug.Log($"Expected: Min: {expectedAabb.Min}, Max: {expectedAabb.Max}, Was: Min: {aabb.Min}, Max: {aabb.Max}");
            TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 27
0
        public void TestBoxColliderMassProperties()
        {
            var geometry = new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = new float3(1.0f, 250.0f, 2.0f),
                BevelRadius = 0.25f
            };

            var boxCollider = BoxCollider.Create(geometry);

            float3 expectedInertiaTensor = 1.0f / 12.0f * new float3(
                geometry.Size.y * geometry.Size.y + geometry.Size.z * geometry.Size.z,
                geometry.Size.x * geometry.Size.x + geometry.Size.z * geometry.Size.z,
                geometry.Size.y * geometry.Size.y + geometry.Size.x * geometry.Size.x);

            MassProperties massProperties = boxCollider.Value.MassProperties;
            float3         inertiaTensor  = massProperties.MassDistribution.InertiaTensor;

            TestUtils.AreEqual(expectedInertiaTensor, inertiaTensor, 1e-3f);
        }
Ejemplo n.º 28
0
        public void TestAabbTransform()
        {
            Random rnd = new Random(0x12345678);

            for (int i = 0; i < 100; i++)
            {
                quaternion r = rnd.NextQuaternionRotation();
                float3     t = rnd.NextFloat3();

                Aabb orig = new Aabb();
                orig.Include(rnd.NextFloat3());
                orig.Include(rnd.NextFloat3());

                Aabb outAabb1 = Unity.Physics.Math.TransformAabb(new RigidTransform(r, t), orig);

                Physics.Math.MTransform bFromA = new Physics.Math.MTransform(r, t);
                Aabb outAabb2 = Unity.Physics.Math.TransformAabb(bFromA, orig);

                TestUtils.AreEqual(outAabb1.Min, outAabb2.Min, 1e-3f);
                TestUtils.AreEqual(outAabb1.Max, outAabb2.Max, 1e-3f);
            }
        }
        public void TestCapsuleColliderCalculateAabbLocal()
        {
            float  radius          = 2.3f;
            float  length          = 5.5f;
            float3 p0              = new float3(1.1f, 2.2f, 3.4f);
            float3 p1              = p0 + length * math.normalize(new float3(1, 1, 1));
            var    capsuleCollider = CapsuleCollider.Create(new CapsuleGeometry
            {
                Vertex0 = p0,
                Vertex1 = p1,
                Radius  = radius
            });

            Aabb expectedAabb = new Aabb();

            expectedAabb.Min = math.min(p0, p1) - new float3(radius);
            expectedAabb.Max = math.max(p0, p1) + new float3(radius);

            Aabb aabb = capsuleCollider.Value.CalculateAabb();

            TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
            TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
        }
Ejemplo n.º 30
0
        public void TestBoxColliderCalculateAabbLocalTranslation()
        {
            // Expected values in this test were generated using CalculateBoxAabbNaive above
            {
                var geometry = new BoxGeometry
                {
                    Center      = new float3(-0.59f, 0.36f, 0.35f),
                    Orientation = quaternion.identity,
                    Size        = new float3(2.32f, 10.87f, 16.49f),
                    BevelRadius = 0.25f
                };

                Aabb expectedAabb = new Aabb
                {
                    Min = new float3(-1.75f, -5.075f, -7.895f),
                    Max = new float3(0.57f, 5.795f, 8.595f)
                };

                var  boxCollider = BoxCollider.Create(geometry);
                Aabb aabb        = boxCollider.Value.CalculateAabb();
                TestUtils.AreEqual(expectedAabb.Min, aabb.Min, 1e-3f);
                TestUtils.AreEqual(expectedAabb.Max, aabb.Max, 1e-3f);
            }
        }