Hello everyone, how are you doing? I'm trying to do something here and if you guys could help me somehow it'd be nice, I'm already grateful for your attention.
The thing is I was able to achieve the "rotation around a point" for Sprites ( in this case a circle ), but the issue is that I want to have multiple objects rotating around a point while starting at different "steps" of the rotation itself ( I'm attaching a image to explain it in more detail).
The code I'm using is the following:
MAIN.LUA
--sincos math timer=0 scr={ w=128, h=128 } opt={ spd=.5 } pix={ c=12, x=0, y=0 } cir={ timer=0, a=false, spd=.5, init=3, o=20, c=12, x=0, y=0, r=6 } function _init() cir.x=scr.w/2 cir.y=scr.h/2 pix.x=scr.w/2 pix.y=scr.h/2 end function _draw() cls(1) pset(pix.x,pix.y,pix.c) print("speed "..opt.spd) for entry in all(circles) do circ(entry.x,entry.y,entry.r,entry.c) end end function _update60() update_keys() for entry in all(circles) do entry.timer+= entry.spd/60 entry.x= pix.x+cos(entry.timer)*entry.o entry.y= pix.y-sin(entry.timer)*entry.o end if(keys.down) then opt.spd-=.1 elseif(keys.up) then opt.spd+=.1 end if(keys.right) then pix.x+=1 end if(keys.left) then pix.x-=1 end if(keys.activate) then make_circle(pix.x,pix.y,opt.spd,15,12) end if(keys.delete) then circles={} end end -- methods -- -- make_circle(x,y,s,o,c) |
UTILS.LUA
--utils circles={} function make_circle(x,y,s,o,c) local obj={ timer=0, a=false, spd=s, o=o, c=c, x=x, y=y, r=6 } add(circles,obj) end function update_keys() keys={ activate=btnp(🅾️), delete=btnp(❎), right=btn(1), down=btnp(3), left=btn(0), up=btnp(2), } end |
Thanks in advance for any help and have a good day!
--vars we'll need px,py=64,64 orbiters,orbitradius=4,5 |
--in _draw() for i=1,orbiters do local portion=i/orbiters local x,y=px+orbitradius*cos(portion),py+orbitradius*sin(portion) circ(x,y,2,7) end |
Add t()
within the cos and sin calls to have them revolve around the center over time. You can multiply t() by a factor (2*t()
) to influence the speed of the rotation.
Rather than keeping each orbiter x,y local as in this example, you can store those in global scope and have access to them elsewhere if you need (e.g. for collision checks)
Thanks a lot for the reply, I needed to make a few changes here but everything worked smoothly, didn't really think about the portion=i/orbiters part of the code but it honestly makes sense, it'd be the same as getting positions as in 0, 0.25, 0.5, 0.75 and so on right?
Thanks again!
Yes, since angles are normalized in PICO-8 we just need those equal portions to separate the angles of rotaton.. this method also allows it to scale properly, evenly distributing any number of orbiters
[Please log in to post a comment]