Howdy, Stranger!

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

In this Discussion

Tagged

Sprite rotation
  • enriverdenriverd
    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??

    Thanks in advance!

  • SimeonSimeon
    Posts: 3,477
    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).

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

  • Bri_GBri_G
    Posts: 309

    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

    :)

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