SlideShare a Scribd company logo
Game Input Tran Minh Triet – Nguyen KhacHuy Faculty of Information Technology University of Science, VNU-HCM
Game Input XNA Framework supports three input sources Xbox 360 controller Wired controller under Windows Wireless or wired for Xbox 360 Up to 4 at a time Keyboard Good default for Windows games Xbox 360 also supports USB keyboards Mouse Windows only (no Xbox 360 support) Poll for input Every clock tick, check the state of your input devices Generally works OK for 1/60th second ticks
Digital vs Analog Controls Input devices have two types of controls Digital Reports only two states: on or off Keyboard: keys Controller A, B, X, Y, Back, Start, D-Pad Analog Report a range of values XBox 360 controller: -1.0f to 1.0f Mouse: mouse cursor values (in pixels)
Input Type Overview
Keyboard Input Handled via the Keyboard class KeyBoard.GetState() retrieves KeyboardState KeyboardState key methods
Keyboard Input Example: Checking the “A” key was pressed Moving a rings 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;
Mouse Input Providing a Mouse class to interact Mouse.GetState() retrieves MouseState MouseState important properties
Mouse Input
Mouse 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;
Gamepad Input Other Buttons Buttons X Y A B Digital DPad Down Left Right Up
Gamepad Input Thumb Sticks Left Right Analogue, range -1..+1
Gamepad Input Buttons LeftShoulder RightShoulder Digital
Gamepad Input Triggers Left Right Analogue, range 0..+1
Gamepad Input - GamePad class A static class Do not need to create an instance to use All methods are static GetState Retrieve current state of all inputs on one controller SetVibration Use to make controller vibrate GetCapabilities Determine which input types are supported.  Can check for voice support, and whether controller is connected.
Gamepad Input - GamePadState Struct Properties for reading state Digital Input: Buttons, DPad Analog Input: ThumbSticks, Triggers Check connection state: IsConneced PacketNumber Number increases when gamepad state changes Use to check if player has changed gamepad state since last tick
Gamepad Input - GamePadState Struct publicstruct 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 Struct Properties for retrieving current button state A, B, X, Y Start, Back LeftStick, 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 up Pressed – button is down
Gamepad 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 Struct Properties for retrieving current DPad button state Up, Down, Left, Right Possible values are given by ButtonState enumeration Released – button is up Pressed – button is down
Gamepad 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 Struct Each thumbstick has X, Y position Ranges 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, have Left.X, Left.Y, Right.X, Right.Y
Gamepad Input – GamePadThumbsticks Struct Example 	 // create GamePadState struct 	GamePadState 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)
Gamepad Input – GamePadTriggersStruct Each trigger ranges from 0.0f to 1.0f  Not pressed: 0.0f Fully pressed: 1.0f Represented as a float Have left and right triggers Properties: Left, Right // create GamePadState struct GamePadState 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 – GamePadTriggersStruct Example Each trigger ranges from 0.0f to 1.0f  Not pressed: 0.0f Fully pressed: 1.0f Represented as a float Have left and right triggers Properties: Left, Right
Gamepad Input - Controller Vibration Can set the vibration level of the gamepad Call SetVibration() on GamePad class Pass controller number, left vibration, right vibration Left motor is low frequency Right motor is high-frequency Turn vibration full on, both motors GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f); Turn vibration off, both motors GamePad.SetVibration(PlayerIndex.One, 0f, 0f);
Collision detection protected 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&A ?
References XNA framework input http:// msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.aspx Controller Images http:// creators.xna.com/en-us/contentpack/controller Ebook Learning XNA 3.0 – Aaron Reed

More Related Content

Viewers also liked

Policiamento Ostensivo de Guardas - Complexo da Bahia
Policiamento Ostensivo de Guardas - Complexo da BahiaPoliciamento Ostensivo de Guardas - Complexo da Bahia
Policiamento Ostensivo de Guardas - Complexo da Bahia
Alisson Soares
 
EEX 4070 Action project
EEX 4070 Action projectEEX 4070 Action project
EEX 4070 Action project
jennifer marietta
 
Produktberater für Fernsehgeräte des SmartShoppingBlog.de
Produktberater für Fernsehgeräte des SmartShoppingBlog.deProduktberater für Fernsehgeräte des SmartShoppingBlog.de
Produktberater für Fernsehgeräte des SmartShoppingBlog.deUnited Internet Media AG
 
5 fitness quotes that irritate midlife exercisers
5 fitness quotes that irritate midlife exercisers5 fitness quotes that irritate midlife exercisers
5 fitness quotes that irritate midlife exercisers
Kymberly Williams-Evans, MA
 
Produktberater für Digitalkameras des SmartShoppingBlog.de
Produktberater für Digitalkameras des SmartShoppingBlog.deProduktberater für Digitalkameras des SmartShoppingBlog.de
Produktberater für Digitalkameras des SmartShoppingBlog.deUnited Internet Media AG
 
Manual Ilustrado do Livro de Parte Digital
Manual Ilustrado do Livro de Parte DigitalManual Ilustrado do Livro de Parte Digital
Manual Ilustrado do Livro de Parte Digital
Alisson Soares
 
Sound Effects and Audio
 Sound Effects and Audio Sound Effects and Audio
Sound Effects and Audio
boybuon205
 
Chapter 02 sprite and texture
Chapter 02 sprite and textureChapter 02 sprite and texture
Chapter 02 sprite and textureboybuon205
 
Create a media kit that gets attention
Create a media kit that gets attentionCreate a media kit that gets attention
Create a media kit that gets attention
Kymberly Williams-Evans, MA
 
Integrate function and cognitive challenges into your older adult fitness groups
Integrate function and cognitive challenges into your older adult fitness groupsIntegrate function and cognitive challenges into your older adult fitness groups
Integrate function and cognitive challenges into your older adult fitness groups
Kymberly Williams-Evans, MA
 

Viewers also liked (15)

Policiamento Ostensivo de Guardas - Complexo da Bahia
Policiamento Ostensivo de Guardas - Complexo da BahiaPoliciamento Ostensivo de Guardas - Complexo da Bahia
Policiamento Ostensivo de Guardas - Complexo da Bahia
 
Overview
OverviewOverview
Overview
 
EEX 4070 Action project
EEX 4070 Action projectEEX 4070 Action project
EEX 4070 Action project
 
Produktberater für Fernsehgeräte des SmartShoppingBlog.de
Produktberater für Fernsehgeräte des SmartShoppingBlog.deProduktberater für Fernsehgeräte des SmartShoppingBlog.de
Produktberater für Fernsehgeräte des SmartShoppingBlog.de
 
5 fitness quotes that irritate midlife exercisers
5 fitness quotes that irritate midlife exercisers5 fitness quotes that irritate midlife exercisers
5 fitness quotes that irritate midlife exercisers
 
Produktberater für Digitalkameras des SmartShoppingBlog.de
Produktberater für Digitalkameras des SmartShoppingBlog.deProduktberater für Digitalkameras des SmartShoppingBlog.de
Produktberater für Digitalkameras des SmartShoppingBlog.de
 
Manual Ilustrado do Livro de Parte Digital
Manual Ilustrado do Livro de Parte DigitalManual Ilustrado do Livro de Parte Digital
Manual Ilustrado do Livro de Parte Digital
 
Network
NetworkNetwork
Network
 
Sound Effects and Audio
 Sound Effects and Audio Sound Effects and Audio
Sound Effects and Audio
 
Wk 2010thenders
Wk 2010thendersWk 2010thenders
Wk 2010thenders
 
Chapter 02 sprite and texture
Chapter 02 sprite and textureChapter 02 sprite and texture
Chapter 02 sprite and texture
 
Maps
MapsMaps
Maps
 
Un
UnUn
Un
 
Create a media kit that gets attention
Create a media kit that gets attentionCreate a media kit that gets attention
Create a media kit that gets attention
 
Integrate function and cognitive challenges into your older adult fitness groups
Integrate function and cognitive challenges into your older adult fitness groupsIntegrate function and cognitive challenges into your older adult fitness groups
Integrate function and cognitive challenges into your older adult fitness groups
 

Similar to Chapter 03 game input

java code please Add event handlers to the buttons in your TicTacToe.pdf
java code please Add event handlers to the buttons in your TicTacToe.pdfjava code please Add event handlers to the buttons in your TicTacToe.pdf
java code please Add event handlers to the buttons in your TicTacToe.pdf
ezzi97
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
kavithaarp
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfHello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdf
jyothimuppasani1
 
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdfimport tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
preetajain
 
grahics
grahicsgrahics
grahics
Hardeep Kaur
 
Programming a guide gui
Programming a guide guiProgramming a guide gui
Programming a guide guiMahmoud Hikmet
 
Please help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdfPlease help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdf
manjan6
 
The following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfThe following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdf
arihantsherwani
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
hainesburchett26321
 
Html5 game, websocket e arduino
Html5 game, websocket e arduino Html5 game, websocket e arduino
Html5 game, websocket e arduino Giuseppe Modarelli
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
FashionColZone
 
Tetris_game_presentation_for_github.pptx
Tetris_game_presentation_for_github.pptxTetris_game_presentation_for_github.pptx
Tetris_game_presentation_for_github.pptx
LiudmilaShurupova
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
apexjaipur
 
Html5 game, websocket e arduino
Html5 game, websocket e arduinoHtml5 game, websocket e arduino
Html5 game, websocket e arduino
monksoftwareit
 
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdfWrite Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
sales98
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
Mahyuddin8
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
info430661
 

Similar to Chapter 03 game input (20)

java code please Add event handlers to the buttons in your TicTacToe.pdf
java code please Add event handlers to the buttons in your TicTacToe.pdfjava code please Add event handlers to the buttons in your TicTacToe.pdf
java code please Add event handlers to the buttons in your TicTacToe.pdf
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdfHello!This is Java assignment applet.Can someone help me writing.pdf
Hello!This is Java assignment applet.Can someone help me writing.pdf
 
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdfimport tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
import tio.;class TicTacToe {static final int EMPTY = 0;stati.pdf
 
grahics
grahicsgrahics
grahics
 
Programming a guide gui
Programming a guide guiProgramming a guide gui
Programming a guide gui
 
Please help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdfPlease help with this. program must be written in C# .. All of the g.pdf
Please help with this. program must be written in C# .. All of the g.pdf
 
The following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfThe following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdf
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
 
Html5 game, websocket e arduino
Html5 game, websocket e arduino Html5 game, websocket e arduino
Html5 game, websocket e arduino
 
You will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
 
Tetris_game_presentation_for_github.pptx
Tetris_game_presentation_for_github.pptxTetris_game_presentation_for_github.pptx
Tetris_game_presentation_for_github.pptx
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
 
Of class2
Of class2Of class2
Of class2
 
Html5 game, websocket e arduino
Html5 game, websocket e arduinoHtml5 game, websocket e arduino
Html5 game, websocket e arduino
 
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdfWrite Java FX code for this pseudocode of the void initilizaHistoryL.pdf
Write Java FX code for this pseudocode of the void initilizaHistoryL.pdf
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
package com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdfpackage com.tictactoe; public class Main {public void play() {.pdf
package com.tictactoe; public class Main {public void play() {.pdf
 

Chapter 03 game input

  • 1. Game Input Tran Minh Triet – Nguyen KhacHuy Faculty of Information Technology University of Science, VNU-HCM
  • 2. Game Input XNA Framework supports three input sources Xbox 360 controller Wired controller under Windows Wireless or wired for Xbox 360 Up to 4 at a time Keyboard Good default for Windows games Xbox 360 also supports USB keyboards Mouse Windows only (no Xbox 360 support) Poll for input Every clock tick, check the state of your input devices Generally works OK for 1/60th second ticks
  • 3. Digital vs Analog Controls Input devices have two types of controls Digital Reports only two states: on or off Keyboard: keys Controller A, B, X, Y, Back, Start, D-Pad Analog Report a range of values XBox 360 controller: -1.0f to 1.0f Mouse: mouse cursor values (in pixels)
  • 5. Keyboard Input Handled via the Keyboard class KeyBoard.GetState() retrieves KeyboardState KeyboardState key methods
  • 6. Keyboard Input Example: Checking the “A” key was pressed Moving a rings 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;
  • 7. Mouse Input Providing a Mouse class to interact Mouse.GetState() retrieves MouseState MouseState important properties
  • 9. Mouse 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;
  • 10. Gamepad Input Other Buttons Buttons X Y A B Digital DPad Down Left Right Up
  • 11. Gamepad Input Thumb Sticks Left Right Analogue, range -1..+1
  • 12. Gamepad Input Buttons LeftShoulder RightShoulder Digital
  • 13. Gamepad Input Triggers Left Right Analogue, range 0..+1
  • 14. Gamepad Input - GamePad class A static class Do not need to create an instance to use All methods are static GetState Retrieve current state of all inputs on one controller SetVibration Use to make controller vibrate GetCapabilities Determine which input types are supported. Can check for voice support, and whether controller is connected.
  • 15. Gamepad Input - GamePadState Struct Properties for reading state Digital Input: Buttons, DPad Analog Input: ThumbSticks, Triggers Check connection state: IsConneced PacketNumber Number increases when gamepad state changes Use to check if player has changed gamepad state since last tick
  • 16. Gamepad Input - GamePadState Struct publicstruct 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; }}
  • 17. Gamepad Input - GamePadButtons Struct Properties for retrieving current button state A, B, X, Y Start, Back LeftStick, 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 up Pressed – button is down
  • 18. Gamepad 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
  • 19. Gamepad Input - GameDPad Struct Properties for retrieving current DPad button state Up, Down, Left, Right Possible values are given by ButtonState enumeration Released – button is up Pressed – button is down
  • 20. Gamepad 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)
  • 21. Gamepad Input – GamePadThumbsticks Struct Each thumbstick has X, Y position Ranges 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, have Left.X, Left.Y, Right.X, Right.Y
  • 22. Gamepad Input – GamePadThumbsticks Struct Example // create GamePadState struct GamePadState 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)
  • 23. Gamepad Input – GamePadTriggersStruct Each trigger ranges from 0.0f to 1.0f Not pressed: 0.0f Fully pressed: 1.0f Represented as a float Have left and right triggers Properties: Left, Right // create GamePadState struct GamePadState 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
  • 24. Gamepad Input – GamePadTriggersStruct Example Each trigger ranges from 0.0f to 1.0f Not pressed: 0.0f Fully pressed: 1.0f Represented as a float Have left and right triggers Properties: Left, Right
  • 25. Gamepad Input - Controller Vibration Can set the vibration level of the gamepad Call SetVibration() on GamePad class Pass controller number, left vibration, right vibration Left motor is low frequency Right motor is high-frequency Turn vibration full on, both motors GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f); Turn vibration off, both motors GamePad.SetVibration(PlayerIndex.One, 0f, 0f);
  • 26. Collision detection protected 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); }
  • 27. Q&A ?
  • 28. References XNA framework input http:// msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.aspx Controller Images http:// creators.xna.com/en-us/contentpack/controller Ebook Learning XNA 3.0 – Aaron Reed

Editor's Notes

  1. Control the objects on the screen and give your application some user interaction. Mouse input is never available on the Xbox360,andThe Zune only supports its emulated controls—you won’t be plugginga mouse, a keyboard, or any other input devices into a Zune anytime soon.
  2. Using the Microsoft.XNA.Framework.Input namespace
  3. XButton1, XButton2:Returns the state of additional buttons on some mice.
  4. Microsoft Permissive License (Ms-PL) This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software. 1. DefinitionsThe terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the same meaning here as under U.S. copyright law.A “contribution” is the original software, or any additions or changes to the software.A “contributor” is any person that distributes its contribution under this license. “Licensed patents” are a contributor’s patent claims that read directly on its contribution. 2. Grant of Rights(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software. 3. Conditions and Limitations (A) No Trademark License- This license does not grant you rights to use any contributors’ name, logo, or trademarks.(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.(E) The software is licensed “as-is.” You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
  5. If you’re developing a game for Windows, you can still program for an Xbox 360 controller.You’ll have to have a wired controller, or you can purchase an Xbox 360 Wireless Receiver for around $20, which will allow you to connect up to four wireless controllers to a PC.
  6. Ý tưởng đơn giản là xác định khung chữ nhật của 2 sprite và dùng phương thức Intersects để kiểm tra có đụng độ hay không