Page 1 of 1

Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 12:54 am
by Guimauve
Hello everyone,

For my 3D game project, I have to use the Joystick axis value to scale the force applied on Rigid Body piloted by the player. For the 3 first axis having values ranging from -32768 to 32767 (the same range as a "Word" variable) are useful but for the last one it's not useful at all. This axis should range for 0 to 65535 (the same range as a "Unicode" variable).

In 2007, I have created this solution but even if it work I'm sure a much faster and simple solution exist.

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Joystick Throttle Input Correction
; File Name : ThrottleInputCorrection.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : Guimauve
; Date : 02-02-2007
; Last Update : 09-08-2012
; PureBasic code : 4.70 Beta 1
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Procedure.u ThrottleInputCorrection(Input.w)
  
  If Input < 0
    ProcedureReturn Input - 32768
  ElseIf Input = 0
    ProcedureReturn 32768
  ElseIf Input > 0
    ProcedureReturn Input + 32768
  EndIf
  
EndProcedure

Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
Debug "; Here we test when the throttle is set near 0%"
Debug ""

For Index = -32768 To -32763
  Debug ThrottleInputCorrection(Index)
Next

Debug ""
Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
Debug "; Here we test when the throttle is set near 50%"
Debug ""

For Index = -3 To 3
  Debug ThrottleInputCorrection(Index)
Next

Debug ""
Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
Debug "; Here we test when the throttle is set near 100%"
Debug ""

For Index = 32762 To 32767
  Debug ThrottleInputCorrection(Index)
Next

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Any idea to improve this solution.

Best regards
Guimauve

Re: Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 1:31 am
by Demivec
Guimauve wrote:Any idea to improve this solution.

Code: Select all

Procedure.u ThrottleInputCorrection(Input.u)
   ProcedureReturn Input + 32768
EndProcedure

Re: Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 2:54 am
by Guimauve
As simple as that ...

I think it is time for me to find a job to be less stressed. Not finding the solution to a problem as simple as that, it's a shame.

Thanks Demivec.

Best regards.
Guimauve

Re: Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 5:16 am
by wilbert
Wouldn't it be faster to not use a procedure and simply do the addition of 32768 directly ?

Re: Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 11:56 am
by Guimauve
@Wilbert

Code: Select all

Macro JoystickThrottleInputCorrection(Input)
  
  (Input + 32768)
  
EndMacro
Best regards
Guimauve

Re: Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 5:48 pm
by Guimauve
Ok this the final version for the Throttle Input correction.

Code: Select all

Macro ThrottleInputCorrection(Input)
  
  (65535 - (Input + 32768))
  
EndMacro
With the previous version the Input as been inverted. At 0% we have 65535 and at 100% we have 0. With this version at 0% we have 0 and at 100% we have 65535.

Best regards.
Guimauve

Re: Joystick Throttle Input Correction

Posted: Fri Aug 10, 2012 6:17 pm
by wilbert
Your latest macro can be rewritten as

Code: Select all

Macro ThrottleInputCorrection(Input)
  
  (32767 - Input)
  
EndMacro
:wink: