예제 #1
0
    public PassiveRagdollSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      GraphicsScreen.DrawReticle = true;

      // Add game objects which allow to shoot balls and grab rigid bodies.
      _ballShooterObject = new BallShooterObject(Services) { Speed = 10 };
      GameObjectService.Objects.Add(_ballShooterObject);
      _grabObject = new GrabObject(Services);
      GameObjectService.Objects.Add(_grabObject);

      var modelNode = ContentManager.Load<ModelNode>("Dude/Dude");
      _meshNode = modelNode.GetSubtree().OfType<MeshNode>().First().Clone();
      _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0));
      SampleHelper.EnablePerPixelLighting(_meshNode);
      GraphicsScreen.Scene.Children.Add(_meshNode);

      // Create a ragdoll for the Dude model.
      _ragdoll = new Ragdoll();
      DudeRagdollCreator.Create(_meshNode.SkeletonPose, _ragdoll, Simulation, 0.571f);

      // Set the world space pose of the whole ragdoll. And copy the bone poses of the
      // current skeleton pose.
      _ragdoll.Pose = _meshNode.PoseWorld;
      _ragdoll.UpdateBodiesFromSkeleton(_meshNode.SkeletonPose);

      // Uncomment to disable dynamic movement (for debugging during ragdoll creation):
      //foreach (var body in _ragdoll.Bodies)
      //  if (body != null)
      //    body.MotionType = MotionType.Kinematic;

      // In this sample we use a passive ragdoll where we need joints to hold the
      // limbs together and limits to restrict angular movement.
      _ragdoll.EnableJoints();
      _ragdoll.EnableLimits();

      // Set all motors to constraint motors that only use damping. This adds a damping
      // effect to all ragdoll limbs.
      foreach (RagdollMotor motor in _ragdoll.Motors)
      {
        if (motor != null)
        {
          motor.Mode = RagdollMotorMode.Constraint;
          motor.ConstraintDamping = 5;
          motor.ConstraintSpring = 0;
        }
      }
      _ragdoll.EnableMotors();

      // Add rigid bodies and the constraints of the ragdoll to the simulation.
      _ragdoll.AddToSimulation(Simulation);

      // Add a rigid body.
      var box = new RigidBody(new BoxShape(0.4f, 0.4f, 0.4f))
      {
        Name = "Box",
        Pose = new Pose(new Vector3F(0, 3, 0)),
      };
      Simulation.RigidBodies.Add(box);
    }
예제 #2
0
        public AvatarRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            // This sample uses for a DebugRenderer for the rendering rigid bodies.
            _debugRenderer = new DebugRenderer(GraphicsService, SpriteFont);

            // Add a custom game object which controls the camera.
            _cameraObject = new CameraObject(Services);
            _cameraObject.ResetPose(new Vector3F(0, 1, -3), ConstantsF.Pi, 0);
            GameObjectService.Objects.Add(_cameraObject);

            // Add some objects which allow the user to interact with the rigid bodies.
            _grabObject        = new GrabObject(Services);
            _ballShooterObject = new BallShooterObject(Services)
            {
                Speed = 20
            };
            GameObjectService.Objects.Add(_grabObject);
            GameObjectService.Objects.Add(_ballShooterObject);

            // Add some default force effects.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Add a ground plane in the simulation.
            Simulation.RigidBodies.Add(new RigidBody(new PlaneShape(Vector3F.UnitY, 0))
            {
                MotionType = MotionType.Static
            });

            // Create a random avatar.
            _avatarDescription = AvatarDescription.CreateRandom();
            _avatarRenderer    = new AvatarRenderer(_avatarDescription);
        }
예제 #3
0
    public AvatarRagdollSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;

      // This sample uses for a DebugRenderer for the rendering rigid bodies.
      _debugRenderer = new DebugRenderer(GraphicsService, SpriteFont);

      // Add a custom game object which controls the camera.
      _cameraObject = new CameraObject(Services);
      _cameraObject.ResetPose(new Vector3F(0, 1, -3), ConstantsF.Pi, 0);
      GameObjectService.Objects.Add(_cameraObject);

      // Add some objects which allow the user to interact with the rigid bodies.
      _grabObject = new GrabObject(Services);
      _ballShooterObject = new BallShooterObject(Services) { Speed = 20 };
      GameObjectService.Objects.Add(_grabObject);
      GameObjectService.Objects.Add(_ballShooterObject);

      // Add some default force effects.
      Simulation.ForceEffects.Add(new Gravity());
      Simulation.ForceEffects.Add(new Damping());

      // Add a ground plane in the simulation.
      Simulation.RigidBodies.Add(new RigidBody(new PlaneShape(Vector3F.UnitY, 0))
      {
        MotionType = MotionType.Static
      });

      // Create a random avatar.
      _avatarDescription = AvatarDescription.CreateRandom();
      _avatarRenderer = new AvatarRenderer(_avatarDescription);
    }
예제 #4
0
        public CollisionDetectionOnlyRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.DrawReticle = true;
            SetCamera(new Vector3F(0, 1, 6), 0, 0);

            // Add a game object which allows to shoot balls.
            _ballShooterObject = new BallShooterObject(Services);
            GameObjectService.Objects.Add(_ballShooterObject);

            var modelNode = ContentManager.Load <ModelNode>("Dude/Dude");

            _meshNode           = modelNode.GetSubtree().OfType <MeshNode>().First().Clone();
            _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(ConstantsF.Pi));
            SampleHelper.EnablePerPixelLighting(_meshNode);
            GraphicsScreen.Scene.Children.Add(_meshNode);

            var animations       = _meshNode.Mesh.Animations;
            var loopingAnimation = new AnimationClip <SkeletonPose>(animations.Values.First())
            {
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };

            AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_meshNode.SkeletonPose);

            // Create a ragdoll for the Dude model.
            _ragdoll = new Ragdoll();
            DudeRagdollCreator.Create(_meshNode.SkeletonPose, _ragdoll, Simulation, 0.571f);

            // Set the world space pose of the whole ragdoll.
            _ragdoll.Pose = _meshNode.PoseWorld;
            // And copy the bone poses of the current skeleton pose.
            _ragdoll.UpdateBodiesFromSkeleton(_meshNode.SkeletonPose);

            foreach (var body in _ragdoll.Bodies)
            {
                if (body != null)
                {
                    // Set all bodies to kinematic - they should not be affected by forces.
                    body.MotionType = MotionType.Kinematic;

                    // Disable collision response.
                    body.CollisionResponseEnabled = false;
                }
            }

            // In this sample, we do not need joints, limits or motors.
            _ragdoll.DisableJoints();
            _ragdoll.DisableLimits();
            _ragdoll.DisableMotors();

            // Add ragdoll rigid bodies to the simulation.
            _ragdoll.AddToSimulation(Simulation);
        }
예제 #5
0
    public CollisionDetectionOnlyRagdollSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      GraphicsScreen.DrawReticle = true;
      SetCamera(new Vector3F(0, 1, 6), 0, 0);

      // Add a game object which allows to shoot balls.
      _ballShooterObject = new BallShooterObject(Services);
      GameObjectService.Objects.Add(_ballShooterObject);

      var modelNode = ContentManager.Load<ModelNode>("Dude/Dude");
      _meshNode = modelNode.GetSubtree().OfType<MeshNode>().First().Clone();
      _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(ConstantsF.Pi));
      SampleHelper.EnablePerPixelLighting(_meshNode);
      GraphicsScreen.Scene.Children.Add(_meshNode);

      var animations = _meshNode.Mesh.Animations;
      var loopingAnimation = new AnimationClip<SkeletonPose>(animations.Values.First())
      {
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };
      AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_meshNode.SkeletonPose);

      // Create a ragdoll for the Dude model.
      _ragdoll = new Ragdoll();
      DudeRagdollCreator.Create(_meshNode.SkeletonPose, _ragdoll, Simulation, 0.571f);

      // Set the world space pose of the whole ragdoll. 
      _ragdoll.Pose = _meshNode.PoseWorld;
      // And copy the bone poses of the current skeleton pose.
      _ragdoll.UpdateBodiesFromSkeleton(_meshNode.SkeletonPose);

      foreach (var body in _ragdoll.Bodies)
      {
        if (body != null)
        {
          // Set all bodies to kinematic - they should not be affected by forces.
          body.MotionType = MotionType.Kinematic;

          // Disable collision response.
          body.CollisionResponseEnabled = false;
        }
      }

      // In this sample, we do not need joints, limits or motors.
      _ragdoll.DisableJoints();
      _ragdoll.DisableLimits();
      _ragdoll.DisableMotors();

      // Add ragdoll rigid bodies to the simulation.
      _ragdoll.AddToSimulation(Simulation);
    }
예제 #6
0
        protected PhysicsSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.ClearBackground = true;
            GraphicsScreen.BackgroundColor = Color.White;
            GraphicsScreen.DrawReticle     = true;
            SetCamera(new Vector3F(0, 2, 10), 0, 0);

            // Add game objects which implement simple physics interactions.
            _grabObject        = new GrabObject(Services);
            _ballShooterObject = new BallShooterObject(Services);
            _explosionObject   = new ExplosionObject(Services);
            GameObjectService.Objects.Add(_grabObject);
            GameObjectService.Objects.Add(_ballShooterObject);
            GameObjectService.Objects.Add(_explosionObject);
        }
예제 #7
0
        public ActiveRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.DrawReticle = true;

            // Add game objects which allow to shoot balls and grab rigid bodies.
            _ballShooterObject = new BallShooterObject(Services)
            {
                Speed = 10
            };
            GameObjectService.Objects.Add(_ballShooterObject);
            _grabObject = new GrabObject(Services);
            GameObjectService.Objects.Add(_grabObject);

            var modelNode = ContentManager.Load <ModelNode>("Dude/Dude");

            _meshNode           = modelNode.GetSubtree().OfType <MeshNode>().First().Clone();
            _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0));
            SampleHelper.EnablePerPixelLighting(_meshNode);
            GraphicsScreen.Scene.Children.Add(_meshNode);

            // Create a copy of the dude's skeleton.
            _targetSkeletonPose = SkeletonPose.Create(_meshNode.Mesh.Skeleton);

            // Animate the _targetSkeletonPose.
            var animations       = _meshNode.Mesh.Animations;
            var loopingAnimation = new AnimationClip <SkeletonPose>(animations.Values.First())
            {
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };

            AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_targetSkeletonPose);

            // Create a ragdoll for the Dude model.
            _ragdoll = new Ragdoll();
            DudeRagdollCreator.Create(_targetSkeletonPose, _ragdoll, Simulation, 0.571f);

            // Set the world space pose of the whole ragdoll. And copy the bone poses of the
            // current skeleton pose.
            _ragdoll.Pose = _meshNode.PoseWorld;
            _ragdoll.UpdateBodiesFromSkeleton(_targetSkeletonPose);

            // In this sample we use an active ragdoll. We need joints because constraint ragdoll
            // motors only affect the body rotations.
            _ragdoll.EnableJoints();

            // We disable limits. If limits are enabled, the ragdoll could get unstable if
            // the animation tries to move a limb beyond an allowed limit. (This happens if
            // a pose in the animation violates one of our limits.)
            _ragdoll.DisableLimits();

            // Set all motors to constraint motors. Constraint motors are like springs that
            // rotate the limbs to a target position.
            foreach (RagdollMotor motor in _ragdoll.Motors)
            {
                if (motor != null)
                {
                    motor.Mode = RagdollMotorMode.Constraint;
                    motor.ConstraintDamping = 10000;
                    motor.ConstraintSpring  = 100000;
                }
            }
            _ragdoll.EnableMotors();

            // Add rigid bodies and the constraints of the ragdoll to the simulation.
            _ragdoll.AddToSimulation(Simulation);

            // Add a rigid body.
            var box = new RigidBody(new BoxShape(0.4f, 0.4f, 0.4f))
            {
                Name = "Box",
                Pose = new Pose(new Vector3F(0, 3, 0)),
            };

            Simulation.RigidBodies.Add(box);
        }
        public AdvancedAvatarRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            SampleFramework.IsMouseVisible = false;

            // This sample uses for a DebugRenderer to draw text and rigid bodies.
            _debugRenderer = new DebugRenderer(GraphicsService, SpriteFont)
            {
                DefaultColor        = Color.Black,
                DefaultTextPosition = new Vector2F(10),
            };

            // Add a custom game object which controls the camera.
            _cameraObject = new CameraObject(Services);
            _cameraObject.ResetPose(new Vector3(0, 1, -3), ConstantsF.Pi, 0);
            GameObjectService.Objects.Add(_cameraObject);

            // Add some objects which allow the user to interact with the rigid bodies.
            _grabObject        = new GrabObject(Services);
            _ballShooterObject = new BallShooterObject(Services)
            {
                Speed = 20
            };
            GameObjectService.Objects.Add(_grabObject);
            GameObjectService.Objects.Add(_ballShooterObject);

            // Add some default force effects.
            Simulation.ForceEffects.Add(new Gravity());
            Simulation.ForceEffects.Add(new Damping());

            // Create a random avatar.
            _avatarDescription = AvatarDescription.CreateRandom();
            _avatarRenderer    = new AvatarRenderer(_avatarDescription);

            // Use the "Wave" animation preset.
            var avatarAnimation = new AvatarAnimation(AvatarAnimationPreset.Wave);

            _expressionAnimation = new AnimationClip <AvatarExpression>(new WrappedAvatarExpressionAnimation(avatarAnimation))
            {
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };
            _skeletonAnimation = new AnimationClip <SkeletonPose>(new WrappedAvatarSkeletonAnimation(avatarAnimation))
            {
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };

            // Add a ground plane in the simulation.
            Simulation.RigidBodies.Add(new RigidBody(new PlaneShape(Vector3.UnitY, 0))
            {
                MotionType = MotionType.Static
            });

            // Distribute a few dynamic spheres and boxes across the landscape.
            SphereShape sphereShape = new SphereShape(0.3f);

            for (int i = 0; i < 10; i++)
            {
                Vector3 position = RandomHelper.Random.NextVector3(-10, 10);
                position.Y = 1;
                Simulation.RigidBodies.Add(new RigidBody(sphereShape)
                {
                    Pose = new Pose(position)
                });
            }

            BoxShape boxShape = new BoxShape(0.6f, 0.6f, 0.6f);

            for (int i = 0; i < 10; i++)
            {
                Vector3 position = RandomHelper.Random.NextVector3(-10, 10);
                position.Y = 1;
                Simulation.RigidBodies.Add(new RigidBody(boxShape)
                {
                    Pose = new Pose(position)
                });
            }
        }
예제 #9
0
    public AdvancedAvatarRagdollSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      SampleFramework.IsMouseVisible = false;

      // This sample uses for a DebugRenderer to draw text and rigid bodies.
      _debugRenderer = new DebugRenderer(GraphicsService, SpriteFont)
      {
        DefaultColor = Color.Black,
        DefaultTextPosition = new Vector2F(10),
      };

      // Add a custom game object which controls the camera.
      _cameraObject = new CameraObject(Services);
      _cameraObject.ResetPose(new Vector3F(0, 1, -3), ConstantsF.Pi, 0);
      GameObjectService.Objects.Add(_cameraObject);

      // Add some objects which allow the user to interact with the rigid bodies.
      _grabObject = new GrabObject(Services);
      _ballShooterObject = new BallShooterObject(Services) { Speed = 20 };
      GameObjectService.Objects.Add(_grabObject);
      GameObjectService.Objects.Add(_ballShooterObject);

      // Add some default force effects.
      Simulation.ForceEffects.Add(new Gravity());
      Simulation.ForceEffects.Add(new Damping());

      // Create a random avatar.
      _avatarDescription = AvatarDescription.CreateRandom();
      _avatarRenderer = new AvatarRenderer(_avatarDescription);

      // Use the "Wave" animation preset.
      var avatarAnimation = new AvatarAnimation(AvatarAnimationPreset.Wave);
      _expressionAnimation = new AnimationClip<AvatarExpression>(new WrappedAvatarExpressionAnimation(avatarAnimation))
      {
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };
      _skeletonAnimation = new AnimationClip<SkeletonPose>(new WrappedAvatarSkeletonAnimation(avatarAnimation))
      {
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };

      // Add a ground plane in the simulation.
      Simulation.RigidBodies.Add(new RigidBody(new PlaneShape(Vector3F.UnitY, 0)) { MotionType = MotionType.Static });

      // Distribute a few dynamic spheres and boxes across the landscape.
      SphereShape sphereShape = new SphereShape(0.3f);
      for (int i = 0; i < 10; i++)
      {
        Vector3F position = RandomHelper.Random.NextVector3F(-10, 10);
        position.Y = 1;
        Simulation.RigidBodies.Add(new RigidBody(sphereShape) { Pose = new Pose(position) });
      }

      BoxShape boxShape = new BoxShape(0.6f, 0.6f, 0.6f);
      for (int i = 0; i < 10; i++)
      {
        Vector3F position = RandomHelper.Random.NextVector3F(-10, 10);
        position.Y = 1;
        Simulation.RigidBodies.Add(new RigidBody(boxShape) { Pose = new Pose(position) });
      }
    }
        public KinematicRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.DrawReticle = true;

            // Add game objects which allow to shoot balls and grab rigid bodies.
            _ballShooterObject = new BallShooterObject(Services)
            {
                Speed = 10
            };
            GameObjectService.Objects.Add(_ballShooterObject);
            _grabObject = new GrabObject(Services);
            GameObjectService.Objects.Add(_grabObject);

            var modelNode = ContentManager.Load <ModelNode>("Dude/Dude");

            _meshNode           = modelNode.GetSubtree().OfType <MeshNode>().First().Clone();
            _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(ConstantsF.Pi));
            SampleHelper.EnablePerPixelLighting(_meshNode);
            GraphicsScreen.Scene.Children.Add(_meshNode);

            var animations       = _meshNode.Mesh.Animations;
            var loopingAnimation = new AnimationClip <SkeletonPose>(animations.Values.First())
            {
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };
            var animationController = AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_meshNode.SkeletonPose);

            animationController.UpdateAndApply();

            // Create a ragdoll for the Dude model.
            _ragdoll = new Ragdoll();
            DudeRagdollCreator.Create(_meshNode.SkeletonPose, _ragdoll, Simulation, 0.571f);

            // Set the world space pose of the whole ragdoll. And copy the bone poses of the
            // current skeleton pose.
            _ragdoll.Pose = _meshNode.PoseWorld;
            _ragdoll.UpdateBodiesFromSkeleton(_meshNode.SkeletonPose);

            // Set all bodies to kinematic -  they should not be affected by forces.
            foreach (var body in _ragdoll.Bodies)
            {
                if (body != null)
                {
                    body.MotionType = MotionType.Kinematic;
                }
            }

            // Set all motors to velocity motors. Velocity motors change RigidBody.LinearVelocity
            // RigidBody.AngularVelocity to move the rigid bodies.
            foreach (RagdollMotor motor in _ragdoll.Motors)
            {
                if (motor != null)
                {
                    motor.Mode = RagdollMotorMode.Velocity;
                }
            }
            _ragdoll.EnableMotors();

            // In this sample, we do not need joints or limits.
            _ragdoll.DisableJoints();
            _ragdoll.DisableLimits();

            // Add ragdoll rigid bodies to the simulation.
            _ragdoll.AddToSimulation(Simulation);

            // Add a rigid body.
            var box = new RigidBody(new BoxShape(0.4f, 0.4f, 0.4f))
            {
                Name = "Box",
                Pose = new Pose(new Vector3F(0, 3, 0)),
            };

            Simulation.RigidBodies.Add(box);
        }
예제 #11
0
        public PassiveRagdollSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            GraphicsScreen.DrawReticle = true;

            // Add game objects which allow to shoot balls and grab rigid bodies.
            _ballShooterObject = new BallShooterObject(Services)
            {
                Speed = 10
            };
            GameObjectService.Objects.Add(_ballShooterObject);
            _grabObject = new GrabObject(Services);
            GameObjectService.Objects.Add(_grabObject);

            var modelNode = ContentManager.Load <ModelNode>("Dude/Dude");

            _meshNode           = modelNode.GetSubtree().OfType <MeshNode>().First().Clone();
            _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0));
            SampleHelper.EnablePerPixelLighting(_meshNode);
            GraphicsScreen.Scene.Children.Add(_meshNode);

            // Create a ragdoll for the Dude model.
            _ragdoll = new Ragdoll();
            DudeRagdollCreator.Create(_meshNode.SkeletonPose, _ragdoll, Simulation, 0.571f);

            // Set the world space pose of the whole ragdoll. And copy the bone poses of the
            // current skeleton pose.
            _ragdoll.Pose = _meshNode.PoseWorld;
            _ragdoll.UpdateBodiesFromSkeleton(_meshNode.SkeletonPose);

            // Uncomment to disable dynamic movement (for debugging during ragdoll creation):
            //foreach (var body in _ragdoll.Bodies)
            //  if (body != null)
            //    body.MotionType = MotionType.Kinematic;

            // In this sample we use a passive ragdoll where we need joints to hold the
            // limbs together and limits to restrict angular movement.
            _ragdoll.EnableJoints();
            _ragdoll.EnableLimits();

            // Set all motors to constraint motors that only use damping. This adds a damping
            // effect to all ragdoll limbs.
            foreach (RagdollMotor motor in _ragdoll.Motors)
            {
                if (motor != null)
                {
                    motor.Mode = RagdollMotorMode.Constraint;
                    motor.ConstraintDamping = 5;
                    motor.ConstraintSpring  = 0;
                }
            }
            _ragdoll.EnableMotors();

            // Add rigid bodies and the constraints of the ragdoll to the simulation.
            _ragdoll.AddToSimulation(Simulation);

            // Add a rigid body.
            var box = new RigidBody(new BoxShape(0.4f, 0.4f, 0.4f))
            {
                Name = "Box",
                Pose = new Pose(new Vector3F(0, 3, 0)),
            };

            Simulation.RigidBodies.Add(box);
        }
예제 #12
0
    public KinematicRagdollSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      GraphicsScreen.DrawReticle = true;

      // Add game objects which allow to shoot balls and grab rigid bodies.
      _ballShooterObject = new BallShooterObject(Services) { Speed = 10 };
      GameObjectService.Objects.Add(_ballShooterObject);
      _grabObject = new GrabObject(Services);
      GameObjectService.Objects.Add(_grabObject);

      var modelNode = ContentManager.Load<ModelNode>("Dude/Dude");
      _meshNode = modelNode.GetSubtree().OfType<MeshNode>().First().Clone();
      _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(ConstantsF.Pi));
      SampleHelper.EnablePerPixelLighting(_meshNode);
      GraphicsScreen.Scene.Children.Add(_meshNode);

      var animations = _meshNode.Mesh.Animations;
      var loopingAnimation = new AnimationClip<SkeletonPose>(animations.Values.First())
      {
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };
      var animationController = AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_meshNode.SkeletonPose);
      animationController.UpdateAndApply();

      // Create a ragdoll for the Dude model.
      _ragdoll = new Ragdoll();
      DudeRagdollCreator.Create(_meshNode.SkeletonPose, _ragdoll, Simulation, 0.571f);

      // Set the world space pose of the whole ragdoll. And copy the bone poses of the
      // current skeleton pose.
      _ragdoll.Pose = _meshNode.PoseWorld;
      _ragdoll.UpdateBodiesFromSkeleton(_meshNode.SkeletonPose);

      // Set all bodies to kinematic -  they should not be affected by forces.
      foreach (var body in _ragdoll.Bodies)
      {
        if (body != null)
        {
          body.MotionType = MotionType.Kinematic;
        }
      }

      // Set all motors to velocity motors. Velocity motors change RigidBody.LinearVelocity
      // RigidBody.AngularVelocity to move the rigid bodies. 
      foreach (RagdollMotor motor in _ragdoll.Motors)
      {
        if (motor != null)
        {
          motor.Mode = RagdollMotorMode.Velocity;
        }
      }
      _ragdoll.EnableMotors();

      // In this sample, we do not need joints or limits.
      _ragdoll.DisableJoints();
      _ragdoll.DisableLimits();

      // Add ragdoll rigid bodies to the simulation.
      _ragdoll.AddToSimulation(Simulation);

      // Add a rigid body.
      var box = new RigidBody(new BoxShape(0.4f, 0.4f, 0.4f))
      {
        Name = "Box",
        Pose = new Pose(new Vector3F(0, 3, 0)),
      };
      Simulation.RigidBodies.Add(box);
    }
예제 #13
0
    public ActiveRagdollSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      GraphicsScreen.DrawReticle = true;

      // Add game objects which allow to shoot balls and grab rigid bodies.
      _ballShooterObject = new BallShooterObject(Services) { Speed = 10 };
      GameObjectService.Objects.Add(_ballShooterObject);
      _grabObject = new GrabObject(Services);
      GameObjectService.Objects.Add(_grabObject);

      var modelNode = ContentManager.Load<ModelNode>("Dude/Dude");
      _meshNode = modelNode.GetSubtree().OfType<MeshNode>().First().Clone();
      _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0));
      SampleHelper.EnablePerPixelLighting(_meshNode);
      GraphicsScreen.Scene.Children.Add(_meshNode);

      // Create a copy of the dude's skeleton.
      _targetSkeletonPose = SkeletonPose.Create(_meshNode.Mesh.Skeleton);

      // Animate the _targetSkeletonPose.
      var animations = _meshNode.Mesh.Animations;
      var loopingAnimation = new AnimationClip<SkeletonPose>(animations.Values.First())
      {
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };
      AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_targetSkeletonPose);

      // Create a ragdoll for the Dude model.
      _ragdoll = new Ragdoll();
      DudeRagdollCreator.Create(_targetSkeletonPose, _ragdoll, Simulation, 0.571f);

      // Set the world space pose of the whole ragdoll. And copy the bone poses of the
      // current skeleton pose.
      _ragdoll.Pose = _meshNode.PoseWorld;
      _ragdoll.UpdateBodiesFromSkeleton(_targetSkeletonPose);

      // In this sample we use an active ragdoll. We need joints because constraint ragdoll
      // motors only affect the body rotations.
      _ragdoll.EnableJoints();

      // We disable limits. If limits are enabled, the ragdoll could get unstable if 
      // the animation tries to move a limb beyond an allowed limit. (This happens if
      // a pose in the animation violates one of our limits.)
      _ragdoll.DisableLimits();

      // Set all motors to constraint motors. Constraint motors are like springs that
      // rotate the limbs to a target position.
      foreach (RagdollMotor motor in _ragdoll.Motors)
      {
        if (motor != null)
        {
          motor.Mode = RagdollMotorMode.Constraint;
          motor.ConstraintDamping = 10000;
          motor.ConstraintSpring = 100000;
        }
      }
      _ragdoll.EnableMotors();

      // Add rigid bodies and the constraints of the ragdoll to the simulation.
      _ragdoll.AddToSimulation(Simulation);

      // Add a rigid body.
      var box = new RigidBody(new BoxShape(0.4f, 0.4f, 0.4f))
      {
        Name = "Box",
        Pose = new Pose(new Vector3F(0, 3, 0)),
      };
      Simulation.RigidBodies.Add(box);
    }