Posted: 14th Oct 2022 11:43
Hi I have an alternative for deadzone control of thumbstick, the original deadzone mode snaps to Up, Down, Left and Right (like jumping a few degrees when you get close to the straight directions), I've made an alternative mode where you can have a center deadzone instead, or combine snapping for one stick and free movement for the other. Just thought I'd share in case someone needed a quick fix for not-snapping.



+ Code Snippet
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 0, 0, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

global mode as integer
mode = 2 //1=Original deadzone mode

global joystickDeadZone# as float
joystickDeadZone# = 0.15

global leftThumbStickMode as integer
global rightThumbStickMode as integer

if mode = 1
	SetRawJoystickDeadZone(joystickDeadZone#)
	
	leftThumbStickMode = 1 //1=Original mode (mode also has to be set to 1), 2=Custom deadzone mode 2 (free of snapping), 3=Custom deadzone mode 3 (snapping but can be chosen individually for each thumb stick)
	rightThumbStickMode = 1 //1=Original mode (mode also has to be set to 1), 2=Custom deadzone mode 2 (free of snapping), 3=Custom deadzone mode 3 (snapping but can be chosen individually for each thumb stick)
endif

if mode = 2
	SetRawJoystickDeadZone(0.0)

	leftThumbStickMode = 2 //1=Original mode (mode also has to be set to 1), 2=Custom deadzone mode 2 (free of snapping and can be chosen individually for each thumb stick)), 3=Custom deadzone mode 2 (snapping and can be chosen individually for each thumb stick)
	rightThumbStickMode = 3 //1=Original mode (mode also has to be set to 1), 2=Custom deadzone mode 2 (free of snapping and can be chosen individually for each thumb stick)), 3=Custom deadzone mode 2 (snapping and can be chosen individually for each thumb stick)
endif

do
	
	if mode = 1
		print(GetRawJoystickX(1))
		print(GetRawJoystickY(1))
		print(GetRawJoystickRX(1))
		print(GetRawJoystickRY(1))
		
		DrawLine(300.0, 200.0, 300.0 + (GetRawJoystickX(1) * 100), 200.0 + (GetRawJoystickY(1) * 100), MakeColor(255, 255, 255), MakeColor(255, 255, 255))
		DrawLine(500.0, 200.0, 500.0 + (GetRawJoystickRX(1) * 100), 200.0 + (GetRawJoystickRY(1) * 100), MakeColor(255, 255, 255), MakeColor(255, 255, 255))
	endif
	
	if mode = 2
		print(GetRawJoystickLeftThumbStickX(1))
		print(GetRawJoystickLeftThumbStickY(1))
		print(GetRawJoystickRightThumbStickX(1))
		print(GetRawJoystickRightThumbStickY(1))
		
		DrawLine(300.0, 200.0, 300.0 + (GetRawJoystickLeftThumbStickX(1) * 100), 200.0 + (GetRawJoystickLeftThumbStickY(1) * 100), MakeColor(255, 255, 255), MakeColor(255, 255, 255))
		DrawLine(500.0, 200.0, 500.0 + (GetRawJoystickRightThumbStickX(1) * 100), 200.0 + (GetRawJoystickRightThumbStickY(1) * 100), MakeColor(255, 255, 255), MakeColor(255, 255, 255))
	endif
	
	sync()
loop

function GetRawJoystickLeftThumbStickX(joystick)
	returnValue# = 0.0
	
	lx# = GetRawJoystickX(joystick)
	ly# = GetRawJoystickY(joystick)
	
	if leftThumbStickMode = 1
		returnValue# = lx#
	endif	
	if leftThumbStickMode = 2
		if lx# < -joystickDeadZone# or lx# > joystickDeadZone# then returnValue# = lx#
		if ly# < -joystickDeadZone# or ly# > joystickDeadZone# then returnValue# = lx#
	endif
	if leftThumbStickMode = 3
		if lx# < -joystickDeadZone# or lx# > joystickDeadZone# then returnValue# = lx#
	endif
endfunction returnValue#

function GetRawJoystickLeftThumbStickY(joystick)
	returnValue# = 0.0
	
	lx# = GetRawJoystickX(joystick)
	ly# = GetRawJoystickY(joystick)
	
	if leftThumbStickMode = 1
		returnValue# = ly#
	endif	
	if leftThumbStickMode = 2
		if lx# < -joystickDeadZone# or lx# > joystickDeadZone# then returnValue# = ly#
		if ly# < -joystickDeadZone# or ly# > joystickDeadZone# then returnValue# = ly#
	endif
	if leftThumbStickMode = 3
		if ly# < -joystickDeadZone# or ly# > joystickDeadZone# then returnValue# = ly#
	endif
endfunction returnValue#

function GetRawJoystickRightThumbStickX(joystick)
	returnValue# = 0.0
	
	rx# = GetRawJoystickRX(joystick)
	ry# = GetRawJoystickRY(joystick)
	
	if rightThumbStickMode = 1
		returnValue# = rx#
	endif	
	if rightThumbStickMode = 2
		if rx# < -joystickDeadZone# or rx# > joystickDeadZone# then returnValue# = rx#
		if ry# < -joystickDeadZone# or ry# > joystickDeadZone# then returnValue# = rx#
	endif
	if rightThumbStickMode = 3
		if rx# < -joystickDeadZone# or rx# > joystickDeadZone# then returnValue# = rx#
	endif
endfunction returnValue#

function GetRawJoystickRightThumbStickY(joystick)
	returnValue# = 0.0
	
	rx# = GetRawJoystickRX(joystick)
	ry# = GetRawJoystickRY(joystick)
	
	if rightThumbStickMode = 1
		returnValue# = ry#
	endif	
	if rightThumbStickMode = 2
		if rx# < -joystickDeadZone# or rx# > joystickDeadZone# then returnValue# = ry#
		if ry# < -joystickDeadZone# or ry# > joystickDeadZone# then returnValue# = ry#	
	endif
	if rightThumbStickMode = 3
		if ry# < -joystickDeadZone# or ry# > joystickDeadZone# then returnValue# = ry#		
	endif
endfunction returnValue#