/// <summary>
        /// Initializes a new instance of the <see cref="CharacterControllerForceEffect"/> class.
        /// </summary>
        /// <param name="characterController">The character controller.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="characterController" /> is <see langword="null"/>.
        /// </exception>
        public CharacterControllerForceEffect(KinematicCharacterController characterController)
        {
            if (characterController == null)
            throw new ArgumentNullException("characterController");

              _cc = characterController;
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CharacterControllerForceEffect"/> class.
        /// </summary>
        /// <param name="characterController">The character controller.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="characterController" /> is <see langword="null"/>.
        /// </exception>
        public CharacterControllerForceEffect(KinematicCharacterController characterController)
        {
            if (characterController == null)
            {
                throw new ArgumentNullException("characterController");
            }

            _cc = characterController;
        }
Esempio n. 3
0
        public override void Initialize()
        {
            // Get a reference to all of the other dependent components
            camera = Game.Components.OfType<Camera>().First();
            simulation = ((GenericGame)Game).Simulation;

            //Need to differentiate between the IBinding objects as we need both of them
            //We COULD fetch them by actual type, but I'd rather get them in a generic fashion with and interface
            foreach (var binding in Game.Components.OfType<IBinding>())
            {
                if (binding is KeyboardBinding)
                    keyboardBinding = binding;
                else if (binding is MouseBinding)
                    mouseBinding = binding;
            }

            //Bind thw WASD keys to the keyboard controller with the appropriate movement actions
            //Also, lambdas are very SEXY -Nick
            keyboardBinding.Bind(Keys.W, () => { moveDirection[0].Z--; });
            keyboardBinding.Bind(Keys.S, () => { moveDirection[0].Z++; });
            keyboardBinding.Bind(Keys.A, () => { moveDirection[0].X--; });
            keyboardBinding.Bind(Keys.D, () => { moveDirection[0].X++; });

            //Same with the mouse x and y axes
            mouseBinding.Bind(MState.XAxis, () =>
            {
                var deltaTime = (float)gTime.ElapsedGameTime.TotalSeconds;

                //Compute new yaw from mouse delta x.
                float deltaYaw = 0;
                deltaYaw -= Mouse.GetState().X - MouseOrigin;
                yaw += deltaYaw * deltaTime * 0.1f;
            });

            mouseBinding.Bind(MState.YAxis, () =>
            {
                var deltaTime = (float)gTime.ElapsedGameTime.TotalSeconds;

                //Compute new pitch from mouse delta y.
                float deltaPitch = 0;
                deltaPitch -= Mouse.GetState().Y - MouseOrigin;
                pitch += deltaPitch * deltaTime * 0.1f;

                //Only reset on Y because we need BOTH x and y to evaluate before the reset happens
                Mouse.SetPosition(MouseOrigin, MouseOrigin);
            });

            CharacterController = new KinematicCharacterController(simulation)
            {
                Position = new Vector3F(-0.5F, 0, 0.5F),
                Gravity = 10
            };

            gun = Game.Content.Load<Model>("gun");

            Mouse.SetPosition(MouseOrigin, MouseOrigin);
            base.Initialize();
        }
Esempio n. 4
0
    //--------------------------------------------------------------
    #region Creation & Cleanup
    //--------------------------------------------------------------

    public CharacterControllerObject(IServiceLocator services)
    {
      Name = "CharacterController";

      _inputService = services.GetInstance<IInputService>();
      _debugRenderer = services.GetInstance<DebugRenderer>();
      _simulation = services.GetInstance<Simulation>();

      // Create character controller.
#if USE_DYNAMIC_CHARACTER_CONTROLLER
      CharacterController = new DynamicCharacterController(_simulation);
#else
      CharacterController = new KinematicCharacterController(_simulation);
#endif
      CharacterController.Enabled = false;
      CharacterController.Position = new Vector3F(0, 0, 10);
      CharacterController.Gravity = 10;   // Setting gravity to 0 switches to fly mode (instead of walking).

      // Special: No gravity and damping for character controller.
      // The character controller uses a rigid body. The gravity and damping force effects
      // should not influence this body. The character controller handles gravity itself.
      // Gravity and Damping are ForceEffects. Each force effect has an AreaOfEffect which,
      // by default, is a GlobalAreaOfEffect (= affects all bodies in the simulation). We can 
      // set a predicate method that excludes the rigid body of the character controller.
      GlobalAreaOfEffect areaOfEffect = new GlobalAreaOfEffect
      {
        Exclude = body => body == CharacterController.Body,
      };
      _simulation.ForceEffects.OfType<Gravity>().First().AreaOfEffect = areaOfEffect;
      _simulation.ForceEffects.OfType<Damping>().First().AreaOfEffect = areaOfEffect;

      // Special: Collision filtering.
      // We will have some collision objects that should not collide with the character controller.
      // We will use collision group 3 for the character and 4 for objects that should not collide
      // with it.
      CharacterController.CollisionGroup = 3;
      ICollisionFilter filter = (ICollisionFilter)_simulation.CollisionDomain.CollisionDetection.CollisionFilter;
      filter.Set(3, 4, false);  // Disable collisions between group 3 and 4.
    }