User Tools

Site Tools


en:tutorials:analog_joystick_gamepad_control

Analog Joystick / Gamepad Control Sticks

Orx supports joysticks and gamepads. The axis (for an analog stick) usually has a value between 0 and 32768 which indicates how far along the axis the stick has been pushed. Orx scales these values to down to the range 0.0 to 1.0.

This gives you smooth multi-direction control in both the X and Y directions.

Orx provides many Axis properties for your Input config.

Here's the list of the available joystick axes for the input config system (replace the * with the ID of the joystick you want to use, starting with 1):

  • JOY_X_*
  • JOY_Y_*
  • JOY_Z_*
  • JOY_R_*
  • JOY_U_*
  • JOY_V_*
  • JOY_POV_*

When pushing the stick in any direction, after getting past the small threshold, the value can be read using the orxInput_IsActive and orxInput_GetValue functions.

Let's work through setting this up. Firstly, init up a new project using these instructions.

Once you have a working project, add in the following graphic into your project's data to act as the object that will be controlled by the joystick:

Next, change the default Object in the config to use the ball.png graphic, and also to use a body so that we can have some physics (and deceleration) on the object:

[Object]
Graphic         = @
Texture         = ball.png
Pivot           = center
Position        = (0, 0, 0)
Body            = ObjectBody
 
[ObjectBody]
LinearDamping = 1.5
FixedRotation = true
Dynamic       = true
AllowSleep    = false
PartList      = ObjectBodyPart
 
[ObjectBodyPart]
Type  = box
Solid = true

Next, we will define the joystick direction controls in the [MainInput] section:

[Input]
SetList          = MainInput
DefaultThreshold = 0.2
 
[MainInput]
JOY_X_1	   = LeftRight
JOY_Y_1	   = UpDown
KEY_ESCAPE = Quit

The JOY_X_1 above means, get values from the analog stick when it is pushed along the X axis, either left or right. No value is read until it crosses the threshold (DefaultThreshold), which is 0.2, After that, register a LeftRight value.

The _1 at the end of JOY_X_1 means joystick #1. There can be up to 4 physical USB inputs used.

In the same way, the JOY_Y_1 means that if the analog stick has been moved up or down the Y axis, it will register a UpDown value.

Finally in the Run() function, we can add the code to respond to these “movements”. Run() is not normally the recommended place to put this code, but for demonstration purposes, it is fine:

        const orxFLOAT speed = 150;
 
	if (orxInput_IsActive("LeftRight")) {
		orxVECTOR speedVector = orxVECTOR_0;
		orxObject_GetSpeed(object, &speedVector);
 
		orxFLOAT x = orxInput_GetValue("LeftRight");
		speedVector.fX = x * speed;
 
		orxObject_SetSpeed(object, &speedVector);
	}
 
	if (orxInput_IsActive("UpDown")) {
		orxVECTOR speedVector = orxVECTOR_0;
		orxObject_GetSpeed(object, &speedVector);
 
		orxFLOAT y = orxInput_GetValue("UpDown");
		speedVector.fY = y * -speed;
 
		orxObject_SetSpeed(object, &speedVector);
	}

In the above code, the LeftRight and UpDown inputs are being checked to see if they are active. If so, get the current speed and the axis position values with orxInput_GetValue and set the proper speed direction value to 150 / the axis the value. The smaller the stick movement, the smaller the object movement.

It's all pretty simple, that's all there is.

en/tutorials/analog_joystick_gamepad_control.txt · Last modified: 2018/02/19 10:18 (3 months ago) by sausage