Presentation is loading. Please wait.

Presentation is loading. Please wait.

Game Programming Algorithms and Techniques

Similar presentations


Presentation on theme: "Game Programming Algorithms and Techniques"— Presentation transcript:

1 Game Programming Algorithms and Techniques
Chapter 5 Input

2 Chapter 5 Objectives Input Devices
Learn the difference between digital and analog devices Using analog filtering to reduce spurious input Event-Based Input Systems Basic principles of event systems Two different implementations of an input system for a game Mobile Input Learn about touch screens and gestures Understand when to use the different types of sensors on mobile devices

3 A variety of input devices
Lots of different types of input devices are used for games: A variety of input devices

4 Digital and Analog Devices
A digital input device can be either on or off: Keyboard button Mouse button Face button on controllers (usually) An analog input device has a range of values: Joysticks Accelerometer

5 Keyboard Input Basics We can't use stdio for keyboard input on most games. Instead, libraries keep an array of the state of all buttons for the keyboard. We can use keycodes to then figure out which index in the array to look up for a particular key, such as K_SPACE.

6 Tracking Key State Changes
We don't want to just do this: if IsKeyDown(K_SPACE) fire missile end When the player presses the spacebar, it's likely to be active for several frames. Furthermore, if the spacebar is held, a missile will be fired every single frame.

7 Tracking Key State Changes, Cont'd
Instead, we should track the state for this frame and the last frame: This way, if we map something to "just pressed," it will only fire once per key press. Last State Current State Result False Still released True Just pressed Just released Still pressed

8 Key State Implementation
enum KeyState StillReleased, JustPressed, JustReleased, StillPressed end function UpdateKeyboard() // lastState and currentState are arrays // which store the entire state of the keyboard. lastState = currentState currentState = get keyboard state

9 Key State Implementation, Cont'd
function GetKeyState(int keyCode) if lastState[keyCode] == true if currentState[keyCode] == true return StillPressed else return JustReleased end return JustPressed return StillReleased

10 Analog Input Because analog devices give a range of values, we have to be mindful of spurious inputs. For example, a joystick with a range of values for x/y (-32k to +32k) will rarely be exactly (0, 0). Analog filtering tries to eliminate spurious input data.

11 Joystick dead zone causes input within the inner circle to be ignored.
We want to ignore input inside the inner circle, and then scale the remaining range from 0-100%. Joystick dead zone causes input within the inner circle to be ignored.

12 Simple Dead Zone (Doesn't Work)
int deadZone = 3000 // (10%) Vector2 joy = get joystick input if joy.x >= -deadZone && joy.x <= deadZone joy.x = 0 end if joy.y >= -deadZone && joy.y <= deadZone joy.y = 0 Problems: Dead zone is a square. When outside of the dead zone, will suddenly get 10% value.

13 Better Dead Zone float deadZone = 3000 float maxValue = Vector2 joy = get joystick input float length = joy.length() // If the length < deadZone, we want no input if length < deadzone joy.x = 0 joy.y = 0 else // Calculate the percent between the deadZone and // maxValue circles float pct = (length – deadZone) / (maxValue – deadZone) // Normalize vector and multiply to get correct final // value joy = joy / length joy = joy * maxValue * pct end

14 Event Systems Polling Checking every frame for a specific value
"Are we there yet?" Event-Based Register to relevant event Notified when event occurs

15 Event System (Example)
class MouseManager List functions // Accepts functions passed as parameters with signature of // (int, int) function RegisterToMouseClick(function handler(int, int)) functions.Add(handler) end function Update(float deltaTime) bool mouseClicked = false int mouseX = 0, mouseY = 0 // Poll for a mouse click ... if mouseClicked foreach function f in functions f(mouseX, mouseY)

16 More Complex Event System
What we'd like: To bind keys such as the spacebar to actions such as "Fire." To be able to pass active actions to the UI first, then gameplay. Still don't have to do low-level polling everywhere of relevance.

17 More Complex System (Diagram)
Proposed input system

18 Touch Screens A touch screen allows the user to tap on the screen with his or her finger. Most touch screens today are multi-touch, meaning multiple fingers can be detected. Built-in libraries are available, both on iOS (UIGestureRecognizer) and Android (android.gesture).

19 Some of the Rubine algorithm features
For detecting pen-based gestures; could also be used for single-finger detection. Some of the Rubine algorithm features

20 Accelerometer Detects acceleration along the cardinal axes
Will always detect gravity because it's an acceleration Accelerometer

21 Gyroscope Detects rotation about the cardinal axes Gyroscope


Download ppt "Game Programming Algorithms and Techniques"

Similar presentations


Ads by Google