Subscribe

RSS Feed (xml)

Powered By

Powered by Blogger

Google
 
xnahelp.blogspot.com

Kamis, 03 April 2008

Working with gamepad Devices

The Microsoft Xbox 360 wired controller works on the PC. XNA provides us with helper
classes that make it very easy to determine the state of our game pad. The template
already provided one call to the game pad helper class. This call is also in the Update
method. Let’s take a look at what is provided for us already.
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
The template is calling the built-in XNA class GamePad and calling its GetState method
passing in which player’s controller they want to check. The template then checks the
Back button on that controller to see if it has been pressed. If the controller’s Back button
has been pressed, the game exits. Now, that was pretty straightforward. To be consistent
we can use our input class to check for the condition.
Before we can do that, we need to update our interface and add the appropriate property
as well as actually getting the game pad state just like we did for our keyboard. Let’s jump
to our input handler code and do some of these things. We can start by adding a property
to get to our list of game pads in our interface:
GamePadState[] GamePads { get; }
Now we can create the member field and property to get that field like the following code:
private GamePadState[] gamePads = new GamePadState[4];
public GamePadState[] GamePads
{
get { return(gamePads); }
}
Working with Input Devices 95
5
We need to initialize each game pad state and can do that in the Update method of the
input handler object:
gamePads[0] = GamePad.GetState(PlayerIndex.One);
gamePads[1] = GamePad.GetState(PlayerIndex.Two);
gamePads[2] = GamePad.GetState(PlayerIndex.Three);
gamePads[3] = GamePad.GetState(PlayerIndex.Four);
Now, let’s remove the code that checks to see if the Back button is pressed on player one’s
game pad from our demo. We can add this code in the Update method of our
InputHandler game component to get the same effect:
if (gamePads[0].Buttons.Back == ButtonState.Pressed)
Game.Exit();
Let’s update our yaw rotation code inside of the camera game component so that we can
get the same result with our controller. We can modify our existing code that checks for
left and right to also handle input from our controller. So we modify our two conditional
statements that set the cameraYaw to also check the right thumb stick state of the game
pad we are examining:
if (input.KeyboardState.IsKeyDown(Keys.Left) ||
(input.GamePads[0].ThumbSticks.Right.X < 0))
{
cameraYaw += (spinRate * timeDelta);
}
if (input.KeyboardState.IsKeyDown(Keys.Right) ||
(input.GamePads[0].ThumbSticks.Right.X > 0))
{
cameraYaw -= (spinRate * timeDelta);
}
The thumb stick x and y axis provide a float value between -1 and 1. A value of 0 means
there is no movement. A value of -0.5 means the stick is pushed to the left halfway. A
value of 0.9 would be the stick is pushed to the right 90 percent of the way.
We did not change the keyboard code, we simply added another “or” condition to handle
our controller. We can see it is very simple, as we only needed to check the
ThumbSticks.Left property. We check the x axis of that joystick and if it is less than zero
the user is pushing the stick to the left. We check to see if it is positive (user pushing to
the right) in the second condition. We leave our cameraYaw variable to be set by our spin
rate (taking into account our time delta). At this point, regardless if players are using the
keyboard or the game pad they will get the same result from the game: The camera will
rotate around the y axis. Compile and run the program to try it out. We can also try the
game on the Xbox 360 because we have hooked up our game pad code.
At this point we know how to get access to any of the buttons (they are treated the same
way as the Back button) and either thumb stick but we have not discussed the D-pad yet.
96 CHAPTER 5 Input Devices and Cameras
The D-pad is actually treated like buttons. If we also wanted to allow the player to rotate
the camera left or right by using the D-pad, we simply need to add the following as part
of our condition:
gamePadState.DPad.Left == ButtonState.Pressed
Add that condition along with a check for the right D-Pad being pressed to our code.
Compile and run the code again and make sure that the demo allows us to look left or
right by using the keyboard, thumb stick, and D-pad. The code should now look like the
following:
if (keyboardState.IsKeyDown(Keys.Left) ||
(gamePadState.ThumbSticks.Right.X < 0) ||
(gamePadState.DPad.Left == ButtonState.Pressed))
{
cameraYaw += spinRate;
}
if (keyboardState.IsKeyDown(Keys.Right) ||
(gamePadState.ThumbSticks.Right.X > 0) ||
(gamePadState.DPad.Right == ButtonState.Pressed))
{
cameraYaw -= spinRate;
}
The shoulder buttons are just that—buttons—and we know how to handle those already.
We can determine when we press down on the left or right thumb stick because those are
also considered buttons. However, the final item we discuss regarding our game pad
controller is the triggers. Now, a good use of our triggers for this demo would be to turn
on vibration!
Before we actually determine how to use the triggers, we can look at another property of
the game pad state we have access to and that is if the controller is actually connected or
not. We can check this by getting the value of the IsConnected property.
The trigger values return a float between 0 and 1 to signify how much the trigger is
pressed (0 = not pressed; 1 = fully pressed). The Xbox 360 controller has two motors that
create its vibration. The motor on the left is a low-frequency motor whereas the motor on
the right is a high-frequency motor. We can set the values on both motors in the same
method call. We do this by calling the GamePad.SetVibration method. Because this is just
something we are doing for our demo and not really a part of the library, we will put this
code in our Game1.cs file inside the Update method:
if (input.GamePads[0].IsConnected)
{
GamePad.SetVibration(PlayerIndex.One, input.GamePads[0].Triggers.Left,
input.GamePads[0].Triggers.Right);
}
Working with Input Devices 97
5
The first thing we are doing with this new code is checking to see if the game pad is actually
connected. If it is connected then we set the vibration of both the left and right
motors based on how much the left and right triggers are being pressed. We are calling
the GamePad’s static SetVibration method. There is currently no benefit in wrapping
that into a method inside of our input handler.
We can also change the information being displayed in our window title bar to include
the left and right motor values. This can help you determine what values you should use
as you implement vibration in your games! The following is the code to accomplish that
task:
this.Window.Title = “left: “ +
input.GamePads[0].Triggers.Left.ToString() + “; right: “ +
input.GamePads[0].Triggers.Right.ToString();
Go ahead and add this debug line inside of the IsConnected condition and compile and
run the code to check the progress. We will no longer be able to see our frame rate with
this code so we could just comment out the fps object until we are ready to actually
check on our performance.

0 komentar: