DEV Community

Graham Long
Graham Long

Posted on

Craving control.

Overview

In order for the player to move around the scene and look around we need to receive input from the player, the planning of this aspect is what this blog post will concentrate on.

Device rotation

The desired control input method for looking around the scene would be using the android devices internal sensors to control the GameCameras look direction, unfortunately while following the android documentation on using the position sensors I discovered that my device does not support either of the required Sensor.TYPE_GAME_ROTATION_VECTOR or Sensor.TYPE_MAGNETIC_FIELD sensors.
So initially, I shall use an xbox controller to look around as well as move around the scene.

Xbox One Controller

The initial plan for moving around the screen was to use a Nintendo Joycon controller, while testing this though, the lag seemed to be too large to make it usable (even with bluetooth headphones connected at the same time).
Therefore I decided to change to an xbox one controller, pairing with my android device was seamless and once paired, the device responded quickly to stick movements and button presses.

Pairing

Pairing the controller with my android device was simple, I followed these steps:

  • Ensure your Xbox console is unplugged to prevent the controller pairing with the console
  • Turn on the controller by pressing the xbox button in the center of the controller which should then start flashing.
  • Hold the sync button at the top of the controller near the USB socket, until the xbox button flashes faster.
  • Enter your devices bluetooth menu and search for a the controller and pair with it, on my device it was called "Xbox Wireless Controller"

If pairing is successful, the xbox button should stop flashing and remain lit.

Controller abstraction

To facilitate changing the control input methods in future I plan on abstracting the game controls from the input methods using an interface for the look control input, an interface for the move control input, an interface for button control inputs and a hub which can use any class that implements these interfaces as the input methods.

The LookControlInterface

This interface is implemented by any class which can be used to generate look control input.
The XboxController class implements this interface and also the DeviceRotationSensor class implements it, so in future I can swap out the control methods depending on what is supported by the device.
LookControlInterface UML

The MoveControlInterface

This interface is implemented by any class which can be used to generate movement control input.
The XboxController class is the only one to implement this interface currently but any new controllers added going forward would also implement it.
MoveControlInterface UML

The ButtonControlInterface

This interface is implemented by any class which can be used to generate button press inputs (initially one an action button input).
The XboxController class is the only one to implement this interface currently but any new controllers added going forward would also implement it.

ButtonControlInterface UML

The GameControlHub Class

This class is the hub which connects the selected look control method, movement control method and button control method to the game code, thus allowing different control methods to be set based on the device capabilities.
GameControlHub UML

The next step

Following from this post, I shall be looking to implement the interfaces and classes detailed here and using the xbox controller as the main input method.

Top comments (0)