public override void Create(EntityManager entityManager, GameObjectConversionSystem conversionSystem)
        {
            if (AutoSetConnected)
            {
                RigidTransform bFromA = math.mul(math.inverse(worldFromB), worldFromA);
                PositionInConnectedEntity          = math.transform(bFromA, PositionLocal);
                AxisInConnectedEntity              = math.mul(bFromA.rot, AxisLocal);
                PerpendicularAxisInConnectedEntity = math.mul(bFromA.rot, PerpendicularAxisLocal);
            }

            CreateJointEntity(JointData.CreatePrismatic(
                                  new JointFrame
            {
                Axis = AxisLocal,
                PerpendicularAxis = PerpendicularAxisLocal,
                Position          = PositionLocal
            },
                                  new JointFrame
            {
                Axis = AxisInConnectedEntity,
                PerpendicularAxis = PerpendicularAxisInConnectedEntity,
                Position          = PositionInConnectedEntity
            },
                                  new FloatRange(MinDistanceOnAxis, MaxDistanceOnAxis),
                                  new FloatRange(MinDistanceFromAxis, MaxDistanceFromAxis)
                                  ),
                              entityManager, conversionSystem
                              );
        }
        public override unsafe void Create(EntityManager entityManager)
        {
            if (AutoSetConnected)
            {
                PositionLocal = math.transform(math.inverse(worldFromA), math.transform(worldFromB, PositionInConnectedEntity));
            }

            CreateJointEntity(JointData.CreatePrismatic(PositionLocal, PositionInConnectedEntity, AxisInConnectedEntity,
                                                        MinDistanceOnAxis, MaxDistanceOnAxis, MinDistanceFromAxis, MaxDistanceFromAxis), entityManager);
        }
        public override unsafe void Create(EntityManager entityManager)
        {
            if (AutoSetConnected)
            {
                RigidTransform bFromA = math.mul(math.inverse(worldFromB), worldFromA);
                PositionInConnectedEntity          = math.transform(bFromA, PositionLocal);
                AxisInConnectedEntity              = math.mul(bFromA.rot, AxisLocal);
                PerpendicularAxisInConnectedEntity = math.mul(bFromA.rot, PerpendicularAxisLocal);
            }

            CreateJointEntity(JointData.CreatePrismatic(PositionLocal, PositionInConnectedEntity, AxisLocal, AxisInConnectedEntity,
                                                        PerpendicularAxisLocal, PerpendicularAxisInConnectedEntity, MinDistanceOnAxis, MaxDistanceOnAxis, MinDistanceFromAxis, MaxDistanceFromAxis), entityManager);
        }
Exemplo n.º 4
0
        public void JointDataCreatePrismaticTest()
        {
            var positionAinA = new float3(0.0f, 1.0f, 2.0f);
            var positionBinB = new float3(1.0f, 0.0f, 3.0f);
            var axisInB      = float3.zero;

            var minDistanceOnAxis   = 1.0f;
            var maxDistanceOnAxis   = 10.0f;
            var minDistanceFromAxis = 2.0f;
            var maxDistanceFromAxis = 20.0f;

            var jointDataRef = JointData.CreatePrismatic(positionAinA, positionBinB, axisInB, minDistanceOnAxis, maxDistanceOnAxis, minDistanceFromAxis, maxDistanceFromAxis);

            var jointData = jointDataRef.Value;

            Assert.AreEqual(positionAinA, jointData.AFromJoint.Translation);
            Assert.AreEqual(positionBinB, jointData.BFromJoint.Translation);
            Assert.AreEqual(1, jointData.Version);
            Assert.AreEqual(2, jointData.NumConstraints);

            var planarConstraint = jointDataRef.Value.Constraints[0];

            Assert.AreEqual(new bool3(true, false, false), planarConstraint.ConstrainedAxes);
            Assert.AreEqual(ConstraintType.Linear, planarConstraint.Type);
            Assert.AreEqual(1.0f, planarConstraint.Min);
            Assert.AreEqual(10.0f, planarConstraint.Max);
            Assert.AreEqual(Constraint.DefaultSpringFrequency, planarConstraint.SpringFrequency);
            Assert.AreEqual(Constraint.DefaultSpringDamping, planarConstraint.SpringDamping);

            var cylindricalConstraint = jointDataRef.Value.Constraints[1];

            Assert.AreEqual(new bool3(false, true, true), cylindricalConstraint.ConstrainedAxes);
            Assert.AreEqual(ConstraintType.Linear, cylindricalConstraint.Type);
            Assert.AreEqual(2.0f, cylindricalConstraint.Min);
            Assert.AreEqual(20.0f, cylindricalConstraint.Max);
            Assert.AreEqual(Constraint.DefaultSpringFrequency, cylindricalConstraint.SpringFrequency);
            Assert.AreEqual(Constraint.DefaultSpringDamping, cylindricalConstraint.SpringDamping);
        }
    protected override void Start()
    {
        float3 gravity = float3.zero;

        base.init(gravity);

        // Enable the joint viewer
        SetDebugDisplay(new Unity.Physics.Authoring.PhysicsDebugDisplayData
        {
            DrawJoints = 1
        });

        // Make soft ball and sockets
        {
            BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = new float3(0.2f, 0.2f, 0.2f),
                BevelRadius = 0.0f
            });

            // Make joints with different spring frequency.  The leftmost joint should oscillate at 0.5hz, the next at 1hz, the next at 1.5hz, etc.
            for (int i = 0; i < 10; i++)
            {
                // Create a body
                float3 position = new float3((i - 4.5f) * 1.0f, 0, 0);
                float3 velocity = new float3(0, -10.0f, 0);
                Entity body     = CreateDynamicBody(
                    position, quaternion.identity, collider, velocity, float3.zero, 1.0f);

                // Create the ball and socket joint
                float3 pivotLocal   = float3.zero;
                float3 pivotInWorld = math.transform(GetBodyTransform(body), pivotLocal);

                BlobAssetReference <JointData> jointData;
                jointData = JointData.CreateBallAndSocket(pivotLocal, pivotInWorld);
                jointData.Value.Constraints[0].SpringDamping   = 0.0f;
                jointData.Value.Constraints[0].SpringFrequency = 0.5f * (float)(i + 1);
                CreateJoint(jointData, body, Entity.Null);
            }
        }

        // Make soft limited hinges
        {
            BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = new float3(0.4f, 0.1f, 0.6f),
                BevelRadius = 0.0f
            });

            // First row has soft limit with hard hinge + pivot, second row has everything soft
            for (int j = 0; j < 2; j++)
            {
                for (int i = 0; i < 10; i++)
                {
                    // Create a body
                    float3 position        = new float3((i - 4.5f) * 1.0f, 0, (j + 1) * 3.0f);
                    float3 velocity        = new float3(0, -10.0f, 0);
                    float3 angularVelocity = new float3(0, 0, -10.0f);
                    Entity body            = CreateDynamicBody(
                        position, quaternion.identity, collider, velocity, angularVelocity, 1.0f);

                    // Create the limited hinge joint
                    float3 pivotLocal           = new float3(0, 0, 0);
                    float3 pivotInWorld         = math.transform(GetBodyTransform(body), pivotLocal);
                    float3 axisLocal            = new float3(0, 0, 1);
                    float3 axisInWorld          = axisLocal;
                    float3 perpendicularLocal   = new float3(0, 1, 0);
                    float3 perpendicularInWorld = perpendicularLocal;

                    BlobAssetReference <JointData> jointData;
                    jointData = JointData.CreateLimitedHinge(pivotLocal, pivotInWorld, axisLocal, axisInWorld, perpendicularLocal, perpendicularInWorld, 0.0f, 0.0f);

                    // First constraint is the limit, next two are the hinge and pivot
                    for (int k = 0; k < 1 + 2 * j; k++)
                    {
                        jointData.Value.Constraints[k].SpringDamping   = 0.0f;
                        jointData.Value.Constraints[k].SpringFrequency = 0.5f * (float)(i + 1);
                    }

                    CreateJoint(jointData, body, Entity.Null);
                }
            }
        }

        // Make a soft prismatic
        {
            BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry
            {
                Center      = float3.zero,
                Orientation = quaternion.identity,
                Size        = new float3(0.2f, 0.2f, 0.2f),
                BevelRadius = 0.0f
            });

            // Create a body
            float3 position = new float3(0, 0, 9.0f);
            float3 velocity = new float3(50.0f, 0, 0);
            Entity body     = CreateDynamicBody(
                position, quaternion.identity, collider, velocity, float3.zero, 1.0f);

            // Create the prismatic joint
            float3 pivotLocal           = float3.zero;
            float3 pivotInWorld         = math.transform(GetBodyTransform(body), pivotLocal);
            float3 axisLocal            = new float3(1, 0, 0);
            float3 axisInWorld          = axisLocal;
            float3 perpendicularLocal   = new float3(0, 1, 0);
            float3 perpendicularInWorld = perpendicularLocal;

            BlobAssetReference <JointData> jointData;
            jointData = JointData.CreatePrismatic(pivotLocal, pivotInWorld, axisLocal, axisInWorld, perpendicularLocal, perpendicularInWorld, -2.0f, 2.0f, 0.0f, 0.0f);
            jointData.Value.Constraints[0].SpringDamping   = 0.0f;
            jointData.Value.Constraints[0].SpringFrequency = 5.0f;
            CreateJoint(jointData, body, Entity.Null);
        }
    }