TGC Codebase Backup



Joystick control and force by IanM

31st Aug 2003 9:36
Summary

Joystick control with deadzones, 4 axis analog movement with adjustable ranges and force-feedback detection



Description

General Joystick commands - Selectable deadzone in the centre of the controller, selectable range of values, 4 axis' of movement within that range, and simple detection of force-feedback capability



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` joystick.dba

` Joystick normalisation and capabilities

remstart
AVAILABLE FUNCTIONS*********************************************************************

SetJoystickDeadZone()
   Specifies the deadzone for the JoystickAxis() routine. Defaults to 5%.

SetJoystickAxisRange()
   Specifies the range for the JoystickAxis() routine. Defaults to 100 (%).

JoystickAxis()
   Normalised positional info for the axis specified.
   Axis 0 = main x, Axis 1 = main y, Axis 2 = secondary x, Axis 3 = secondary y

HasForceFeedback()
   Discovers whether the current device has force feedback capabilities. The results
   are cached for performance.

****************************************************************************************
remend

type T_Controller
   Initialised as integer
   DeviceName as string
   HasForceFeedback as integer
   DeadZone as integer
   Range as integer
endtype

global Controller as T_Controller

function __InitialiseController()
   Controller.Initialised=1
   HasForceFeedback()
   SetJoystickDeadZone(5)
   SetJoystickAxisRange(100)
endfunction

function SetJoystickDeadZone(Size as integer)
   if Controller.Initialised <> 1 then __InitialiseController()
   Controller.DeadZone=Size
endfunction

function SetJoystickAxisRange(Range as integer)
   if Controller.Initialised <> 1 then __InitialiseController()
   Controller.Range=Range
endfunction

function JoystickAxis(Axis as integer)
   local Result as integer
   local Range as integer

   if Controller.Initialised <> 1 then __InitialiseController()

   Range=Controller.Range

   ` Get the reading and adjust to within the requested range - note
   ` that the numbers being used are slightly lower (2%) than you would normally
   ` expect - this is to allow you to get the maximum range without breaking
   ` your joystick.
   select Axis
      case 0 :  Result=joystick x()/(980/Range) : endcase
      case 1 :  Result=joystick y()/(980/Range) : endcase
      case 2 :  Result=(joystick twist z()/(32112/Range))-Range : endcase
      case 3 :  Result=(joystick slider a()/(32112/Range))-Range : endcase
   endselect

   ` If within the deadzone, set it to zero so that it is ignored
   if abs(Result) <= Controller.DeadZone
      Result=0
   else
      ` If outside the requested range, adjust to inside it - this will happen
      ` because of the adjustments made above.
      if Result < (0-Range)
         Result=0-Range
      else
         if Result > Range then Result=Range
      endif
   endif
endfunction Result

function HasForceFeedback()
   local Current as string
   local Device as integer
   local Result as integer

   if Controller.Initialised <> 1 then __InitialiseController()

   perform checklist for control devices

   Current=control device name$()

   ` Check the cached results
   if Controller.DeviceName = Current

      ` If already checked, get the cached results
      Result=Controller.HasForceFeedback

   else

      ` Otherwise, check for the device details
      Result=0

      for Device=1 to checklist quantity()

         ` If this is the one we want
         if checklist string$(Device) = Current

            ` Get the force-feedback status
            Result=checklist value a(Device)


            exit
         endif

      next Device

      ` Cache the results for next time
      Controller.HasForceFeedback=Result
      Controller.DeviceName=Current

   endif

endfunction Result


` Modification History
` $Log: joystick.dba,v $
` Revision 1.1  2003/03/12 13:43:27  Ian Mold
` Initial load into CVS
`