none
Xbox to KeyBoard

    Question

  • I want to change this code so it can be used by a Keyboard and not the Xbox controller

    protected void UpdateInput()
    {
        // Get the game pad state.
        GamePadState currentState = GamePad.GetState(PlayerIndex.One);
        if (currentState.IsConnected)
        {
            // Rotate the model using the left thumbstick, and scale it down
            modelRotation -= currentState.ThumbSticks.Left.X * 0.10f;

            // Create some velocity if the right trigger is down.
            Vector3 modelVelocityAdd = Vector3.Zero;

            // Find out what direction we should be thrusting,
            // using rotation.
            modelVelocityAdd.X = -(float)Math.Sin(modelRotation);
            modelVelocityAdd.Z = -(float)Math.Cos(modelRotation);

            // Now scale our direction by how hard the trigger is down.
            modelVelocityAdd *= currentState.Triggers.Right;

            // Finally, add this vector to our velocity.
            modelVelocity += modelVelocityAdd;

            GamePad.SetVibration(PlayerIndex.One,
                currentState.Triggers.Right,
                currentState.Triggers.Right);


            // In case you get lost, press A to warp back to the center.
            if (currentState.Buttons.A == ButtonState.Pressed)
            {
                modelPosition = Vector3.Zero;
                modelVelocity = Vector3.Zero;
                modelRotation = 0.0f;
            }
        }
    }

    Tuesday, October 02, 2012 10:02 AM

Answers

  •   

    // Get the game pad state.


               

    GamePadStatecurrentState = GamePad.GetState(PlayerIndex.One);

               

    KeyboardStatecurrentKeyState = Keyboard.GetState();

               

                   

    // Rotate the model using the left thumbstick, and scale it down


                    modelRotation -= currentState.ThumbSticks.Left.X * 0.10f;

                   

    // Create some velocity if the right trigger is down.


                   

    Vector3modelVelocityAdd = Vector3.Zero;

                   

    // Find out what direction we should be thrusting,


                   

    // using rotation.


                   

    if(currentKeyState.IsKeyDown(Keys.A))

                        modelRotation += 0.10f;

                   

    elseif(currentKeyState.IsKeyDown(Keys.D))

                        modelRotation -= 0.10f;

                   

    else


                    modelVelocityAdd.X = -(

    float)Math.Sin(modelRotation);

                    modelVelocityAdd.Z = -(

    float)Math.Cos(modelRotation);

                   

    // Now scale our direction by how hard the trigger is down.


                    modelVelocityAdd *= currentState.Triggers.Right;

                   

    // Finally, add this vector to our velocity.


                   

    if(currentKeyState.IsKeyDown(Keys.W))

                        modelVelocityAdd *= 6;

                   

    else


                    modelVelocity += modelVelocityAdd;

                   

    GamePad.SetVibration(PlayerIndex.One,

                        currentState.Triggers.Right,

                        currentState.Triggers.Right);

                   

    //Play engine sound only when the engine is on.


                   

    if(currentState.Triggers.Right > 0 || currentKeyState.IsKeyDown(Keys.D) || currentKeyState.IsKeyDown(Keys.A))

                    {

                       

    if(soundEngineInstance.State == SoundState.Stopped)

                        {

                            soundEngineInstance.Volume = 0.75f;

                            soundEngineInstance.IsLooped =

    true;

                            soundEngineInstance.Play();

                        }

                       

    else


                            soundEngineInstance.Resume();

                    }

                   

    elseif(currentState.Triggers.Right == 0 || currentKeyState.IsKeyUp(Keys.D) || currentKeyState.IsKeyUp(Keys.A))

                    {

                       

    if(soundEngineInstance.State == SoundState.Playing)

                            soundEngineInstance.Pause();

                    }

     

                   

    if(currentState.Buttons.A == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.Enter))

                    {

                        modelPosition =

    Vector3.Zero;

                        modelVelocity =

    Vector3.Zero;

                        modelRotation = 0.0f;

                        soundHyperspaceActivation.Play();

                    }

                }

    • Marked as answer by Richie1990 Tuesday, October 02, 2012 11:10 AM
    Tuesday, October 02, 2012 11:09 AM

All replies

  •   

    // Get the game pad state.


               

    GamePadStatecurrentState = GamePad.GetState(PlayerIndex.One);

               

    KeyboardStatecurrentKeyState = Keyboard.GetState();

               

                   

    // Rotate the model using the left thumbstick, and scale it down


                    modelRotation -= currentState.ThumbSticks.Left.X * 0.10f;

                   

    // Create some velocity if the right trigger is down.


                   

    Vector3modelVelocityAdd = Vector3.Zero;

                   

    // Find out what direction we should be thrusting,


                   

    // using rotation.


                   

    if(currentKeyState.IsKeyDown(Keys.A))

                        modelRotation += 0.10f;

                   

    elseif(currentKeyState.IsKeyDown(Keys.D))

                        modelRotation -= 0.10f;

                   

    else


                    modelVelocityAdd.X = -(

    float)Math.Sin(modelRotation);

                    modelVelocityAdd.Z = -(

    float)Math.Cos(modelRotation);

                   

    // Now scale our direction by how hard the trigger is down.


                    modelVelocityAdd *= currentState.Triggers.Right;

                   

    // Finally, add this vector to our velocity.


                   

    if(currentKeyState.IsKeyDown(Keys.W))

                        modelVelocityAdd *= 6;

                   

    else


                    modelVelocity += modelVelocityAdd;

                   

    GamePad.SetVibration(PlayerIndex.One,

                        currentState.Triggers.Right,

                        currentState.Triggers.Right);

                   

    //Play engine sound only when the engine is on.


                   

    if(currentState.Triggers.Right > 0 || currentKeyState.IsKeyDown(Keys.D) || currentKeyState.IsKeyDown(Keys.A))

                    {

                       

    if(soundEngineInstance.State == SoundState.Stopped)

                        {

                            soundEngineInstance.Volume = 0.75f;

                            soundEngineInstance.IsLooped =

    true;

                            soundEngineInstance.Play();

                        }

                       

    else


                            soundEngineInstance.Resume();

                    }

                   

    elseif(currentState.Triggers.Right == 0 || currentKeyState.IsKeyUp(Keys.D) || currentKeyState.IsKeyUp(Keys.A))

                    {

                       

    if(soundEngineInstance.State == SoundState.Playing)

                            soundEngineInstance.Pause();

                    }

     

                   

    if(currentState.Buttons.A == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.Enter))

                    {

                        modelPosition =

    Vector3.Zero;

                        modelVelocity =

    Vector3.Zero;

                        modelRotation = 0.0f;

                        soundHyperspaceActivation.Play();

                    }

                }

    • Marked as answer by Richie1990 Tuesday, October 02, 2012 11:10 AM
    Tuesday, October 02, 2012 11:09 AM
  • Hi Richie1990,

      Welcome to MSDN Forum Support.

      I am glad to hear that you have solved your problem by marking your self-reply as answers.But I am not sure you really make clearly regarding your problem,so I suggest that you'd better posting your question at AppHub where you can contact Game Development experts.

      Hope it helps you.

      Sincerely,

      Jason Wang


     

     


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 03, 2012 2:21 AM
    Moderator