Sprite rotation

Hi guys,

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.

Do you know how to achieve this??

Thanks in advance!

function draw()
    background(40, 40, 50)

    -- Position of our sprite
    spritePos = vec2( WIDTH/2, HEIGHT/2 )

    -- Do this in a push/pop matrix block    
    -- Translate to the position first
    translate(spritePos.x, spritePos.y)

    -- Then rotate

    -- Draw
    sprite("Tyrian Remastered:Boss D", 0, 0)

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!!

Hi enriverd,

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.



And what about moving object? I have about 100 mooving sprites and each I need rotate. Is this posible or not?

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.

I see thanks, I did try it out previously and the sprite only rotated 90, but I didn’t want to jump to conclusions.