ÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÀ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌÀ¬ÀÀÀ¬ÀÀÀ¬ÀÀÎÌÌÌ̈ˆˆ¬ªªª¬ˆˆˆ¬ÌÌ̬ÌÌ̬ÌÀ̬ÌÀÌÌÌìÌÀÀî̬ÀÌ̬ÀÀÀ¬ÀÀÀ¬ÀÀ¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî¬ÌÌ̬ªªª¬ª¬ª¬ªÊ̬ªªªŒˆˆˆÌÀÌÎÌÌÎ̬̪ªÀ쬪ÀÀÀÌîÀìîÌÌîî >/ÌÌ ÀÌÌÀÌÌÀÌ ÌÌÌ À ÀÌ À ÀÌÌÀ ÀÌÌÀ ÀÌ ÌÌÌÌÀÌÌÌÌÀÀ Ì ÌÀÀÌÌÀ À ÀÀ À ÀÀÌÀÀ À À À ÌÌ Ì Ì Ì ÌÀÀ Ì ÌÌÌ ÌÀ À ÀÌÀ ÀÀÌÌÀ ÌÀ ÀÌ À ÌÌÌÌ Ì Ì ÌÌÌÀ Ì À ÀÌÌÀ À ÀÀ ÀÌÌÀ ÌÀ ÀÀ À À ÌÌ ÌÌÌÌ ÌÌÀ ÀÌÀÌÀ ÀÌ ÌÌÌÌ À ÀÀ ÀÀ ÀÌÌÀÌÌÀÌÌÀÌÌÀÌ ÌÌÌ Ì Ì À Ð Ð Ð Ý ÝÐÝÝÝÐÝÐÝ ÐÝ ÐÝ ÐÝÐÝ ÐÝÐÝ ÐÝÝÐÝÐÐÝÝÝÝÝ ÐÝ Ð ÐÝÝÝÝÐÐÝÝÝÐÝÐÝ ÝÝ Ý ÝÐ Ð Ð Ý Ý Ð ÝÐ Ý Ý ÝÝ ÝÝ ÝÐ Ý Ð Ý ÝÝ Ý Ý ÝÝ Ý Ý ÝÐ ÝÐ ÐÐÝÝÐ ÐÝÝ Ð Ð Ý Ý Ð Ý Ý Ý Ý Ý Ð Ý Ð Ý Ý Ý ÝÝ ÝÐ ÐÝÐ ÐÝÐÝÐÝ ÐÝ ÐÝ ÐÝÐÝ ÐÝÐÝ ÐÝ ÐÝÝ Ý ÝÝÐ ÐÝÝ ÐÝÝÐÝÝÐ ÐÝÝ 003003303030033333003303300330330030303030303000303003030303303030003000330330003003033333300330033033300303033PPUPUUPUUUPUPUUUPPPUUPUUUPUUUPUUUPUUUPUPUUUPUUUPUUUPUUPPUUPUUUPPPUPPUPPUPUPPPUPUPUUUPUUUPUPPPUPUPUPPUPUUPPUUUUPPUUUUPUPPUUPPPUUPPUUUUPPUPPPUPUUPPUUPUUPPUUPUUUPUPUUUPUUUPUPUUPUUPUUPUPUÐÝÝÝÝÝÐÝ ÝÝÝ Ý ÝÐ ÝÐ ÐÐÐ Ý ÐÐ ÐÐ ÐÐ ÐÐ  PÐPÐÐPÐ P P P ÐPÐÐÐÐUÐÐUÐ U U U U P 33333333 ÐÐÐÐÐÐÐÐ ÐÐÐÐ ÐÐ ÐÐ ÐÐ ÐÐ Ð ÝÐÐÝÐ ÝÐ Ý Ý ÐÝ ÝÝÐÝÝÝÝݬ -- title: joystick angle library -- author: atesin (atesin%gmail.com) -- desc: function to read joysticks angle fast -- site: soon -- license: WTFPL, but mention me would be nice -- version: 0.1 -- script: lua --[[ WHY THIS FUNCTION best viewed with narrow font certainly, when operating with angles and vectors, we can read joystick position and translate to angles using a lot of "btn()", "if" and math functions, which is very cody, long, slow and inefficient instead that i read ram values directly with peek() and use a tanslation table HOW IT WORKS screen in tic80 has (x,y) cartesian coordinates like 0,0 120,0 +--------+ | | | | +--------+ 0,136 120,136 compared with analytic geometry you see the plane is upside down, origin is at top-left corner and y-axis increments while moving DOWN so angles are also upside down, they start pointing to right as usual but increment rotating CLOCKWISE, if we divide the circle in 8 parts like a pizza we got 6 5 7 4 + 0 3 1 2 readStick() functions return similar values but in positive radians, with a negative number when joystick is left at center or blocked (key presses that nullify each other) --]] -- read joystick #1 angle -- return: joystick #1 angle (0-2pi) or negative if centered function readStick() local stickMap={-1,6,2,-1,4,5,3,4,0,7,1,0,-1,6,2,-1} return stickMap[1+peek(0x0FF80)&15]*math.pi/4 end -- read joystick angle, multiplayer version -- param stick: (optional, default=1) joystick id (1-4) -- return: joystick angle (0-2pi) or negative if centered -- throws: error if joystick id is out of range function readStick(stick) stick=stick or 1 -- default parameter if stick<1 or stick>4 then error('out of range') end local stickMap={-1,6,2,-1,4,5,3,4,0,7,1,0,-1,6,2,-1} return stickMap[1+peek(0x0FF7F+stick)&15]*math.pi/4 end -- the only global variable we need persistent targetAngle=0 -- main loop function TIC() -- circle sections expressed in radians -- this is very inefficient but didactical circ1=math.pi*2 circ2=circ1/2 circ128=circ1/128 -- default joystick movement vector: none (centered) local jx,jy=0,0 -- read the joystick #1 angle in radians local stickAngle=readStick() -- if joystick moves, process changes if stickAngle>=0 then -- new joystick movement jx,jy=math.cos(stickAngle),math.sin(stickAngle) -- angle difference between joystick and target local deltaAngle=(targetAngle-stickAngle)%circ1 -- if difference is too small set target as joystick if deltaAnglecirc2 then targetAngle=(targetAngle+circ128)%circ1 else targetAngle=(targetAngle-circ128)%circ1 end end -- calculate target movement vector local tx,ty=math.cos(targetAngle),math.sin(targetAngle) cls() -- print some info print('JOYSTICK ANGLE LIBRARY',0,0,12) print('read code comments for more info',0,8,13) print('joystick angle: '..stickAngle,0,16,3) print('target angle: '..targetAngle,0,24,5) -- trace vectors centered in screen circb(120,68,32,13) line(120,68,120+(tx*32),68+(ty*32),5) line(120,68,120+(jx*16),68+(jy*16),3) end