Chapter 03 game input

Preview:

Citation preview

Game InputGame Input

Tran Minh Triet – Nguyen Khac HuyFaculty of Information TechnologyUniversity of Science, VNU-HCM

Game InputGame Input

XNA Framework supports three input sourcesXbox 360 controller

Wired controller under WindowsWireless or wired for Xbox 360Up to 4 at a time

KeyboardGood default for Windows gamesXbox 360 also supports USB keyboards

MouseWindows only (no Xbox 360 support)

Poll for inputEvery clock tick, check the state of your input devicesGenerally works OK for 1/60th second ticks

Digital vs Analog ControlsDigital vs Analog Controls

Input devices have two types of controlsDigital

Reports only two states: on or offKeyboard: keysController A, B, X, Y, Back, Start, D-Pad

AnalogReport a range of valuesXBox 360 controller: -1.0f to 1.0fMouse: mouse cursor values (in pixels)

Input Device

Digital Buttons

Analog Controls

Vibration Win? Xbox? Number

Xbox 360Controller

14 4 Yes Yes (wired)

Yes (wireless or wired)

4

Keyboard >100 0 No Yes Yes 1

Mouse 5 3 No Yes No 1

Input Type OverviewInput Type Overview

Keyboard InputKeyboard Input

Handled via the Keyboard classKeyBoard.GetState() retrieves KeyboardState

KeyboardState key methods

Keyboard InputKeyboard Input

Example: Checking the “A” key was pressed

Moving a rings

if (Keyboard.GetState().IsKeyDown(Keys.A))

// BAM!!! A is pressed!

if (Keyboard.GetState().IsKeyDown(Keys.A))

// BAM!!! A is pressed!

KeyboardState keyboardState = Keyboard.GetState();

if (keyboardState.IsKeyDown(Keys.Left))

ringsPosition.X -= ringsSpeed;

if (keyboardState.IsKeyDown(Keys.Right))

ringsPosition.X += ringsSpeed;

KeyboardState keyboardState = Keyboard.GetState();

if (keyboardState.IsKeyDown(Keys.Left))

ringsPosition.X -= ringsSpeed;

if (keyboardState.IsKeyDown(Keys.Right))

ringsPosition.X += ringsSpeed;

Mouse InputMouse Input

Providing a Mouse class to interactMouse.GetState() retrieves MouseState

MouseState important properties

Mouse InputMouse Input

Mouse InputMouse Input

Example: Moving the ring

On Update() method

MouseState prevMouseState;

MouseState mouseState = Mouse.GetState();

if(mouseState.X != prevMouseState.X ||

mouseState.Y != prevMouseState.Y)

ringsPosition = new Vector2(mouseState.X, mouseState.Y);

prevMouseState = mouseState;

MouseState prevMouseState;

MouseState mouseState = Mouse.GetState();

if(mouseState.X != prevMouseState.X ||

mouseState.Y != prevMouseState.Y)

ringsPosition = new Vector2(mouseState.X, mouseState.Y);

prevMouseState = mouseState;

Gamepad InputGamepad Input Buttons

X Y A B

Digital

OtherButtons

DPadDown Left Right Up

Gamepad InputGamepad Input

Thumb SticksLeft Right

Analogue, range -1..+1

Gamepad InputGamepad Input

Digital

ButtonsLeftShoulder

RightShoulder

Gamepad InputGamepad Input

Analogue, range 0..+1Triggers

Left Right

Gamepad Input - GamePad classGamepad Input - GamePad class

A static classDo not need to create an instance to useAll methods are static

GetStateRetrieve current state of all inputs on one controller

SetVibrationUse to make controller vibrate

GetCapabilitiesDetermine which input types are supported. Can check for voice support, and whether controller is connected.

Gamepad Input - GamePadState StructGamepad Input - GamePadState Struct

Properties for reading stateDigital Input: Buttons, DPadAnalog Input: ThumbSticks, TriggersCheck connection state: IsConnecedPacketNumber

Number increases when gamepad state changesUse to check if player has changed gamepad state since last tick

Gamepad Input - GamePadState StructGamepad Input - GamePadState Struct

public struct GamePadState{ public static bool operator !=(GamePadState left, GamePadState right); public static bool operator ==(GamePadState left, GamePadState right);

public GamePadButtons Buttons { get; } public GamePadDPad DPad { get; } public bool IsConnected { get; } public int PacketNumber { get; } public GamePadThumbSticks ThumbSticks { get; } public GamePadTriggers Triggers { get; }}

Gamepad Input - GamePadButtons StructGamepad Input - GamePadButtons Struct

Properties for retrieving current button stateA, B, X, YStart, BackLeftStick, RightStick

When you press straight down on each joystick, is a button press

LeftShoulder, RightShoulder

Possible values are given by ButtonState enumeration

Released – button is upPressed – button is down

Gamepad Input - GamePadButtons StructGamepad Input - GamePadButtons Struct

Example// create GamePadState struct GamePadState m_pad;

// retrieve current controller state m_pad = GamePad.GetState(PlayerIndex.One);

if (m_pad.Buttons.A == ButtonState.Pressed) // do something if A button pressed|

if (m_pad.Buttons.LeftStick == ButtonState.Pressed) // do something if left stick button pressed

if (m_pad.Buttons.Start == ButtonState.Pressed) // do something if start button pressed

Gamepad Input - GameDPad StructGamepad Input - GameDPad Struct

Properties for retrieving current DPad button state

Up, Down, Left, Right

Possible values are given by ButtonState enumeration

Released – button is upPressed – button is down

Gamepad Input - GameDPad StructGamepad Input - GameDPad Struct

Example// create GamePadState struct GamePadState m_pad;

// retrieve current controller state m_pad = GamePad.GetState(PlayerIndex.One);

// do something if DPad up button pressed| if (m_pad.DPad.Up == ButtonState.Pressed)

// do something if DPad left button pressed if (m_pad.DPad.Left == ButtonState.Pressed)

Gamepad Input – GamePadThumbsticks StructGamepad Input – GamePadThumbsticks Struct

Each thumbstick has X, Y positionRanges from -1.0f to 1.0f

Left (-1.0f), Right (1.0f), Up (1.0f), Down (-1.0f)0.0f indicates not being pressed at all

Represented as a Vector2

So, haveLeft.X, Left.Y, Right.X, Right.Y

Gamepad Input – GamePadThumbsticks StructGamepad Input – GamePadThumbsticks Struct

// create GamePadState structGamePadState m_pad;

// retrieve current controller statem_pad = GamePad.GetState(PlayerIndex.One);

// do something if Left joystick pressed to right|if (m_pad.Thumbsticks.Left.X > 0.0f)

// do something if Right joystick pressed down if (m_pad.Thumbsticks.Right.Y < 0.0f)

Example

Gamepad Input – GamePadTriggers StructGamepad Input – GamePadTriggers StructEach trigger ranges from 0.0f to 1.0f

Not pressed: 0.0fFully pressed: 1.0fRepresented as a float

Have left and right triggersProperties: Left, Right

// create GamePadState structGamePadState m_pad;// retrieve current controller statem_pad = GamePad.GetState(PlayerIndex.One);

if (m_pad.Triggers.Left != 0.0f) // do something if Left trigger pressed down|

if (m_pad.Triggers.Right >= 0.95f) // do something if Right trigger pressed all the way down

Gamepad Input – GamePadTriggers StructGamepad Input – GamePadTriggers Struct

Each trigger ranges from 0.0f to 1.0f Not pressed: 0.0fFully pressed: 1.0fRepresented as a float

Have left and right triggersProperties: Left, Right

Example

Gamepad Input - Controller VibrationGamepad Input - Controller Vibration

Can set the vibration level of the gamepadCall SetVibration() on GamePad classPass controller number, left vibration, right vibration

Left motor is low frequencyRight motor is high-frequency

Turn vibration full on, both motorsGamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);

Turn vibration off, both motorsGamePad.SetVibration(PlayerIndex.One, 0f, 0f);

Collision detectionCollision detectionprotected bool Collide(){ Rectangle ringsRect = new Rectangle( (int)ringsPosition.X + ringsCollisionRectOffset, (int)ringsPosition.Y + ringsCollisionRectOffset, ringsFrameSize.X - (ringsCollisionRectOffset * 2), ringsFrameSize.Y - (ringsCollisionRectOffset * 2)); Rectangle skullRect = new Rectangle( (int)skullPosition.X + skullCollisionRectOffset, (int)skullPosition.Y + skullCollisionRectOffset, skullFrameSize.X - (skullCollisionRectOffset * 2), skullFrameSize.Y - (skullCollisionRectOffset * 2)); return ringsRect.Intersects(skullRect);}

Q&AQ&A

?

ReferencesReferences

XNA framework inputhttp:// msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.aspx

Controller Imageshttp:// creators.xna.com/en-us/contentpack/controller

EbookLearning XNA 3.0 – Aaron Reed