Top Banner
Game Input Tran Minh Triet – Nguyen Khac H Faculty of Information Technology University of Science, VNU-HCM
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 03 game input

Game InputGame Input

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

Page 2: Chapter 03 game input

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

Page 3: Chapter 03 game input

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)

Page 4: Chapter 03 game input

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

Page 5: Chapter 03 game input

Keyboard InputKeyboard Input

Handled via the Keyboard classKeyBoard.GetState() retrieves KeyboardState

KeyboardState key methods

Page 6: Chapter 03 game input

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;

Page 7: Chapter 03 game input

Mouse InputMouse Input

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

MouseState important properties

Page 8: Chapter 03 game input

Mouse InputMouse Input

Page 9: Chapter 03 game 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;

Page 10: Chapter 03 game input

Gamepad InputGamepad Input Buttons

X Y A B

Digital

OtherButtons

DPadDown Left Right Up

Page 11: Chapter 03 game input

Gamepad InputGamepad Input

Thumb SticksLeft Right

Analogue, range -1..+1

Page 12: Chapter 03 game input

Gamepad InputGamepad Input

Digital

ButtonsLeftShoulder

RightShoulder

Page 13: Chapter 03 game input

Gamepad InputGamepad Input

Analogue, range 0..+1Triggers

Left Right

Page 14: Chapter 03 game input

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.

Page 15: Chapter 03 game input

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

Page 16: Chapter 03 game input

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

Page 17: Chapter 03 game input

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

Page 18: Chapter 03 game input

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

Page 19: Chapter 03 game input

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

Page 20: Chapter 03 game input

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)

Page 21: Chapter 03 game input

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

Page 22: Chapter 03 game input

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

Page 23: Chapter 03 game input

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

Page 24: Chapter 03 game input

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

Page 25: Chapter 03 game input

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);

Page 26: Chapter 03 game input

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

Page 27: Chapter 03 game input

Q&AQ&A

?

Page 28: Chapter 03 game input

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