Example #1
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);
    }
        protected override void LoadContent()
        {
            // Load model and start animation.
              _model = Game.Content.Load<Model>("Dude");
              var additionalData = (Dictionary<string, object>)_model.Tag;
              var skeleton = (Skeleton)additionalData["Skeleton"];
              _skeletonPose = SkeletonPose.Create(skeleton);
              var animations = (Dictionary<string, SkeletonKeyFrameAnimation>)additionalData["Animations"];
              var loopingAnimation = new AnimationClip<SkeletonPose>(animations.Values.First())
              {
            LoopBehavior = LoopBehavior.Cycle,
            Duration = TimeSpan.MaxValue,
              };
              AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_skeletonPose);

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

              // Set the world space pose of the whole ragdoll.
              _ragdoll.Pose = _pose;
              // And copy the bone poses of the current skeleton pose.
              _ragdoll.UpdateBodiesFromSkeleton(_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);

              base.LoadContent();
        }
Example #3
0
    public IKPhysicsSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      GraphicsScreen.DrawReticle = true;

      // Add game objects which allows to grab rigid bodies.
      _grabObject = new GrabObject(Services);
      GameObjectService.Objects.Add(_grabObject);

      // Add Dude model.
      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 initial 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);

      // Enable constraints (joints and limits, no motors)
      _ragdoll.EnableJoints();
      _ragdoll.EnableLimits();
      _ragdoll.DisableMotors();

      foreach (var body in _ragdoll.Bodies)
      {
        if (body != null)
        {
          // Disable rigid body sleeping. (If we leave it enabled, the simulation might
          // disable slow bodies before they reach their IK goal.)
          body.CanSleep = false;

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

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

      // Disable all force effects (default gravity and damping).
      Simulation.ForceEffects.Clear();

      // Create constraints which hold selected bodies at their current position 
      // relative to the world.
      // To constrain the position + orientation, we use a FixedJoint.
      foreach (var boneName in new[] { "Pelvis" })
      {
        var ragdollBody = _ragdoll.Bodies[_meshNode.SkeletonPose.Skeleton.GetIndex(boneName)];
        var ikJoint = new FixedJoint
        {
          AnchorPoseALocal = ragdollBody.Pose,
          BodyA = Simulation.World,
          AnchorPoseBLocal = Pose.Identity,
          BodyB = ragdollBody,
          CollisionEnabled = false,
          MaxForce = 1000,
        };
        _ikJoints.Add(ikJoint);
        Simulation.Constraints.Add(ikJoint);
      }
      // To constrain only the position, we use a BallJoint.
      foreach(var boneName in new[] { "L_Hand", "R_Hand", "L_Ankle1", "R_Ankle" })
      {
        var ragdollBody = _ragdoll.Bodies[_meshNode.SkeletonPose.Skeleton.GetIndex(boneName)];
        var ikJoint = new BallJoint
        {
          AnchorPositionALocal = ragdollBody.Pose.Position,
          BodyA = Simulation.World,
          AnchorPositionBLocal = Vector3F.Zero,
          BodyB = ragdollBody,
          CollisionEnabled = false,
          MaxForce = 1000,
        };
        _ikJoints.Add(ikJoint);
        Simulation.Constraints.Add(ikJoint);
      }
    }