Ejemplo n.º 1
0
        public ConstraintDemo()
        {
            CollisionShape shape = new BoxShape(new Vector3(HalfExtents, HalfExtents, HalfExtents));
            Matrix         trans = Matrix.Identity;

            trans.Translation = new Vector3(0, 20, 0);

            float mass = 1f;
            //Point to Point constraint (ball socket)
            {
                RigidBody bodyA = CreateRigidBody(mass, trans, shape);
                trans.Translation = new Vector3(2 * HalfExtents, 20, 0);

                mass = 1f;
                RigidBody bodyB = null;
                //RigidBody bodyB = CreateRigidBody(mass, trans, shape);
                //bodyB.ActivationState = ActivationState.DisableDeactivation;
                //bodyB.SetDamping(0.3f, 0.3f);

                Vector3 pivotInA = new Vector3(HalfExtents, -HalfExtents, -HalfExtents);
                Vector3 axisInA  = new Vector3(0, 0, 1);

                Vector3 pivotInB = bodyB != null?MathHelper.MatrixToVector(MathHelper.InvertMatrix(bodyB.CenterOfMassTransform), MathHelper.MatrixToVector(bodyA.CenterOfMassTransform, pivotInA)) : pivotInA;

                Vector3 axisInB = bodyB != null?
                                  Vector3.TransformNormal(Vector3.TransformNormal(axisInA, bodyB.CenterOfMassTransform), MathHelper.InvertMatrix(bodyB.CenterOfMassTransform)) :
                                      Vector3.TransformNormal(axisInA, bodyA.CenterOfMassTransform);

                //TypedConstraint p2p = new Point2PointConstraint(bodyA, bodyB, pivotInA, pivotInB);
                //TypedConstraint hinge = new HingeConstraint(bodyA, bodyB, pivotInA, pivotInB, axisInA, axisInB);
                HingeConstraint hinge = new HingeConstraint(bodyA, pivotInA, axisInA);

                //use zero targetVelocity and a small maxMotorImpulse to simulate joint friction
                //float	targetVelocity = 0.0f;
                //float	maxMotorImpulse = 0.01;
                float targetVelocity  = 1.0f;
                float maxMotorImpulse = 1.0f;
                hinge.EnableAngularMotor(true, targetVelocity, maxMotorImpulse);

                PhysicsWorld.AddConstraint(hinge);
            }

            // create a slider, using the generic D6 constraint
            {
                mass = 1f;
                Vector3 sliderWorldPos    = new Vector3(0, 10, 0);
                Vector3 sliderAxis        = new Vector3(1, 0, 0);
                float   angle             = 0;
                Matrix  sliderOrientation = Matrix.CreateFromQuaternion(new Quaternion(sliderAxis, angle));

                trans             = Matrix.Identity;
                trans.Translation = sliderWorldPos;
                //trans.setBasis(sliderOrientation);
                _sliderTransform = trans;

                _d6BodyA = CreateRigidBody(mass, trans, shape);
                _d6BodyA.ActivationState = ActivationState.DisableDeactivation;
                RigidBody fixedBody1 = CreateRigidBody(0, trans, null);

                Matrix frameInA, frameInB;
                frameInA = Matrix.Identity;
                frameInB = Matrix.Identity;

                Generic6DofConstraint slider = new Generic6DofConstraint(_d6BodyA, fixedBody1, frameInA, frameInB);
                slider.SetLinearLowerLimit(_lowerSliderLimit);
                slider.SetLinearUpperLimit(_hiSliderLimit);

                //range should be small, otherwise singularities will 'explode' the constraint
                slider.SetAngularLowerLimit(new Vector3(10, 0, 0));
                slider.SetAngularUpperLimit(new Vector3(0, 0, 0));

                PhysicsWorld.AddConstraint(slider);
            }
        }