Blitz3D+ Command Reference

JoyX# ( [port] )

Parameters

port (optional) - which joystick to read; 0 is the first stick (default)

Description

Returns the position of the joystick's x-axis as a float from -1 to 1.

Fully left is -1, centred is 0 and fully right is 1, with everything in between available on an analogue stick. That is what makes it better than JoyXDir for driving and flying games: multiply the value by your steering rate and a gentle lean gives a gentle turn.

Real sticks rarely rest at exactly 0, so apply your own dead zone - ignore anything smaller than about 0.15 - or a slow drift creeps into the controls. Squaring the value while keeping its sign is a common trick for finer control near the centre.

Heads up: the current DirectX 12 runtime does not enumerate joysticks - it reports zero attached devices - so JoyX returns 0.0 on every port, as does an out-of-range port number. Check JoyType before you build controls around it, and always offer a keyboard alternative.

The other axes follow the same pattern: JoyY for forwards and back, JoyZ for the throttle or twist, and JoyU, JoyV and JoyHat for the extras.

See also: JoyY, JoyZ, JoyXDir, JoyType, JoyDown.

Example

; JoyX Example
; ------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

While Not KeyDown(1)

    Cls

    ; JoyX returns the stick's x-axis as a float from -1.0 (full
    ; left) to 1.0 (full right); JoyY is its vertical partner.
    jx#=JoyX()
    jy#=JoyY()

    If JoyType()=0 Then
        Text 0,0,"Esc: exit"
        Text 0,60,"No joystick detected - plug in a gamepad to try this example."
        Text 0,80,"With one attached, the crosshair below follows the stick."
    Else
        Text 0,0,"Move the stick left and right   Esc: exit"
    End If

    ; Crosshair driven by the stick (sits centred with no input)
    x=320+jx*220
    y=240+jy*180
    Color 90,90,90
    Line x,120,x,479 ; vertical marker tracks the JoyX value
    Color 255,255,0
    Oval x-10,y-10,20,20,False
    Line x-14,y,x+14,y
    Line x,y-14,x,y+14

    Color 255,255,255
    Text 0,20,"JoyX() = "+jx
    Text 0,40,"JoyY() = "+jy

    Flip

Wend

End

Index