Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public override void Update(GameTime gameTime)
        {
            if (_avatarPose == null)
            {
                if (_avatarRenderer.State == AvatarRendererState.Ready)
                {
                    _avatarPose = new AvatarPose(_avatarRenderer);

                    // Create a ragdoll for the avatar.
                    _ragdoll = Ragdoll.CreateAvatarRagdoll(_avatarPose, Simulation);

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

                    // In this sample we use a passive ragdoll where we need joints to hold the
                    // limbs together and limits to control the 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);
                }
            }
            else
            {
                // Copy skeleton pose from ragdoll.
                _ragdoll.UpdateSkeletonFromBodies(_avatarPose.SkeletonPose);
            }

            // Render rigid bodies.
            _debugRenderer.Clear();
            foreach (var body in Simulation.RigidBodies)
            {
                if (!(body.Shape is EmptyShape)) // Do not draw dummy bodies which might be used by the ragdoll.
                {
                    _debugRenderer.DrawObject(body, Color.Black, true, false);
                }
            }
        }
Ejemplo n.º 3
0
        private void InitializeModelAndRagdoll()
        {
            // Load Dude model.
            var contentManager = Services.GetInstance <ContentManager>();
            var dudeModelNode  = contentManager.Load <ModelNode>("Dude/Dude");

            _meshNode           = dudeModelNode.GetSubtree().OfType <MeshNode>().First().Clone();
            _meshNode.PoseLocal = new Pose(new Vector3(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);

            // Disable sleeping.
            foreach (var body in _ragdoll.Bodies)
            {
                if (body != null)
                {
                    body.CanSleep = false;
                    //body.CollisionResponseEnabled = false;
                }
            }

            // The pelvis bone (index 1) is updated directly from the Kinect hip center.
            _ragdoll.Bodies[1].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 = 100;
                    motor.ConstraintSpring  = 0;
                }
            }
            _ragdoll.EnableMotors();

            // Add rigid bodies and the constraints of the ragdoll to the simulation.
            _ragdoll.AddToSimulation(Simulation);
        }
        public AutoRagdollShapesSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            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.AutoRecycle();
            animationController.UpdateAndApply();

            // Create a ragdoll for the Dude model.
            _ragdoll = CreateRagdoll(_meshNode);

            // 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;
                }
            }

            // Add ragdoll rigid bodies to the simulation.
            _ragdoll.AddToSimulation(Simulation);
        }
Ejemplo n.º 5
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 override void Update(GameTime gameTime)
        {
            if (_avatarPose == null)
            {
                if (_avatarRenderer.State == AvatarRendererState.Ready)
                {
                    _avatarPose = new AvatarPose(_avatarRenderer);
                    _targetPose = SkeletonPose.Create(_avatarPose.SkeletonPose.Skeleton);

                    // Create a ragdoll for the avatar.
                    _ragdoll = Ragdoll.CreateAvatarRagdoll(_avatarPose, Simulation);

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

                    // To simplify collision checks, we need a simple way to determine whether
                    // a rigid body belongs to the ragdoll.
                    // --> Set RigidBody.UserData = _ragdoll.
                    // (Alternatively we could also set specific names for the rigid bodies,
                    // or we could assign the collision objects to a certain collision group.)
                    foreach (var body in _ragdoll.Bodies)
                    {
                        if (body != null)
                        {
                            body.UserData = _ragdoll;
                        }
                    }

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

                    // Start by playing the key frame animation.
                    SwitchMode(RagdollMode.Mode1);

                    // The facial expression can be applied directly to the _avatarPose.
                    _animationController0 = AnimationService.StartAnimation(_expressionAnimation, _avatarPose);

                    // The skeletal animation is applied to the _targetPose. The _targetPose
                    // is used to drive the ragdoll. (See end of method.)
                    _animationController1 = AnimationService.StartAnimation(_skeletonAnimation, (IAnimatableProperty <SkeletonPose>)_targetPose);
                }

                return;
            }

            if (InputService.IsPressed(Buttons.A, false, LogicalPlayerIndex.One))
            {
                SwitchMode(RagdollMode.Mode1);
            }
            else if (InputService.IsPressed(Buttons.B, false, LogicalPlayerIndex.One))
            {
                SwitchMode(RagdollMode.Mode2);
            }
            else if (InputService.IsPressed(Buttons.X, false, LogicalPlayerIndex.One))
            {
                SwitchMode(RagdollMode.Mode3);
            }
            else if (InputService.IsPressed(Buttons.Y, false, LogicalPlayerIndex.One))
            {
                SwitchMode(RagdollMode.Mode4);
            }

            if (_mode == RagdollMode.Mode1 || _mode == RagdollMode.Mode2)
            {
                // The ragdoll plays a certain animation. Check whether the character was
                // hit by a ball.
                foreach (var contactConstraint in Simulation.ContactConstraints)
                {
                    if (contactConstraint.BodyA.UserData == _ragdoll && contactConstraint.BodyB.Name.StartsWith("Ball") ||
                        contactConstraint.BodyB.UserData == _ragdoll && contactConstraint.BodyA.Name.StartsWith("Ball"))
                    {
                        // Switch to the "Passive Ragdoll" mode and let the character collapse.
                        SwitchMode(RagdollMode.Mode3);

                        // Hint: You can read contactConstraint.LinearConstraintImpulse.Length to
                        // determine the strength of the impact.
                    }
                }
            }

            switch (_mode)
            {
            case RagdollMode.Mode1:
                // In mode 1 we update the rigid bodies directly.
                _ragdoll.UpdateBodiesFromSkeleton(_targetPose);
                break;

            case RagdollMode.Mode2:
                // Compute how much time the simulation will advance in the next Update().
                TimeSpan nextSimulationTimeStep;
                int      numberOfSubTimeSteps;
                Simulation.GetNextTimeStep(gameTime.ElapsedGameTime, out nextSimulationTimeStep, out numberOfSubTimeSteps);

                // In mode 2 velocity motors update the rigid bodies.
                _ragdoll.DriveToPose(_targetPose, (float)nextSimulationTimeStep.TotalSeconds);
                break;

            case RagdollMode.Mode3:
                // In mode 3 we don't have to update the rigid bodies.
                break;

            case RagdollMode.Mode4:
                // In mode 4 constraint motors control the joints of the ragdoll.
                // (The second parameter is only required for velocity motors.)
                _ragdoll.DriveToPose(_targetPose, 0);
                break;
            }

            // Copy the skeleton pose. (_avatarPose stores the skeleton pose which is
            // being rendered.)
            _ragdoll.UpdateSkeletonFromBodies(_avatarPose.SkeletonPose);

            _debugRenderer.Clear();
            _debugRenderer.DrawText("\n");
            _debugRenderer.DrawText(_statusMessage);

            // Render rigid bodies.
            foreach (var body in Simulation.RigidBodies)
            {
                if (!(body.Shape is EmptyShape)) // Do not draw dummy bodies which might be used by the ragdoll.
                {
                    _debugRenderer.DrawObject(body, Color.Black, true, false);
                }
            }
        }
        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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
            }
        }