I need to render a Sprite rotated 90 degrees to the left, however if I call the roate function before drawing the sprite all my world gets rotated. I’ve tried calling pushMatrix and popMatrix before and after calling rotate and drawing the sprite but I see no difference.
function draw()
background(40, 40, 50)
-- Position of our sprite
spritePos = vec2( WIDTH/2, HEIGHT/2 )
-- Do this in a push/pop matrix block
pushMatrix()
-- Translate to the position first
translate(spritePos.x, spritePos.y)
-- Then rotate
rotate(90)
-- Draw
sprite("Tyrian Remastered:Boss D", 0, 0)
popMatrix()
end
The reason you need to translate before rotating is due to the order of matrix operations.
It’s a little bit unintuitive but you have to imagine the operations happening in the reverse order you set them — it’s as if it rotates around the origin by 90 degrees, then translates to the sprite position (so it appears as if the sprite has rotated around its center at spritePos).
Understand! I need to make my sprite position the center of the world first and then rotate the world before drawing my sprite to finally restore the previous wolrd matrix with a popMatrix! Makes sense! Thank you so much for the help!!
Simeons covered the ground work - I’m still struggling here myself - as Simeon says this is a little unintuitive. There is a good example using mesh() in Codea - the one with the spinning gem pattern. It also includes a roptated shaded triangle fill and really demonstrates the power of the pad and Codea.
I’ll post something later when I get my system running.
In the code Simon posted above, if the pushMatrix and popMatrix where removed would the sprite just rotate by 90 every frame because rotation() is accumulative ?
@rydergaz You could have run the above code and made changes to find out for yourself or you could have read the documentation. But since you asked, it is accumulative but only within each draw cycle. If there was a rotate(90) and a rotate(60), it would only rotate 150. It resets at the start of each draw cycle.