Programming Input Devices
Input devices come in a wide range. They are the only devices that separate the player from the running game. These devices try to capture the intention of the user to perform an action into a specific space of inputs, only to be translated into comprehensible events by the game engine (through the input engine). Thus, both input devices and input engine need to be well designed to fit the human.
Some example of device input
There is a large variety of devices nowaday, such as:
Mouse
Keyboard
Controller's buttons (DS4, XBox)
Analog Axes
Accelerometes
Camera
...
Game Engine HID system and detecting IE (input events)
Retrieving low level event from input device is quite a tedious task. Usually, the game engine you use should handle some of the inputs of the device, by wrapping them into either objects or structs.
Raylib offers a wide inputs wrappers.
KB Down/UP
Invoking the IsKeyDown()
boolean function allow us to check whether a specific key is currently pressed or not. Respectively, IsKeyUp()
boolean function allows us to check is a specific key is NOT being pressed. There is also the isKeyPressed()
, etc...
To use such input-related kb function, in the main loop, considers to check inputs before the update of the world.
Mouse input-related function
The mouse is a more complex input device than a keyboard, mostly because it evolves in a 2D space, and in a continued space, discretized by the refresh rate of the embeded mouse's cpu. Therefore, two coordinates are generated periodically, in addition of mouse's click that can be performed by the player.
Generally speaking, clicks work the same as keys of a keyboard: we check if a mouse's button as been pressed or released and we take action accordingly.
However, mouse position implies coordinates, translated into the game world (either in 2D or 3D). Depending of the HID, mouse position has to be retrieved individually (first get the x then the y), or in a special structure. The later is done in raylib. You have a predefined Vector2
struct representing coordinate in a 2D space, and the mouse position retrieving function has been implemented around this structure.
You also can handle the mouse's wheel in raylib.
Game Controller
Game controllers become more and more common nowadays: they encompass a KB style approach with, generally speaking, two analog axises. Some of them can also embed accelerometers. However, they are quite identiqual in the way of handling the inputs, except that, some time, some extra driver are requiered in order to the OS to correctly interprets the device event.
Consequently, before taking into account inputs, one should identify if the gamepad is plug, and what king of controller it is. In raylib:
Then, detecting button down is straightforward:
And to retrieve an axis movement from a specific analog stick:
DO NOT FORGET THE DEAD ZONES!
If you use intensively a two-stick control scheme, it is most than probable that you observe during your QA phase that camera will behave weirdly. This is often due to the fact that dead zone for pitch control is not taken into account. Therefore, if the stick is push not exactly on the X (or Y), then movement on the other axis (Y or respectively X) is also recorded: the camera will also move in an unwanted axis. Do not forget that.
To handle dead zone, in the easiest way you juste have to define a threshold, and check if it is reached.
Few tips
Make your objects or structures agnostic of the input devices !
Instead, map controls directly to the controlled objects. For example, in a A-RPG, if you have a double jump action allowed, a character could send the action JUMP or DOUBLE JUMP independently of the input device associated by being translated to the controler (not the input device) of the character.
Consider your control scheme carefully: do you want to stay with a well-known mapping or go in a new direction?
Be cautious with overloading simple controls with complicated results!
Always give the player some feedback. Not to much, thought!
Players won't use it if they don't know about it.
Avoid pixel perfect accuracy! Especially now with UHD resolution.
Think ergonomic!
Watch and learn
Last updated
Was this helpful?