Analogue to Digital Joystick convertor

Community Forums/General Help/Analogue to Digital Joystick convertor

(tu) ENAY(Posted 2016) [#1]
I'm trying to write something that I wrote ages ago but somehow lost the code. For example if you have an angle of 86 or 93 it converts to 90, and 55 converts to 45, because 55 is nearer to 45 than it is 90

0, 45, 90, 125... 360 etc.

I know I could simply do a big for loop checking between > 0 < 45, > 45 < 90 until I get my answer, but I'm pretty sure I made an angle convertor that used some math that did it easily in one line of code.

Something like

(thisAngles > max angle) % anglearc

Anybody else have any idea what I'm talking about? :)

At any rate how do you guys go about converting analogue angles to digital angles efficiently is probably the better question than my ramblings.


Matty(Posted 2016) [#2]
i would imagine something like this should do it:

diff = thisangle % 45;
if(diff>22) then
newangle = thisangle - (thisangle % 45) + 45;
else
newangle = thisangle - (thisangle % 45);
endif


(tu) ENAY(Posted 2016) [#3]
Cheers Matty


xlsior(Posted 2016) [#4]
Not sure if this may help:

For Local t:Int=1 To 720
Print t+"  "+(((t+22) Mod 360)/45*45)
Next


(Adding the 22 (45/2) to account for rounding, since integer division will otherwise always round everything downwards, not upwards)


Floyd(Posted 2016) [#5]
The simplest way is to divide by 45.0, round to integer then multiply by 45.

In Blitz3D 45*Int(x/45.0) would be x rounded to a multiple of 45 because Int() is really round-to-integer.


TomToad(Posted 2016) [#6]
Code in BlitzMax