Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead February 12, 2008 Creative Commons Attribution.

Slides:



Advertisements
Similar presentations
Instruction: Use mouses left button. Use mouses left button. If mouse buttons dont work, use arrow keys of keyboard for slide show. If mouse buttons dont.
Advertisements

Instruction: Use mouses left button. Use mouses left button. If mouse buttons dont work, use arrow keys of keyboard for slide show. If mouse buttons dont.
DEVELOPING ICT SKILLS PART -TWO
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
IR Control Materials taken from a variety of sources including IR Remote for the Boe-Bot by Andy Lindsay.
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
3.3. G AME I NPUT Handling input within games. In lecture exploration of answers to frequently asked student questions.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Input.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Imagine Cup Team Formation XNA Input January 14, 2010 Arnav Jhala Adapted from.
Horizontal Scrolling through Tileset Levels Game Design Experience Professor Jim Whitehead March 4, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Introduction to C# Properties, Arrays, Loops, Lists Game Design Experience Professor Jim Whitehead January 28, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
2D Graphics in XNA Game Studio Express (Modeling a Class in UML) Game Design Experience Professor Jim Whitehead February 5, 2008 Creative Commons Attribution.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead January 21, 2009 Creative Commons Attribution.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0/ Creating a Platformer in Game Maker Foundations of Interactive Game Design Prof.
More Game GUI and methods. This is where we left off.
Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.
IR SENSORS AND ENCODERS. LCDs Timothy Friez Class # 2.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0 Key Abstractions in Game Maker Foundations of Interactive Game Design Prof. Jim Whitehead.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.
Technology Transfer: Haptic Feedback Explain technology transfer and technological innovation.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 6 Decisions and Conditions.
Developing the Game User Interface (UI) Lesson 5.
1 Useful Tools for Making Video Games Part V An overview of.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
The mouse What is a mouse.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
11 Using the Keyboard Session Session Overview  Introduce the keyboard device  Show how keys on a keyboard can be represented by enumerated types.
FLTK. Objectives Install and Use FLTK Widgets ◦Callbacks Handling event ◦System events ◦Mouse events ◦Keyboard events.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
Setting Up Your CPS Receiver and Student Responders.
11 Getting Player Input Using a Gamepad Session 3.1.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
Haptic & Direct User Input with DirectInput ® 8 API Graphics Lab. Korea Univ.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
CHAPTER 6 Multiplayer XNA Game Studio 4.0. Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and.
 successfully connected electricity and magnetism  aligned a straight wire with a compass pointing to Earth’s magnetic north  when current is present,
XP 1 ﴀ New Perspectives on Microsoft Office 2003, Premium Edition Excel Tutorial 2 Microsoft Office Excel 2003 Tutorial 2 – Working With Formulas and Functions.
Programming with LabVIEW Intro to programming and.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Chapter 2 HAEDWAER.
XNA ● Proprietary Microsoft framework ● C#. Interface.
11 Adding a Bread Bat Session Session Overview  We have created a cheese sprite that bounces around the display  We now need to create a bread.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
11 Adding Vibration Effects Session Session Overview  Describe how the vibration feature of the gamepad works  Show how an XNA program can control.
1 Module 1 Computer Basics A Module of the CYC Course Computer Basics.
CMPS 20: Game Design Experience January 14, 2010 Arnav Jhala.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
Programming Applied Sensors in FIRST Robots Chris Elston – Team Download sample code:
By Joshua Shaw.  We have previously added a While Loop  We are now going to use it for programming the robot  We have also added two motor programs.
CS 134 Alternate Inputs.
EE 200 Design Tools Laboratory 14
Lecture 2 Richard Gesick
Introduction to Classes and Objects
Creating a Multi-Player Game
MINDSENSORS PSP-Nx Controller for LEGO® MINDSTORMS®
10. User Input.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Introduction to Events
Programming a Servo By Joshua Shaw.
Game Controller Lesson Two.
How to organize and document your classes
Nate Brunelle Today: Conditional Decision Statements
Game Programming Algorithms and Techniques
Presentation transcript:

Input from Controller and Keyboard in XNA Game Studio Express Game Design Experience Professor Jim Whitehead February 12, 2008 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0

Announcements Weekly help session for CS 20 (C# and XNA Game Studio Express) ► Thursday, 4:30-7pm ► Engineering 2, room 399 (third floor, by elevators) ► Exchange ideas, tips, pointers ► Get assistance from TAs

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/60 th 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 Input Device Digital Buttons Analog Controls VibrationWin?Xbox?Number Xbox 360 Controller 144YesYes (wired) Yes (wireless or wired) 4 Keyboard>1000NoYes 1 Mouse53NoYesNo1

Xbox 360 Controller Input Every clock tick ► Get state of controller Call GetState() on GamePad class Pass in PlayerIndex –PlayerIndex.One, PlayerIndex.Two, … –Corresponds to lit region in “ring of light” Returns a GamePadState structure ► Check if controller is connected IsConnected boolean in GamePadState ► Check GamePadState for current state of digital and analog inputs ► Recall that update() is called every clock tick Get input in update(), or a method called from it

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. public static class GamePad { public static GamePadCapabilities GetCapabilities(PlayerIndex playerIndex); public static GamePadState GetState(PlayerIndex playerIndex); public static GamePadState GetState(PlayerIndex playerIndex, GamePadDeadZone deadZoneMode); public static bool SetVibration(PlayerIndex playerIndex, float leftMotor, float rightMotor); }

C# Structs A struct in C# is a lightweight alternative to a class Similar to class ► Can have constructors, properties, methods, fields, operators, nested types, indexers Different from class ► Struct does not support inheritance, or destructors ► Is a value type (classes are reference types) Rule of thumb: ► Use structs for types that are small, simple, similar in behavior to built-in types

GamePadState Struct Properties for reading state of the GamePad ► 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 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; } }

GamePadButtons Struct (Buttons) 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 GamePadState m_pad; // create GamePadState struct m_pad = GamePad.GetState(PlayerIndex.One); // retrieve current controller state 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

GameDPad Struct (DPad) 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 GamePadState m_pad; // create GamePadState struct m_pad = GamePad.GetState(PlayerIndex.One); // retrieve current controller state if (m_pad.DPad.Up == ButtonState.Pressed) // do something if DPad up button pressed| if (m_pad.DPad.Left == ButtonState.Pressed) // do something if DPad left button pressed

GamePadThumbsticks Struct (Thumbsticks) 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 GamePadState m_pad; // create GamePadState struct m_pad = GamePad.GetState(PlayerIndex.One); // retrieve current controller state if (m_pad.Thumbsticks.Left.X > 0.0f) // do something if Left joystick pressed to right| if (m_pad.Thumbsticks.Right.Y < 0.0f) // do something if Right joystick pressed down

Joystick Dead Zone Joysticks typically have tiny deflection to left/right or up/down ► Leads to “drift” if uncompensated Dead-zone ► Region around 0.0f that is interpreted as not-moving ► Allows joysticks to have a small amount of deflection without leading to drift Three ways to handle this ► From GamePadDeadZone enumeration ► IndependentAxes: X & Y each have separate dead zone (default) ► Circular: X & Y combined before processing dead zone ► None: No processing, application must determine

GamePadTriggers Struct (Triggers) 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 Go to controller demonstration from book GamePadState m_pad; // create GamePadState struct m_pad = GamePad.GetState(PlayerIndex.One); // retrieve current controller state 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

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

Keyboard Input Every clock tick, poll state of keyboard ► Call GetState() on Keyboard class KeyboardState keyState = Keyboard.GetState() Keyboard is a static class ► Check if a specific key is pressed if (keyState.IsKeyDown(Keys.keyname)) … Also, IsKeyUp(Keys.keyname) Keys is an enumeration of keynames ► Also, GetPressedKeys() Returns array of keys currently pressed If length of array is zero, no keys currently pressed Show keyboard input demo from text

Mouse Input Every clock tick, poll state of the mouse ► Call GetState on Mouse class ► MouseState mouseState = Mouse.GetState(); ► Mouse is a static class MouseState contains a series of properties ► X, Y : position of mouse (int) ► LeftButton, MiddleButton, RightButton, XButton1, XButton2 Either Released or Pressed (ButtonState enumeration) ► ScrollWheelValue (int) Scroll wheel represents cumulative change over lifetime of the game Show mouse input example from text

Wrapper Class What if you want to use the controller, if present, and the keyboard if not? Create an input wrapper class ► Checks both controller and keyboard input ► Has a series of properties to set/get current direction state Example: ► If controller connected AND controller DPad up arrow pressed Set wrapper’s up property to true ► Else if keyboard Up key pressed Set wrapper’s up property to true ► Else Set wrapper’s up property to false Rest of application checks input wrapper class up property