Howdy, Stranger!

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

I forgot how to use translate/rotate

Hey, so yeah I can’t remember how to use these lol. I’m trying to animate my player sprite by rotating it but when I rotate it at a 45 degree angle, it rotates from whatever center point i don’t want. I can’t remember how I use translation I believe to correct this so it rotates around the center of the sprite.

Thanks guys!

Tagged:

Comments

  • SimeonSimeon Admin Mod
    Posts: 4,958

    Here's an example where a sprite rotates under your finger wherever you touch the screen

    Basically translate to the position, rotate, translate back

    -- RotationAroundPoint
    
    function setup()
    end
    
    function draw()
        background(40, 40, 50)
    
        local pos = CurrentTouch.pos
    
        rotateAround(pos, ElapsedTime * 100)
        sprite("Tyrian Remastered:Boss D", pos.x, pos.y)
    
    end
    
    function rotateAround(pos, angle)
        translate(pos.x, pos.y)
        rotate(angle)
        translate(-pos.x, -pos.y)
    end
    
  • Posts: 1,229
    @Programmer1500 - also remember rotate is additive, so multiple rotates in a draw function add up. Your not starting from the zero poi t with each rotation - its from the last angle you used.
  • dave1707dave1707 Mod
    Posts: 7,606

    @Programmer1500 Here's an example using tween.

    displayMode(FULLSCREEN)
    
    function setup()
        tw={x=0,y=HEIGHT/2,rot=0}
        tween(5,tw,{x=WIDTH,y=HEIGHT/2,rot=720},
                    {easing=tween.easing.linear,loop=tween.loop.pingpong})
    end
    
    function draw()
        background(40, 40, 50)
        translate(tw.x,tw.y)
        rotate(tw.rot)
        sprite("Cargo Bot:Clear Button")
    end
    
Sign In or Register to comment.