The Issue
In the relatively short amount of time that I've spent creating in PICO-8 and viewing other PICO-8 developers' creations, I've realized that jittery diagonal movement seems to be one of the most common issues in PICO-8 projects. Through online searches, I've seen a few different methods of preventing this problem, but so far I haven't seen anything that also accounts for accelerating/decelerating velocity. The cart and code below showcases the solution (heavily inspired by the code from two of @Krystman's Lazy Devs videos: https://www.youtube.com/watch?v=oBjZ1W50brM and https://www.youtube.com/watch?v=stoDWgR-kF8) that I'm currently using in a WIP game. I'm wondering what other solutions users have found, and would greatly appreciate feedback on how my current method can be improved.
Notes: the code below is not the same code I am using in my WIP game, so recommendations on how to save tokens or make my code cleaner may not be applicable. Additionally, I am not actually following @Krystman's tutorial series, I only used the aforementioned videos for ideas on this specific issue, so if the code was improved on later in the series, I have not seen it.
Cart
Arrow keys to move, (O) to toggle between normal and smooth movement.
Code
function _init() x=60 y=60 xv=0 yv=0 speed=2 accel=0.3 olddir=0 destutter=false toggle="normal" function smooth(x1,x2,x3) return (x1-x2)*x3 end end function _update() if btnp(🅾️) then if toggle=="normal" then toggle="smooth" pal(8,12,1) else toggle="normal" pal(8,8,1) end end local input=btn()&0xf local inputarr={1,2,0,3,5,6,3,4,8,7,4,0,1,2,0} local dir=inputarr[input] or 0 local dxarr={-1,1,0,0,-0.71,0.71,0.71,-0.71} local dyarr={0,0,-1,1,-0.71,-0.71,0.71,0.71} local dx=dxarr[dir] or 0 local dy=dyarr[dir] or 0 xv+=smooth(dx*speed,xv,accel) yv+=smooth(dy*speed,yv,accel) if dir>=5 and toggle=="smooth" then local xspeed=abs(xv) local yspeed=abs(yv) if dir!=olddir or toggle!=oldtoggle then destutter=false elseif abs(xspeed-yspeed)<0.1 and not destutter then x=flr(x+0.5) y=flr(y+0.5) local newspeed=(xspeed+yspeed)/2 xv=dx*newspeed yv=dy*newspeed destutter=true end end oldtoggle=toggle olddir=dir x+=xv y+=yv end function _draw() cls() spr(1,x,y) print(toggle,1,1,8) end |
[Please log in to post a comment]