User Tools

Site Tools


en:tutorials:analog_joystick_gamepad_control

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:tutorials:analog_joystick_gamepad_control [2018/02/19 10:18 (7 months ago)] (current)
sausage created
Line 1: Line 1:
 +====== 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. ​
 +
 +{{section>​en:​orx:​config:​settings_main:​input_joystick#​[joystick_axes]&​noheader&​nofooter&​noeditbutton}}
 +
 +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 [[en:​tutorials:​creating_your_own_project|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:
 +
 +{{ :​tutorials:​ball.png |}}
 +
 +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:
 +
 +<code ini>
 +[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
 +</​code>​
 +
 +Next, we will define the joystick direction controls in the ''​[MainInput]''​ section:
 +
 +<code ini>
 +[Input]
 +SetList ​         = MainInput
 +DefaultThreshold = 0.2
 +
 +[MainInput]
 +JOY_X_1  ​  = LeftRight
 +JOY_Y_1  ​  = UpDown
 +KEY_ESCAPE = Quit
 +</​code>​
 +
 +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:
 +
 +<code c>
 +        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);​
 + }
 +</​code>​
 +
 +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 (7 months ago) by sausage