#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Sprite rotation

edited April 2012 in General Posts: 5

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

Tagged:

Posts: 5,387
``````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).

• Posts: 5

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

• Posts: 1,768

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.

Bri_G

• Posts: 97

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

• Posts: 35

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 ?

• Mod
Posts: 8,505

@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.

• Posts: 35
I see thanks, I did try it out previously and the sprite only rotated 90, but I didn't want to jump to conclusions.