Пример #1
0
        public MassSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // Add basic force effects.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Add a ground plane.
            RigidBody groundPlane = new RigidBody(new PlaneShape(Vector3F.UnitY, 0))
            {
                Name       = "GroundPlane", // Names are not required but helpful for debugging.
                MotionType = MotionType.Static,
            };

            Simulation.RigidBodies.Add(groundPlane);

            // Add a static body that serves as the base of the see-saw.
            RigidBody body = new RigidBody(new BoxShape(0.1f, 1, 2))
            {
                MotionType = MotionType.Static,
                Pose       = new Pose(new Vector3F(0, 0.5f, 0))
            };

            Simulation.RigidBodies.Add(body);

            // Create a plank.
            body = new RigidBody(new BoxShape(5, 0.1f, 1.3f))
            {
                Pose = new Pose(new Vector3F(0, 1.05f, 0))
            };
            Simulation.RigidBodies.Add(body);

            // ----- Create a few light bodies on the left.
            Shape boxShape = new BoxShape(0.7f, 0.7f, 0.7f);

            // The light bodies have a density of 200.
            // (The first three parameters of FromShapeAndDensity are: shape, scale, density.
            // The last two parameters are required for shapes where the mass properties can only
            // be approximated using an iterative procedure: 0.01 --> The shape is approximated
            // up to approx. 1%. The procedure aborts after 3 iterations.
            // Since the shape is a box FromShapeAndDensity computes the exact mass and the last two
            // parameters are irrelevant in this case.)
            MassFrame mass = MassFrame.FromShapeAndDensity(boxShape, Vector3F.One, 200, 0.01f, 3);

            body = new RigidBody(boxShape, mass, null)
            {
                Pose = new Pose(new Vector3F(-1.5f, 2f, 0))
            };
            Simulation.RigidBodies.Add(body);

            body = new RigidBody(boxShape, mass, null)
            {
                Pose = new Pose(new Vector3F(-1.5f, 2.7f, 0))
            };
            Simulation.RigidBodies.Add(body);

            body = new RigidBody(boxShape, mass, null)
            {
                Pose = new Pose(new Vector3F(-1.5f, 3.4f, 0))
            };
            Simulation.RigidBodies.Add(body);

            // ----- Create a heavy body on the right.
            // The heavy body has a density of 2000.
            mass = MassFrame.FromShapeAndDensity(boxShape, Vector3F.One, 2000, 0.01f, 3);
            body = new RigidBody(boxShape, mass, null)
            {
                Pose = new Pose(new Vector3F(1.5f, 3, 0))
            };
            Simulation.RigidBodies.Add(body);
        }
Пример #2
0
        public BuoyancySample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // Add basic force effects.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // ----- Buoyancy Force Effect
            // Buoyancy is a force effect that lets bodies swim in water. The water area is
            // defined by two properties:
            //  - Buoyancy.AreaOfEffect defines which objects are affected.
            //  - Buoyancy.Surface defines the water level within this area.

            // The area of effect can be defined in different ways. In this sample we will use
            // a geometric object ("trigger volume").

            // First, define the shape of the water area. We will create simple pool.
            Shape    poolShape  = new BoxShape(16, 10, 16);
            Vector3F poolCenter = new Vector3F(0, -5, 0);

            // Then create a geometric object for the water area. (A GeometricObject is required
            // to position the shape in the world. A GeometricObject stores shape, scale, position,
            // orientation, ...)
            GeometricObject waterGeometry = new GeometricObject(poolShape, new Pose(poolCenter));

            // Then create a collision object for the geometric object. (A CollisionObject required
            // because the geometry should be used for collision detection with other objects.)
            _waterCollisionObject = new CollisionObject(waterGeometry)
            {
                // Assign the object to a different collision group:
                // The Grab component (see Grab.cs) uses a ray to perform hit tests. We don't want the ray
                // to collide with the water. Therefore, we need to assign the water collision object to a
                // different collision group. The general geometry is in collision group 0. The rays are in
                // collision group 2. Add the water to collision group 1. Collision between 0 and 2 are
                // enabled. Collision between 1 and 2 need to be disabled - this collision filter was set
                // in PhysicsGame.cs.
                CollisionGroup = 1,

                // Set the type to "Trigger". This improves the performance because the collision
                // detection does not need to compute detailed contact information. The collision
                // detection only returns whether an objects has contact with the water.
                Type = CollisionObjectType.Trigger,
            };

            // The collision object needs to be added into the collision domain of the simulation.
            Simulation.CollisionDomain.CollisionObjects.Add(_waterCollisionObject);

            // Now we can add the buoyancy effect.
            Buoyancy buoyancy = new Buoyancy
            {
                AreaOfEffect = new GeometricAreaOfEffect(_waterCollisionObject),
                Surface      = new Plane(Vector3F.Up, 0),

                Density     = 1000f, // The density of water (1000 kg/m³).
                AngularDrag = 0.4f,
                LinearDrag  = 4f,

                // Optional: Let the objects drift in the water by setting a flow velocity.
                //Velocity = new Vector3F(-0.5f, 0, 0.5f),
            };

            Simulation.ForceEffects.Add(buoyancy);


            // Add static area around the pool.
            RigidBody bottom = new RigidBody(new BoxShape(36, 2, 36))
            {
                MotionType = MotionType.Static,
                Pose       = new Pose(new Vector3F(0, -11, 0)),
            };

            Simulation.RigidBodies.Add(bottom);
            RigidBody left = new RigidBody(new BoxShape(10, 10, 36))
            {
                MotionType = MotionType.Static,
                Pose       = new Pose(new Vector3F(-13, -5, 0)),
            };

            Simulation.RigidBodies.Add(left);
            RigidBody right = new RigidBody(new BoxShape(10, 10, 36))
            {
                MotionType = MotionType.Static,
                Pose       = new Pose(new Vector3F(13, -5, 0)),
            };

            Simulation.RigidBodies.Add(right);
            RigidBody front = new RigidBody(new BoxShape(16, 10, 10))
            {
                MotionType = MotionType.Static,
                Pose       = new Pose(new Vector3F(0, -5, 13)),
            };

            Simulation.RigidBodies.Add(front);
            RigidBody back = new RigidBody(new BoxShape(16, 10, 10))
            {
                MotionType = MotionType.Static,
                Pose       = new Pose(new Vector3F(0, -5, -13)),
            };

            Simulation.RigidBodies.Add(back);

            // ----- Add some random objects to test the effect.
            // Note: Objects swim if their density is less than the density of water. They sink
            // if the density is greater than the density of water.
            // We can define the density of objects by explicitly setting the mass.

            // Add a swimming board.
            BoxShape  raftShape = new BoxShape(4, 0.3f, 4);
            MassFrame raftMass  = MassFrame.FromShapeAndDensity(raftShape, Vector3F.One, 700, 0.01f, 3);
            RigidBody raft      = new RigidBody(raftShape, raftMass, null)
            {
                Pose = new Pose(new Vector3F(0, 4, 0)),
            };

            Simulation.RigidBodies.Add(raft);

            // Add some boxes on top of the swimming board.
            BoxShape  boxShape = new BoxShape(1, 1, 1);
            MassFrame boxMass  = MassFrame.FromShapeAndDensity(boxShape, Vector3F.One, 700, 0.01f, 3);

            for (int i = 0; i < 5; i++)
            {
                RigidBody box = new RigidBody(boxShape, boxMass, null)
                {
                    Pose = new Pose(new Vector3F(0, 5 + i * 1.1f, 0)),
                };
                Simulation.RigidBodies.Add(box);
            }

            // Add some "heavy stones" represented as spheres.
            SphereShape stoneShape = new SphereShape(0.5f);
            MassFrame   stoneMass  = MassFrame.FromShapeAndDensity(stoneShape, Vector3F.One, 2500, 0.01f, 3);

            for (int i = 0; i < 10; i++)
            {
                Vector3F position = RandomHelper.Random.NextVector3F(-9, 9);
                position.Y = 5;

                RigidBody stone = new RigidBody(stoneShape, stoneMass, null)
                {
                    Pose = new Pose(position),
                };
                Simulation.RigidBodies.Add(stone);
            }

            // Add some very light objects.
            CylinderShape cylinderShape = new CylinderShape(0.3f, 1);
            MassFrame     cylinderMass  = MassFrame.FromShapeAndDensity(cylinderShape, Vector3F.One, 500, 0.01f, 3);

            for (int i = 0; i < 10; i++)
            {
                Vector3F position = RandomHelper.Random.NextVector3F(-9, 9);
                position.Y = 5;
                QuaternionF orientation = RandomHelper.Random.NextQuaternionF();

                RigidBody cylinder = new RigidBody(cylinderShape, cylinderMass, null)
                {
                    Pose = new Pose(position, orientation),
                };
                Simulation.RigidBodies.Add(cylinder);
            }
        }
Пример #3
0
        public ConstraintCarSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // Add basic force effects.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Create a material with high friction - this will be used for the ground and the wheels
            // to give the wheels some traction.
            UniformMaterial roughMaterial = new UniformMaterial
            {
                DynamicFriction = 1,
                StaticFriction  = 1,
            };

            // Add a ground plane.
            RigidBody groundPlane = new RigidBody(new PlaneShape(Vector3F.UnitY, 0), null, roughMaterial)
            {
                MotionType = MotionType.Static,
            };

            Simulation.RigidBodies.Add(groundPlane);

            // Now, we build a car out of one box for the chassis and 4 cylindric wheels.
            // Front wheels are fixed with Hinge2Joints and motorized with AngularVelocityMotors.
            // Back wheels are fixed with HingeJoints and not motorized. - This creates a sloppy
            // car configuration. Please note that cars for racing games are not normally built with
            // simple constraints - nevertheless, it is funny to do and play with it.

            // Check out the "Vehicle Sample" (not included in this project)! The "Vehicle Sample"
            // provides a robust ray-car implementation.)

            // ----- Chassis
            BoxShape  chassisShape = new BoxShape(1.7f, 1, 4f);
            MassFrame chassisMass  = MassFrame.FromShapeAndDensity(chassisShape, Vector3F.One, 200, 0.01f, 3);

            // Here is a trick: The car topples over very easily. By lowering the center of mass we
            // make it more stable.
            chassisMass.Pose = new Pose(new Vector3F(0, -1, 0));
            RigidBody chassis = new RigidBody(chassisShape, chassisMass, null)
            {
                Pose = new Pose(new Vector3F(0, 1, 0)),
            };

            Simulation.RigidBodies.Add(chassis);

            // ------ Wheels
            CylinderShape cylinderShape  = new CylinderShape(0.4f, 0.3f);
            MassFrame     wheelMass      = MassFrame.FromShapeAndDensity(cylinderShape, Vector3F.One, 500, 0.01f, 3);
            RigidBody     wheelFrontLeft = new RigidBody(cylinderShape, wheelMass, roughMaterial)
            {
                Pose = new Pose(new Vector3F(0, 1, 0), Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };

            Simulation.RigidBodies.Add(wheelFrontLeft);
            RigidBody wheelFrontRight = new RigidBody(cylinderShape, wheelMass, roughMaterial)
            {
                Pose = new Pose(new Vector3F(0, 1, 0), Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };

            Simulation.RigidBodies.Add(wheelFrontRight);
            RigidBody wheelBackLeft = new RigidBody(cylinderShape, wheelMass, roughMaterial)
            {
                Pose = new Pose(new Vector3F(0, 1, 0), Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };

            Simulation.RigidBodies.Add(wheelBackLeft);
            RigidBody wheelBackRight = new RigidBody(cylinderShape, wheelMass, roughMaterial)
            {
                Pose = new Pose(new Vector3F(0, 1, 0), Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };

            Simulation.RigidBodies.Add(wheelBackRight);

            // ----- Hinge2Joints for the front wheels.
            // A Hinge2Joint allows a limited rotation on the first constraint axis - the steering
            // axis. The second constraint axis is locked and the third constraint axis is the
            // rotation axis of the wheels.
            _frontLeftHinge = new Hinge2Joint
            {
                BodyA = chassis,
                // --> To define the constraint anchor orientation for the chassis:
                // The columns are the axes. We set the local y axis in the first column. This is
                // steering axis. In the last column we set the -x axis. This is the wheel rotation axis.
                // In the middle column is a vector that is normal to the first and last axis.
                // (All three columns are orthonormal and form a valid rotation matrix.)
                AnchorPoseALocal = new Pose(new Vector3F(-0.9f, -0.4f, -1.4f),
                                            new Matrix33F(0, 0, -1,
                                                          1, 0, 0,
                                                          0, -1, 0)),
                BodyB = wheelFrontLeft,
                // --> To define the constraint anchor orientation for the chassis:
                // The columns are the axes. We set the local x axis in the first column. This is
                // steering axis. In the last column we set the y axis. This is the wheel rotation axis.
                // (In local space of a cylinder the cylinder axis is the +y axis.)
                // In the middle column is a vector that is normal to the first and last axis.
                // (All three columns are orthonormal and form a valid rotation matrix.)
                AnchorPoseBLocal = new Pose(new Matrix33F(1, 0, 0,
                                                          0, 0, 1,
                                                          0, -1, 0)),
                CollisionEnabled = false,
                Minimum          = new Vector2F(-0.7f, float.NegativeInfinity),
                Maximum          = new Vector2F(0.7f, float.PositiveInfinity),
            };
            Simulation.Constraints.Add(_frontLeftHinge);

            _frontRightHinge = new Hinge2Joint
            {
                BodyA            = chassis,
                BodyB            = wheelFrontRight,
                AnchorPoseALocal = new Pose(new Vector3F(0.9f, -0.4f, -1.4f),
                                            new Matrix33F(0, 0, -1,
                                                          1, 0, 0,
                                                          0, -1, 0)),
                AnchorPoseBLocal = new Pose(new Matrix33F(1, 0, 0,
                                                          0, 0, 1,
                                                          0, -1, 0)),
                CollisionEnabled = false,
                Minimum          = new Vector2F(-0.7f, float.NegativeInfinity),
                Maximum          = new Vector2F(0.7f, float.PositiveInfinity),
            };
            Simulation.Constraints.Add(_frontRightHinge);

            // ----- HingeJoints for the back wheels.
            // Hinges allow free rotation on the first constraint axis.
            HingeJoint backLeftHinge = new HingeJoint
            {
                BodyA            = chassis,
                AnchorPoseALocal = new Pose(new Vector3F(-0.9f, -0.4f, 1.4f)),
                BodyB            = wheelBackLeft,
                // --> To define the constraint anchor orientation:
                // The columns are the axes. We set the local y axis in the first column. This is
                // cylinder axis and should be the hinge axis. In the other two columns we set two
                // orthonormal vectors.
                // (All three columns are orthonormal and form a valid rotation matrix.)
                AnchorPoseBLocal = new Pose(new Matrix33F(0, 0, 1,
                                                          1, 0, 0,
                                                          0, 1, 0)),
                CollisionEnabled = false,
            };

            Simulation.Constraints.Add(backLeftHinge);
            HingeJoint backRightHinge = new HingeJoint
            {
                BodyA            = chassis,
                AnchorPoseALocal = new Pose(new Vector3F(0.9f, -0.4f, 1.4f)),
                BodyB            = wheelBackRight,
                AnchorPoseBLocal = new Pose(new Matrix33F(0, 0, 1,
                                                          1, 0, 0,
                                                          0, 1, 0)),
                CollisionEnabled = false,
            };

            Simulation.Constraints.Add(backRightHinge);

            // ----- Motors for the front wheels.
            // (Motor axes and target velocities are set in Update() below.)
            _frontLeftMotor = new AngularVelocityMotor
            {
                BodyA            = chassis,
                BodyB            = wheelFrontLeft,
                CollisionEnabled = false,

                // We use "single axis mode", which means the motor drives only on axis and does not
                // block motion orthogonal to this axis. - Rotation about the orthogonal axes is already
                // controlled by the Hinge2Joint.
                UseSingleAxisMode = true,

                // The motor has only limited power:
                MaxForce = 50000,
            };
            Simulation.Constraints.Add(_frontLeftMotor);

            _frontRightMotor = new AngularVelocityMotor
            {
                BodyA             = chassis,
                BodyB             = wheelFrontRight,
                CollisionEnabled  = false,
                UseSingleAxisMode = true,
                MaxForce          = 50000,
            };
            Simulation.Constraints.Add(_frontRightMotor);

            // ----- Drop a few boxes to create obstacles.
            BoxShape  boxShape = new BoxShape(1, 1, 1);
            MassFrame boxMass  = MassFrame.FromShapeAndDensity(boxShape, Vector3F.One, 100, 0.01f, 3);

            for (int i = 0; i < 20; i++)
            {
                Vector3F position = RandomHelper.Random.NextVector3F(-20, 20);
                position.Y = 5;
                QuaternionF orientation = RandomHelper.Random.NextQuaternionF();

                RigidBody body = new RigidBody(boxShape, boxMass, null)
                {
                    Pose = new Pose(position, orientation),
                };
                Simulation.RigidBodies.Add(body);
            }
        }
Пример #4
0
        // Using a softness value > 0 is important to give the ragdoll a more natural movement and to
        // avoid jittering.
        public static void AddRagdoll(Simulation simulation, float scale, Vector3F ragdollPosition, float softness, bool addDamping)
        {
            // Ragdolls are usually used in games to create realistic death animations of
            // characters. The character is usually rendered using a skinned triangle mesh.
            // But in the physics simulation the body parts of the character are represented
            // using simple shapes, such as spheres, capsules, boxes, or convex polyhedra,
            // which are connected with joints.
            // The physics simulations computes how these parts collide and fall. The positions
            // and orientations are then read back each frame to update the animation of the
            // triangle mesh.

            // In this example the ragdoll is built from spheres, capsules and boxes. The
            // rigid bodies are created in code. In practice, ragdolls should be built using
            // external tools, such as a 3D modeler or a game editor.

            #region ----- Create rigid bodies for the most relevant body parts -----

            // The density used for all bodies.
            const float density = 1000;

            BoxShape  pelvisShape = new BoxShape(0.3f * scale, 0.22f * scale, 0.20f * scale);
            MassFrame pelvisMass  = MassFrame.FromShapeAndDensity(pelvisShape, Vector3F.One, density, 0.01f, 3);
            RigidBody pelvis      = new RigidBody(pelvisShape, pelvisMass, null)
            {
                Pose = new Pose(new Vector3F(0f, 0.01f * scale, -0.03f * scale) + ragdollPosition),
            };
            simulation.RigidBodies.Add(pelvis);

            BoxShape  torsoShape = new BoxShape(0.35f * scale, 0.22f * scale, 0.44f * scale);
            MassFrame torsoMass  = MassFrame.FromShapeAndDensity(torsoShape, Vector3F.One, density, 0.01f, 3);
            RigidBody torso      = new RigidBody(torsoShape, torsoMass, null)
            {
                Pose = new Pose(new Vector3F(0f, 0.01f * scale, -0.4f * scale) + ragdollPosition),
            };
            simulation.RigidBodies.Add(torso);

            SphereShape headShape = new SphereShape(0.13f * scale);
            MassFrame   headMass  = MassFrame.FromShapeAndDensity(headShape, Vector3F.One, density, 0.01f, 3);
            RigidBody   head      = new RigidBody(headShape, headMass, null)
            {
                Pose = new Pose(new Vector3F(0f * scale, 0f, -0.776f * scale) + ragdollPosition),
            };
            simulation.RigidBodies.Add(head);

            CapsuleShape upperArmShape = new CapsuleShape(0.08f * scale, 0.3f * scale);
            MassFrame    upperArmMass  = MassFrame.FromShapeAndDensity(upperArmShape, Vector3F.One, density, 0.01f, 3);
            RigidBody    leftUpperArm  = new RigidBody(upperArmShape, upperArmMass, null)
            {
                Pose = new Pose(new Vector3F(-0.32f * scale, 0.06f * scale, -0.53f * scale) + ragdollPosition, Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(leftUpperArm);
            RigidBody rightUpperArm = new RigidBody(upperArmShape, upperArmMass, null)
            {
                Pose = new Pose(new Vector3F(0.32f * scale, 0.06f * scale, -0.53f * scale) + ragdollPosition, Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(rightUpperArm);

            CapsuleShape lowerArmShape = new CapsuleShape(0.08f * scale, 0.4f * scale);
            MassFrame    lowerArmMass  = MassFrame.FromShapeAndDensity(lowerArmShape, Vector3F.One, density, 0.01f, 3);
            RigidBody    leftLowerArm  = new RigidBody(lowerArmShape, lowerArmMass, null)
            {
                Pose = new Pose(new Vector3F(-0.62f * scale, 0.06f * scale, -0.53f * scale) + ragdollPosition, Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(leftLowerArm);
            RigidBody rightLowerArm = new RigidBody(lowerArmShape, lowerArmMass, null)
            {
                Pose = new Pose(new Vector3F(0.62f * scale, 0.06f * scale, -0.53f * scale) + ragdollPosition, Matrix33F.CreateRotationZ(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(rightLowerArm);

            CapsuleShape upperLegShape = new CapsuleShape(0.09f * scale, 0.5f * scale);
            MassFrame    upperLegMass  = MassFrame.FromShapeAndDensity(upperLegShape, Vector3F.One, density, 0.01f, 3);
            RigidBody    leftUpperLeg  = new RigidBody(upperLegShape, upperLegMass, null)
            {
                Pose = new Pose(new Vector3F(-0.10f * scale, 0.01f * scale, 0.233f * scale) + ragdollPosition, Matrix33F.CreateRotationX(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(leftUpperLeg);

            RigidBody rightUpperLeg = new RigidBody(upperLegShape, upperLegMass, null)
            {
                Pose = new Pose(new Vector3F(0.10f * scale, 0.01f * scale, 0.233f * scale) + ragdollPosition, Matrix33F.CreateRotationX(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(rightUpperLeg);

            CapsuleShape lowerLegShape = new CapsuleShape(0.08f * scale, 0.4f * scale);
            MassFrame    lowerLegMass  = MassFrame.FromShapeAndDensity(pelvisShape, Vector3F.One, density, 0.01f, 3);
            RigidBody    leftLowerLeg  = new RigidBody(lowerLegShape, lowerLegMass, null)
            {
                Pose = new Pose(new Vector3F(-0.11f * scale, 0.01f * scale, 0.7f * scale) + ragdollPosition, Matrix33F.CreateRotationX(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(leftLowerLeg);
            RigidBody rightLowerLeg = new RigidBody(lowerLegShape, lowerLegMass, null)
            {
                Pose = new Pose(new Vector3F(0.11f * scale, 0.01f * scale, 0.7f * scale) + ragdollPosition, Matrix33F.CreateRotationX(ConstantsF.PiOver2)),
            };
            simulation.RigidBodies.Add(rightLowerLeg);

            BoxShape  footShape = new BoxShape(0.12f * scale, 0.28f * scale, 0.07f * scale);
            MassFrame footMass  = MassFrame.FromShapeAndDensity(footShape, Vector3F.One, density, 0.01f, 3);
            RigidBody leftFoot  = new RigidBody(footShape, footMass, null)
            {
                Pose = new Pose(new Vector3F(-0.11f * scale, -0.06f * scale, 0.94f * scale) + ragdollPosition),
            };
            simulation.RigidBodies.Add(leftFoot);
            RigidBody rightFoot = new RigidBody(footShape, footMass, null)
            {
                Pose = new Pose(new Vector3F(0.11f * scale, -0.06f * scale, 0.94f * scale) + ragdollPosition),
            };
            simulation.RigidBodies.Add(rightFoot);
            #endregion

            #region ----- Add joints between body parts -----

            float errorReduction = 0.3f;
            float maxForce       = float.PositiveInfinity;

            Vector3F   pelvisJointPosition = new Vector3F(0f, 0.026f * scale, -0.115f * scale) + ragdollPosition;
            HingeJoint pelvisJoint         = new HingeJoint
            {
                BodyA            = torso,
                BodyB            = pelvis,
                AnchorPoseALocal = new Pose(torso.Pose.ToLocalPosition(pelvisJointPosition)),
                AnchorPoseBLocal = new Pose(pelvis.Pose.ToLocalPosition(pelvisJointPosition)),
                Minimum          = -0.5f,
                Maximum          = 1.1f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(pelvisJoint);

            Vector3F   neckJointPosition = new Vector3F(0f, 0.026f * scale, -0.690f * scale) + ragdollPosition;
            HingeJoint neckJoint         = new HingeJoint
            {
                BodyA            = head,
                BodyB            = torso,
                AnchorPoseALocal = new Pose(head.Pose.ToLocalPosition(neckJointPosition)),
                AnchorPoseBLocal = new Pose(torso.Pose.ToLocalPosition(neckJointPosition)),
                Minimum          = -1f,
                Maximum          = 1f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(neckJoint);

            Vector3F  leftShoulderJointPosition    = new Vector3F(-0.193f * scale, 0.056f * scale, -0.528f * scale) + ragdollPosition;
            Vector3F  leftShoulderJointAxis        = new Vector3F(0, -1, -1).Normalized;
            Matrix33F leftShoulderJointOrientation = new Matrix33F();
            leftShoulderJointOrientation.SetColumn(0, leftShoulderJointAxis);
            leftShoulderJointOrientation.SetColumn(1, leftShoulderJointAxis.Orthonormal1);
            leftShoulderJointOrientation.SetColumn(2, leftShoulderJointAxis.Orthonormal2);
            BallJoint leftShoulderJoint = new BallJoint
            {
                BodyA = leftUpperArm,
                BodyB = torso,
                AnchorPositionALocal = leftUpperArm.Pose.ToLocalPosition(leftShoulderJointPosition),
                AnchorPositionBLocal = torso.Pose.ToLocalPosition(leftShoulderJointPosition),
                CollisionEnabled     = false,
                ErrorReduction       = errorReduction,
                Softness             = softness,
                MaxForce             = maxForce,
            };
            simulation.Constraints.Add(leftShoulderJoint);

            Vector3F  rightShoulderJointPosition    = new Vector3F(0.193f * scale, 0.056f * scale, -0.528f * scale) + ragdollPosition;
            Vector3F  rightShoulderJointAxis        = new Vector3F(0, 1, 1).Normalized;
            Matrix33F rightShoulderJointOrientation = new Matrix33F();
            rightShoulderJointOrientation.SetColumn(0, rightShoulderJointAxis);
            rightShoulderJointOrientation.SetColumn(1, rightShoulderJointAxis.Orthonormal1);
            rightShoulderJointOrientation.SetColumn(2, rightShoulderJointAxis.Orthonormal2);
            BallJoint rightShoulderJoint = new BallJoint
            {
                BodyA = rightUpperArm,
                BodyB = torso,
                AnchorPositionALocal = rightUpperArm.Pose.ToLocalPosition(rightShoulderJointPosition),
                AnchorPositionBLocal = torso.Pose.ToLocalPosition(rightShoulderJointPosition),
                CollisionEnabled     = false,
                ErrorReduction       = errorReduction,
                Softness             = softness,
                MaxForce             = maxForce,
            };
            simulation.Constraints.Add(rightShoulderJoint);

            Vector3F  leftElbowJointPosition = new Vector3F(-0.451f * scale, 0.071f * scale, -0.538f * scale) + ragdollPosition;
            Matrix33F elbowAxisOrientation   = new Matrix33F(0, 0, -1,
                                                             0, 1, 0,
                                                             1, 0, 0);
            HingeJoint leftElbowJoint = new HingeJoint
            {
                BodyA            = leftLowerArm,
                BodyB            = leftUpperArm,
                AnchorPoseALocal = new Pose(leftLowerArm.Pose.ToLocalPosition(leftElbowJointPosition), leftLowerArm.Pose.Orientation.Inverse * elbowAxisOrientation),
                AnchorPoseBLocal = new Pose(leftUpperArm.Pose.ToLocalPosition(leftElbowJointPosition), leftUpperArm.Pose.Orientation.Inverse * elbowAxisOrientation),
                Minimum          = -2,
                Maximum          = 0,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(leftElbowJoint);

            Vector3F   rightElbowJointPosition = new Vector3F(0.451f * scale, 0.071f * scale, -0.538f * scale) + ragdollPosition;
            HingeJoint rightElbowJoint         = new HingeJoint
            {
                BodyA            = rightLowerArm,
                BodyB            = rightUpperArm,
                AnchorPoseALocal = new Pose(rightLowerArm.Pose.ToLocalPosition(rightElbowJointPosition), rightLowerArm.Pose.Orientation.Inverse * elbowAxisOrientation),
                AnchorPoseBLocal = new Pose(rightUpperArm.Pose.ToLocalPosition(rightElbowJointPosition), rightUpperArm.Pose.Orientation.Inverse * elbowAxisOrientation),
                Minimum          = 0,
                Maximum          = 2,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(rightElbowJoint);

            Vector3F   leftHipJointPosition = new Vector3F(-0.107f * scale, 0.049f * scale, 0.026f * scale) + ragdollPosition;
            HingeJoint leftHipJoint         = new HingeJoint
            {
                BodyA            = pelvis,
                BodyB            = leftUpperLeg,
                AnchorPoseALocal = new Pose(pelvis.Pose.ToLocalPosition(leftHipJointPosition)),
                AnchorPoseBLocal = new Pose(leftUpperLeg.Pose.ToLocalPosition(leftHipJointPosition), leftUpperLeg.Pose.Orientation.Inverse),
                Minimum          = -0.1f,
                Maximum          = 1.2f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(leftHipJoint);

            Vector3F   rightHipJointPosition = new Vector3F(0.107f * scale, 0.049f * scale, 0.026f * scale) + ragdollPosition;
            HingeJoint rightHipJoint         = new HingeJoint
            {
                BodyA            = pelvis,
                BodyB            = rightUpperLeg,
                AnchorPoseALocal = new Pose(pelvis.Pose.ToLocalPosition(rightHipJointPosition)),
                AnchorPoseBLocal = new Pose(rightUpperLeg.Pose.ToLocalPosition(rightHipJointPosition), rightUpperLeg.Pose.Orientation.Inverse),
                Minimum          = -0.1f,
                Maximum          = 1.2f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(rightHipJoint);

            Vector3F   leftKneeJointPosition = new Vector3F(-0.118f * scale, -0.012f * scale, 0.439f * scale) + ragdollPosition;
            HingeJoint leftKneeJoint         = new HingeJoint
            {
                BodyA            = leftLowerLeg,
                BodyB            = leftUpperLeg,
                AnchorPoseALocal = new Pose(leftLowerLeg.Pose.ToLocalPosition(leftKneeJointPosition)),
                AnchorPoseBLocal = new Pose(leftUpperLeg.Pose.ToLocalPosition(leftKneeJointPosition)),
                Minimum          = 0,
                Maximum          = 1.7f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(leftKneeJoint);

            Vector3F   rightKneeJointPosition = new Vector3F(0.118f * scale, -0.012f * scale, 0.439f * scale) + ragdollPosition;
            HingeJoint rightKneeJoint         = new HingeJoint
            {
                BodyA            = rightLowerLeg,
                BodyB            = rightUpperLeg,
                AnchorPoseALocal = new Pose(rightLowerLeg.Pose.ToLocalPosition(rightKneeJointPosition)),
                AnchorPoseBLocal = new Pose(rightUpperLeg.Pose.ToLocalPosition(rightKneeJointPosition)),
                Minimum          = 0,
                Maximum          = 1.7f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(rightKneeJoint);

            Vector3F   leftAnkleJointPosition = new Vector3F(-0.118f * scale, -0.016f * scale, 0.861f * scale) + ragdollPosition;
            HingeJoint leftAnkleJoint         = new HingeJoint
            {
                BodyA            = leftFoot,
                BodyB            = leftLowerLeg,
                AnchorPoseALocal = new Pose(leftFoot.Pose.ToLocalPosition(leftAnkleJointPosition)),
                AnchorPoseBLocal = new Pose(leftLowerLeg.Pose.ToLocalPosition(leftAnkleJointPosition), leftLowerLeg.Pose.Orientation.Inverse),
                Minimum          = -0.4f,
                Maximum          = 0.9f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(leftAnkleJoint);

            Vector3F   rightAnkleJointPosition = new Vector3F(0.118f * scale, -0.016f * scale, 0.861f * scale) + ragdollPosition;
            HingeJoint rightAnkleJoint         = new HingeJoint
            {
                BodyA            = rightFoot,
                BodyB            = rightLowerLeg,
                AnchorPoseALocal = new Pose(rightFoot.Pose.ToLocalPosition(rightAnkleJointPosition)),
                AnchorPoseBLocal = new Pose(rightLowerLeg.Pose.ToLocalPosition(rightAnkleJointPosition), rightLowerLeg.Pose.Orientation.Inverse),
                Minimum          = -0.4f,
                Maximum          = 0.9f,
                CollisionEnabled = false,
                ErrorReduction   = errorReduction,
                Softness         = softness,
                MaxForce         = maxForce,
            };
            simulation.Constraints.Add(rightAnkleJoint);
            #endregion

            #region ----- Add damping to improve stability -----

            if (addDamping)
            {
                // Damping removes jiggling and improves stability.
                softness = 0.05f;
                maxForce = float.PositiveInfinity;

                AddDamping(simulation, pelvis, torso, softness, maxForce);
                AddDamping(simulation, torso, head, softness, maxForce);
                AddDamping(simulation, torso, leftUpperArm, softness, maxForce);
                AddDamping(simulation, leftUpperArm, leftLowerArm, softness, maxForce);
                AddDamping(simulation, torso, rightUpperArm, softness, maxForce);
                AddDamping(simulation, rightUpperArm, rightLowerArm, softness, maxForce);
                AddDamping(simulation, pelvis, leftUpperLeg, softness, maxForce);
                AddDamping(simulation, pelvis, rightUpperLeg, softness, maxForce);
                AddDamping(simulation, leftUpperLeg, leftLowerLeg, softness, maxForce);
                AddDamping(simulation, rightUpperLeg, rightLowerLeg, softness, maxForce);
                AddDamping(simulation, leftLowerLeg, leftFoot, softness, maxForce);
                AddDamping(simulation, rightLowerLeg, rightFoot, softness, maxForce);
            }
            #endregion
        }
Пример #5
0
        public BridgeSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // Add basic force effects.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Add a ground plane.
            RigidBody groundPlane = new RigidBody(new PlaneShape(Vector3.UnitY, 0))
            {
                Name       = "GroundPlane", // Names are not required but helpful for debugging.
                MotionType = MotionType.Static,
            };

            Simulation.RigidBodies.Add(groundPlane);

            // We add another damping effect that acts only on the suspension bridge parts.
            // This damping uses higher damping factors than the standard damping. It makes the
            // bridge movement smoother and more stable.
            // We use a ListAreaOfEffect. So the additional damping acts only on bodies in this list.
            ListAreaOfEffect boardList = new ListAreaOfEffect(new List <RigidBody>());
            Damping          damping   = new Damping
            {
                AreaOfEffect   = boardList,
                AngularDamping = 1f,
                LinearDamping  = 0.5f
            };

            Simulation.ForceEffects.Add(damping);

            const int numberOfBoards = 20;
            BoxShape  boardShape     = new BoxShape(0.8f, 0.1f, 1.5f);
            RigidBody lastBoard      = null;

            for (int i = 0; i < numberOfBoards; i++)
            {
                // A single plank of the bridge.
                RigidBody body = new RigidBody(boardShape)
                {
                    Pose = new Pose(new Vector3(-10 + boardShape.WidthX * i, 4, 0))
                };
                Simulation.RigidBodies.Add(body);

                // Add the body to the list of the additional damping force effect.
                boardList.RigidBodies.Add(body);

                if (lastBoard != null)
                {
                    // Connect the last body with current body using a hinge.
                    HingeJoint hinge = new HingeJoint
                    {
                        BodyA = lastBoard,
                        // The attachment point is at the right side of the board.
                        // --> To define the constraint anchor orientation:
                        // The columns are the axes. We set the local z axis in the first column. This is
                        // the hinge axis. In the other two columns we set two orthonormal vectors.
                        // (All three columns are orthonormal and form a valid rotation matrix.)
                        AnchorPoseALocal = new Pose(new Vector3(boardShape.WidthX / 2, 0, 0),
                                                    new Matrix(0, 0, -1,
                                                               0, 1, 0,
                                                               1, 0, 0)),
                        BodyB = body,
                        // The attachment point is at the left side of the board.
                        // The anchor orientation is defined as above.
                        AnchorPoseBLocal = new Pose(new Vector3(-boardShape.WidthX / 2, 0, 0),
                                                    new Matrix(0, 0, -1,
                                                               0, 1, 0,
                                                               1, 0, 0)),
                        CollisionEnabled = false,
                        // ErrorReduction and Softness are tweaked to get a stable and smooth bridge
                        // movement.
                        ErrorReduction = 0.3f,
                        Softness       = 0.00005f,
                    };
                    Simulation.Constraints.Add(hinge);
                }
                else if (i == 0)
                {
                    // To attach the bridge somewhere, connect the the first board to a fixed position in the
                    // world.
                    HingeJoint hinge = new HingeJoint
                    {
                        BodyA            = Simulation.World,
                        AnchorPoseALocal = new Pose(new Vector3(-9, 3, 0),
                                                    new Matrix(0, 0, -1,
                                                               0, 1, 0,
                                                               1, 0, 0)),
                        BodyB            = body,
                        AnchorPoseBLocal = new Pose(new Vector3(-boardShape.WidthX / 2, 0, 0),
                                                    new Matrix(0, 0, -1,
                                                               0, 1, 0,
                                                               1, 0, 0)),
                    };
                    Simulation.Constraints.Add(hinge);
                }

                if (i == numberOfBoards - 1)
                {
                    // To attach the bridge somewhere, connect the the last board to a fixed position in the
                    // world.
                    HingeJoint hinge = new HingeJoint
                    {
                        BodyA            = Simulation.World,
                        AnchorPoseALocal = new Pose(new Vector3(9, 3, 0),
                                                    new Matrix(0, 0, -1,
                                                               0, 1, 0,
                                                               1, 0, 0)),
                        BodyB            = body,
                        AnchorPoseBLocal = new Pose(new Vector3(boardShape.WidthX / 2, 0, 0),
                                                    new Matrix(0, 0, -1,
                                                               0, 1, 0,
                                                               1, 0, 0)),
                    };
                    Simulation.Constraints.Add(hinge);
                }

                lastBoard = body;
            }

            // The bridge is ready.
            // Now, add some ramps so that the character controller can walk up to the bridge.
            BoxShape  rampShape = new BoxShape(10, 10, 2);
            RigidBody ramp0     = new RigidBody(rampShape)
            {
                Pose       = new Pose(new Vector3(-12.5f, -3f, 0), Matrix.CreateRotationZ(0.3f)),
                MotionType = MotionType.Static,
            };

            Simulation.RigidBodies.Add(ramp0);
            RigidBody ramp1 = new RigidBody(rampShape)
            {
                Pose       = new Pose(new Vector3(12.5f, -3f, 0), Matrix.CreateRotationZ(-0.3f)),
                MotionType = MotionType.Static,
            };

            Simulation.RigidBodies.Add(ramp1);

            // Drop a few light boxes onto the bridge.
            BoxShape  boxShape = new BoxShape(1, 1, 1);
            MassFrame boxMass  = MassFrame.FromShapeAndDensity(boxShape, Vector3.One, 100, 0.01f, 3);

            for (int i = 0; i < 10; i++)
            {
                Vector3    randomPosition    = new Vector3(RandomHelper.Random.NextFloat(-10, 10), 5, 0);
                Quaternion randomOrientation = RandomHelper.Random.NextQuaternion();
                RigidBody  body = new RigidBody(boxShape, boxMass, null)
                {
                    Pose = new Pose(randomPosition, randomOrientation),
                };
                Simulation.RigidBodies.Add(body);
            }
        }