예제 #1
0
    public void CapsuleCollider_Vertical_Root_ConversionTest()
    {
        var expectedOffset = new float2(3f, -4f);
        var expectedSize   = new float2(5f, 10f);

        // Set-up the hierarchy.
        {
            Root = new GameObject();

            var collider = CreateClassicComponent <CapsuleCollider2D>(Root);
            collider.direction = CapsuleDirection2D.Vertical;
            collider.offset    = expectedOffset;
            collider.size      = expectedSize;
        }

        // Run the conversion test and validate.
        RunConversionTest(
            (BlobAssetReference <Unity.U2D.Entities.Physics.Collider> colliderBlob) =>
        {
            // Sanity.
            Assert.AreEqual(CollisionType.Convex, colliderBlob.Value.CollisionType);
            Assert.AreEqual(ColliderType.Capsule, colliderBlob.Value.ColliderType);

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

            var actualOffset = (collider.Vertex0 + collider.Vertex1) * 0.5f;
            var actualSize   = new float2(collider.Radius * 2f, math.distance(collider.Vertex0, collider.Vertex1) + collider.Radius * 2f);

            PhysicsAssert.AreEqual(expectedOffset, actualOffset, Epsilon);
            PhysicsAssert.AreEqual(expectedSize, actualSize, Epsilon);
        });
예제 #2
0
    public void TestAddFlexibleInPlace()
    {
        Random  rnd = new Random(2);
        Vector3 p1 = RandomUtil.Vector3(rnd); float m1 = RandomUtil.PositiveFloat(rnd);
        Vector3 p2 = RandomUtil.Vector3(rnd); float m2 = RandomUtil.PositiveFloat(rnd);
        Vector3 p3 = RandomUtil.Vector3(rnd); float m3 = RandomUtil.PositiveFloat(rnd);

        MassMoment momentToAdd = new MassMoment();

        momentToAdd.AddInplace(m1, p1);
        momentToAdd.AddInplace(m2, p2);
        momentToAdd.AddInplace(m3, p3);

        float      flexiblity       = 0.6f;
        Vector3    centerOfRotation = RandomUtil.Vector3(rnd);
        MassMoment massMoment       = new MassMoment();

        massMoment.AddFlexibleInPlace(momentToAdd, flexiblity, centerOfRotation);

        MassMoment expectedMoment = new MassMoment();

        expectedMoment.AddInplace((1 - flexiblity) * m1, p1);
        expectedMoment.AddInplace((1 - flexiblity) * m2, p2);
        expectedMoment.AddInplace((1 - flexiblity) * m3, p3);
        expectedMoment.AddInplace(flexiblity * m1, centerOfRotation);
        expectedMoment.AddInplace(flexiblity * m2, centerOfRotation);
        expectedMoment.AddInplace(flexiblity * m3, centerOfRotation);

        PhysicsAssert.AreEqual(massMoment, expectedMoment, Acc);
    }
예제 #3
0
    public void PolygonCollider_Root_ConversionTest()
    {
        // Set-up the hierarchy.
        {
            Root = new GameObject();

            var collider = CreateClassicComponent <PolygonCollider2D>(Root);
            collider.SetPath(0, m_Points);
        }

        // Run the conversion test and validate.
        RunConversionTest(
            (BlobAssetReference <Unity.U2D.Entities.Physics.Collider> colliderBlob) =>
        {
            // Sanity.
            Assert.AreEqual(CollisionType.Convex, colliderBlob.Value.CollisionType);
            Assert.AreEqual(ColliderType.Polygon, colliderBlob.Value.ColliderType);

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

            Assert.AreEqual(m_Points.Length, collider.VertexCount);
            var vertices = collider.Vertices;
            for (var i = 0; i < m_Points.Length; ++i)
            {
                PhysicsAssert.AreEqual(m_Points[i], vertices[m_GiftWrappedIndices[i]], Epsilon);
            }
        });
예제 #4
0
    public void CircleCollider_Root_ConversionTest()
    {
        var expectedOffset = new float2(3f, -4f);
        var expectedRadius = 1.6f;

        // Set-up the hierarchy.
        {
            Root = new GameObject();

            var collider = CreateClassicComponent <CircleCollider2D>(Root);
            collider.offset = expectedOffset;
            collider.radius = expectedRadius;
        }

        // Run the conversion test and validate.
        RunConversionTest(
            (BlobAssetReference <Unity.U2D.Entities.Physics.Collider> colliderBlob) =>
        {
            // Sanity.
            Assert.AreEqual(CollisionType.Convex, colliderBlob.Value.CollisionType);
            Assert.AreEqual(ColliderType.Circle, colliderBlob.Value.ColliderType);

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

            PhysicsAssert.AreEqual(expectedOffset, collider.Center, Epsilon);
            Assert.AreEqual(expectedRadius, collider.Radius, Epsilon);
        });
예제 #5
0
    public void TestRotate()
    {
        Random  rnd = new Random(1);
        Vector3 p1 = RandomUtil.Vector3(rnd); float m1 = RandomUtil.PositiveFloat(rnd);
        Vector3 p2 = RandomUtil.Vector3(rnd); float m2 = RandomUtil.PositiveFloat(rnd);
        Vector3 p3 = RandomUtil.Vector3(rnd); float m3 = RandomUtil.PositiveFloat(rnd);

        Quaternion rotation = RandomUtil.UnitQuaternion(rnd);

        var massMoment        = new MassMoment();
        var rotatedMassMoment = new MassMoment();

        massMoment.AddInplace(m1, p1); rotatedMassMoment.AddInplace(m1, Vector3.Transform(p1, rotation));
        massMoment.AddInplace(m2, p2); rotatedMassMoment.AddInplace(m2, Vector3.Transform(p2, rotation));
        massMoment.AddInplace(m3, p3); rotatedMassMoment.AddInplace(m3, Vector3.Transform(p3, rotation));

        PhysicsAssert.AreEqual(massMoment.Rotate(rotation), rotatedMassMoment, Acc);
    }
예제 #6
0
    public void TestTranslate()
    {
        Random  rnd = new Random(1);
        Vector3 p1 = RandomUtil.Vector3(rnd); float m1 = RandomUtil.PositiveFloat(rnd);
        Vector3 p2 = RandomUtil.Vector3(rnd); float m2 = RandomUtil.PositiveFloat(rnd);
        Vector3 p3 = RandomUtil.Vector3(rnd); float m3 = RandomUtil.PositiveFloat(rnd);

        Vector3 translation = RandomUtil.Vector3(rnd);

        var massMoment           = new MassMoment();
        var translatedMassMoment = new MassMoment();

        massMoment.AddInplace(m1, p1); translatedMassMoment.AddInplace(m1, p1 + translation);
        massMoment.AddInplace(m2, p2); translatedMassMoment.AddInplace(m2, p2 + translation);
        massMoment.AddInplace(m3, p3); translatedMassMoment.AddInplace(m3, p3 + translation);

        PhysicsAssert.AreEqual(massMoment.Translate(translation), translatedMassMoment, Acc);
    }
예제 #7
0
    public void TestAddingMassMoments()
    {
        Random  rnd = new Random(2);
        Vector3 p1 = RandomUtil.Vector3(rnd); float m1 = RandomUtil.PositiveFloat(rnd);
        Vector3 p2 = RandomUtil.Vector3(rnd); float m2 = RandomUtil.PositiveFloat(rnd);
        Vector3 p3 = RandomUtil.Vector3(rnd); float m3 = RandomUtil.PositiveFloat(rnd);

        var accumA = new MassMoment();

        accumA.AddInplace(m1, p1);
        accumA.AddInplace(m2, p2);
        accumA.AddInplace(m3, p3);

        var accumB = new MassMoment();

        accumB.AddInplace(m1, p1);
        var accumB2 = new MassMoment();

        accumB2.AddInplace(m2, p2);
        accumB2.AddInplace(m3, p3);
        accumB.AddInplace(accumB2);

        PhysicsAssert.AreEqual(accumA, accumB, Acc);
    }
예제 #8
0
    public void KinematicRigidbody_ConversionTest()
    {
        var expectedLinearVelocity  = new float2(5f, -3f);
        var expectedAngularVelocity = 4f;

        // Set-up the hierarchy.
        {
            Root = new GameObject();
            var rigidbody = CreateClassicComponent <Rigidbody2D>(Root);
            rigidbody.bodyType        = RigidbodyType2D.Kinematic;
            rigidbody.mass            = 7f;
            rigidbody.velocity        = expectedLinearVelocity;
            rigidbody.angularVelocity = expectedAngularVelocity;
        }

        // Run the GameObject Conversion.
        RunConversion(Root);

        // Verify the Conversion.
        using (
            var query = EntityManager.CreateEntityQuery(
                new EntityQueryDesc
        {
            All = new ComponentType[]
            {
                ComponentType.ReadOnly <PhysicsMass>(),
                ComponentType.ReadOnly <PhysicsVelocity>()
            },
            None = new ComponentType[]
            {
                ComponentType.ReadOnly <PhysicsDamping>(),
                ComponentType.ReadOnly <PhysicsGravity>(),
            }
        }))
        {
            Assert.AreEqual(1, query.CalculateEntityCount(), "Was expecting a single entity!");

            // Fetch the entity.
            using (var entities = query.ToEntityArray(Allocator.TempJob))
            {
                var entity = entities[0];

                // Sanity check.
                Assert.IsFalse(HasComponent <PhysicsColliderBlob>(entity));
                Assert.IsFalse(HasComponent <PhysicsDamping>(entity));
                Assert.IsFalse(HasComponent <PhysicsGravity>(entity));

                // Mass.
                {
                    Assert.IsTrue(HasComponent <PhysicsMass>(entity));
                    var mass = GetComponentData <PhysicsMass>(entity);

                    Assert.AreEqual(0f, mass.GetMass(), Epsilon);
                    PhysicsAssert.AreEqual(new float2(0f), mass.LocalCenterOfMass, Epsilon);
                    Assert.AreEqual(0f, mass.GetInertia(), Epsilon);
                }

                // Velocity.
                {
                    Assert.IsTrue(HasComponent <PhysicsVelocity>(entity));
                    var velocity = GetComponentData <PhysicsVelocity>(entity);

                    PhysicsAssert.AreEqual(expectedLinearVelocity, velocity.Linear, Epsilon);
                    Assert.AreEqual(expectedAngularVelocity, velocity.Angular, Epsilon);
                }
            }
        }
    }
예제 #9
0
    public void DynamicRigidbody_ConversionTest()
    {
        var expectedMass            = 7f;
        var expectedLinearVelocity  = new float2(5f, -3f);
        var expectedAngularVelocity = 4f;
        var expectedLinearDamping   = 0.6f;
        var expectedAngularDamping  = 0.3f;
        var expectedGravityScale    = 1.2f;

        // Set-up the hierarchy.
        {
            Root = new GameObject();
            var rigidbody = CreateClassicComponent <Rigidbody2D>(Root);
            rigidbody.bodyType        = RigidbodyType2D.Dynamic;
            rigidbody.mass            = expectedMass;
            rigidbody.velocity        = expectedLinearVelocity;
            rigidbody.angularVelocity = expectedAngularVelocity;
            rigidbody.drag            = expectedLinearDamping;
            rigidbody.angularDrag     = expectedAngularDamping;
            rigidbody.gravityScale    = expectedGravityScale;
        }

        // Run the GameObject Conversion.
        RunConversion(Root);

        // Verify the Conversion.
        using (
            var query = EntityManager.CreateEntityQuery(
                typeof(PhysicsMass),
                typeof(PhysicsVelocity),
                typeof(PhysicsDamping),
                typeof(PhysicsGravity)
                ))
        {
            Assert.AreEqual(1, query.CalculateEntityCount(), "Was expecting a single entity!");

            // Fetch the entity.
            using (var entities = query.ToEntityArray(Allocator.TempJob))
            {
                var entity = entities[0];

                // Sanity check.
                Assert.IsFalse(HasComponent <PhysicsColliderBlob>(entity));

                // Mass.
                {
                    Assert.IsTrue(HasComponent <PhysicsMass>(entity));
                    var mass = GetComponentData <PhysicsMass>(entity);

                    Assert.AreEqual(expectedMass, mass.GetMass(), Epsilon);
                    PhysicsAssert.AreEqual(new float2(0f), mass.LocalCenterOfMass, Epsilon);
                    Assert.AreEqual(1f, mass.GetInertia(), Epsilon);
                }

                // Velocity.
                {
                    Assert.IsTrue(HasComponent <PhysicsVelocity>(entity));
                    var velocity = GetComponentData <PhysicsVelocity>(entity);

                    PhysicsAssert.AreEqual(expectedLinearVelocity, velocity.Linear, Epsilon);
                    Assert.AreEqual(expectedAngularVelocity, velocity.Angular, Epsilon);
                }

                // Damping.
                {
                    Assert.IsTrue(HasComponent <PhysicsDamping>(entity));
                    var damping = GetComponentData <PhysicsDamping>(entity);

                    Assert.AreEqual(expectedLinearDamping, damping.Linear, Epsilon);
                    Assert.AreEqual(expectedAngularDamping, damping.Angular, Epsilon);
                }

                // Gravity scale.
                {
                    Assert.IsTrue(HasComponent <PhysicsGravity>(entity));
                    var gravity = GetComponentData <PhysicsGravity>(entity);

                    Assert.AreEqual(expectedGravityScale, gravity.Scale, Epsilon);
                }
            }
        }
    }