CSc 165 Lecture Notes 2 - Player Input Handling CSc 165 Computer Game Architecture Overview

• Device Types • Device Abstractions 2 - Input Handling • Controllers • Input Handling Packages • Event Queues • Input Action Commands (making the inputs do things)

2

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Types of Input Devices Input Handling Goals

• Keyboard • Steering Wheel Keep games device-independent o Game shouldn’t contain hard-coded device details • Mouse • Dance Pad o Game shouldn’t fail when a particular device is absent • (“POV”) • Guitar (allow substitution) • “POV Hat Switch” • WiiMote Keep engine device-independent

o Engine components should not contain hard-coded • • Kinect device details • • others? … isolate details in an Input Manager

3 4

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Device Abstractions “D-pad” (Directional-pad) Axes

Two fundamental device types: Discrete axis devices D-pad o Can have either one or two axes ▪ Button – returns pressed or not pressed Frequently represented as 1.0 or 0.0 Single-axis form: one component; U returns one value: L R ▪ Axis – returns a float .25 D Two types of Axis: .125 .375 Dual axis form: two components; each returns a value: ▪ Continuous: returns a value in a range 1.0 0 .5 e.g. { -1 … 1 } or { 0 … 1 } L N R X: ▪ Discrete: returns a value from a set .875 .625 -1 0 +1 e.g. [ 0, 1 ] or [ -1, 0, 1 ] .75 D N U

Can be absolute or relative N UL U UR R DR D DL L Y: -1 0 +1

0 .125 .25 .375 .5 .625 .75 .875 1.0

5 6 CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Controllers Controller Example: GamePad

Most “devices” are really collections :

o Keyboard: collection of (e.g. 127) “buttons”

o Mouse: collection of (typically 1 to 3 or more) L & R Triggers “buttons”, plus two “axes” (‘X’ and ‘Y’) PDP Afterglow o Gamepad: collection of buttons and axes -360 GamePad Right Controller Left Joystick ❑ Multiple physical buttons Joystick ❑ Joystick: two (continuous) “axes” (per joystick) 16 “devices”: 10 Buttons, ❑ D-pad: one or two (discrete) “axes” 2 (4 continuous axes, each {-1..1}), ❑ “POV hat switch”: one or two (discrete) “axes” 2 “Triggers” (combined as a single Axis {-1..1}),

1 D-pad (1 discrete axis: Currently pressed: D-pad A, Y, & LB [0, .125, .25, … .875, 1]) (currently Left & Up, = Device collection == “Controller” 0.125)

ControlPanel -> Devices&Printers -> RIGHT-click the controller icon -> Settings > Properties 7 8

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Controller Example: Dance Pad Controller Example: Guitar

9 1 10

7 8

Wah-wah handle

1&2 = Vert. Axis 3 4 3&4 = Horiz. Axis Guitar orientation

6 Currently pressed: 5 Forward, Right, “O” 16 “devices”: 10 Buttons, Chord Guitar 2 5 continuous axes, each {0..1}, buttons pick = .75 1 discrete axis, [ 0, .125, .25, .375, … .875, 1.0] 12 “devices”: 10 Buttons, (pick Off/Up/Down = [0, .25, .75]) 2 discrete axes, each [-1, 0, 1] 9 10

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Controller Example : Steering Wheel Accessing Game Controllers DirectInput X axis (-1..1) Buttons o Windows-specific (part of Microsoft’s DirectX framework) o (deprecated)

Currently turning left XInput and pressing gas o Microsoft API for controllers Y axis pedal D- (-1..0) o No support for keyboards or mice pad OIS (Object-oriented Input System)

o SourceForge (open source) project, mostly C++ Y axis (0..1) JInput

o Part of JGI (Java Gaming Initiative) framework (JOGL, etc.) 15 “devices”: 12 Buttons (0-11), D-pad up-left, 1 discrete axis (D-pad, “pov”) [ 0, .125, .25, .375, … .875, 1.0], returning 0.125 o Under negotiation with Jogamp 2 continuous axes (X & Y) each [-1..1] o Supports Windows, Linux, OS-X, AWT, etc.

11 12 CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Primary JInput Objects JInput Organization

<> <> Component • ControllerEnvironment Controller Contains the collection of defined “controllers” Controller- ▪ Examples: Keyboard, Mouse, Joystick, GamePad… Environment * Abstract- * Abstract- Component • Controller Controller Contains a collection of “components” (input generators) Key Examples: button, key, slider, dial, controller Button Can also contain “rumblers” (output feedback devices) <> <> <> <> Keyboard Mouse Windows- … Linux- • Component Controller Controller Axis An object with a single “range”

▪ Button: on/off OS-specific keyboard loaded by ▪ Key: pressed/notPressed driver implementations OS-specific game controller driver OS-specific game JInput at ▪ Axis: a value in some range OS-specific mouse implementations controller driver startup driver implementations implementations 13 14

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Controller Attributes Component Attributes

• Name (human-readable) • Name (human-readable) • “Identifier” (type) • Type o Axis, Button, or Key o Keyboard, Mouse, Fingerstick, GamePad, HeadTracker, Rudder, Stick, , • Return value type Trackpad, Wheel, Unknown o Relative: value is relative to previous return value o Absolute: value is independent of previous return value • Array of (sub)-controllers • Return value range capability • Array of components o Analog: allows more than two values • Array of rumblers o Digital: only two values allowed (e.g. a button) • Dead zone value • Event Queue o Threshold before switching from 0 to non-zero (useful for joysticks: minor movement ignored)

See code example for accessing controller attributes with JInput See code example for accessing component attributes with JInput 15 16

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Component ID Input Events

Every component has a type identifier Each controller has an event queue predefined identifiers in Jinput javadoc for Component.Identifier.* Event component Component value time

Identifier

Key Button Axis Button Button Key press Motion in press Axis X or Y change press or or or release release release … LEFT_ X Y Z POV X_ACCELERATION _0 _1 _31 MODE … A … Z CTRL … … THUMB

RX_FORCE … BACK RX RY RZ A B X Y 17 18 CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling Simplifying Input Handling RAGE InputManager*

Game goals: ▪ For each device component event, invoke some Implements: (game-specified) action associated with that event associateAction(controller, ▪ Hide details inside Game Engine component, Action,…)

Examples: o Registers a user-specified action corresponding ▪ Gamepad Button 2 pressed → Fire Rocket to the given controller & component ▪ Keyboard ‘f’ key pressed → Fire Rocket update(float time) ▪ Joystick “X” axis moved → Change Camera View ▪ Guitar “Pick” axis “down” → o Polls the underlying device event queues button = getCurrentChordButton(); o Performs event dispatch (action invocation) if (button == displayedNote) {score++}

19 *adapted from SAGE 20

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling

RAGE InputManager (continued) Defining an Action interface

(1) register component-event/action <> Application Action (game) (2) update( ) InputManager + performAction(float time, Event e)

(4) invoke actions <> (3) getEvents() Abstract- JInput InputAction JInput Event Queue - speed + getSpeed() + setSpeed(float) + performAction(float,Event)

MyAction

+ performAction(float,Event)

21 22

CSc 165 Lecture Notes CSc 165 Lecture Notes 2 - Player Input Handling 2 - Player Input Handling

Button/Key Action Types Modifier Actions

Actions can contain modifier actions. Not all actions should be invoked on every component state-change Example: MoveForward is a standard Action Object ▪ On Press Only: RunAction is also an Action Object, but its purpose is to modify Fire_Missile_Action, Reset_Camera_Action how fast MoveForward operates ▪ On Press And Release: Usage example: Toggle_Running_Action ▪ Construct both actions, associate them both with user inputs. ▪ Pass the RunAction object to MoveForward during construction ▪ Repeatedly while held down: ▪ Invoking RunAction modifies state information Move_Forward_Action ▪ When invoking MoveForward, its behavior depends on the state information in RunAction (see code example) 23 24