November 25, 2020, 04:57:51 AM

Author Topic: [bb] Joystick Correction by jfk EO-11110 [ 1+ years ago ]  (Read 779 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Joystick Correction by jfk EO-11110 [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Joystick Correction
Author : jfk EO-11110
Posted : 1+ years ago

Description : Thought those fancy random values returned by JoyX and JoyY should be filtered out. Try this:

Code :
Code: BlitzBasic
  1. ; JOYSTICK CORRECTION
  2. ;
  3. ; On some Joysticks you get fancy values at random times.
  4. ; If you had this Problem too then you might try this:
  5. ; These Functions will filter and smooth the values of JoyX() and JoyY()
  6. ; simply use "my_joyy()" instead of "joyy()". Don't forget to set the globals.
  7. ; you should use this with framerates up to about 60 Hz. If it's running too fast then
  8. ; the filter don't works perfect anymore.
  9. ;
  10. ; this Method has a One-Call-Lag: the functions return the previous Value, not the current.
  11. ; This is neccessary to detect and filter potential Random Peeks aka 'spikes'.
  12. ; If such spikes are detected they will be replaced by the interpolation of the two surrounding
  13. ; peeks.
  14. ; You should not call the functions more than one time in your Loop.
  15. Graphics3D 320,240,0,2
  16. Global joyvx1#,joyvx2#,joyvx3#
  17. Global joyvy1#,joyvy2#,joyvy3#
  18.  
  19. ;--------------------
  20. Color 255,255,255
  21. While KeyDown(1)=0
  22.  jx# = my_joyx#()
  23.  jy# = my_joyy#()
  24.  ;---
  25.  ; compare for yourself:
  26.  Print jx#+" "+jy#+" "+JoyX()+" "+JoyY()+" "
  27.  Color 0,0,0
  28.  Rect 0,0,320,17,1
  29.  Color 255,0,0
  30.  Text 0,0,"my_hoyx(),my_joyy(), JoyX(),JoyY()"
  31.  Color 255,255,255
  32.  ;---
  33.  Flip            ; note: if you turn off sync - the faster the
  34. Wend             ; FPS the more Random Peeks you still get
  35. End
  36. ;--------------------
  37.  
  38. Function my_joyx#()
  39.  jl#=JoyX()
  40.  jst$=Str$(jl#)
  41.  jle=Len(jst$)
  42.  If jle>10 Then
  43.   If Mid$(jst$,jle-4,1)="e"
  44.    jl=0.0
  45.   EndIf
  46.  EndIf
  47.  joyvx3 = joyvx2
  48.  joyvx2 = joyvx1
  49.  joyvx1 = jl
  50.  If (Abs(joyvx3-joyvx2)>0.1) And (Abs(joyvx2-joyvx1)>0.1)
  51.   joyvx2=(joyvx3+joyvx1)/2
  52.  EndIf
  53.  Return joyvx2
  54. End Function
  55.  
  56. Function my_joyy#()
  57.  jl#=JoyY()
  58.  jst$=Str$(jl#)
  59.  jle=Len(jst$)
  60.  If jle>10 Then
  61.   If Mid$(jst$,jle-4,1)="e"
  62.    jl=0.0
  63.   EndIf
  64.  EndIf
  65.  joyvy3 = joyvy2
  66.  joyvy2 = joyvy1
  67.  joyvy1 = jl
  68.  If (Abs(joyvy3-joyvy2)>0.1) And (Abs(joyvy2-joyvy1)>0.1)
  69.   joyvy2=(joyvy3+joyvy1)/2
  70.  EndIf
  71.  Return joyvy2
  72. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal