imageluarotationlove2d

Love2d Rotating an image


I would like to rotate an image in Love2D. I have found a documentation on love2d.org: https://love2d.org/wiki/love.graphics.rotate But I can't seem to get it to work when I try to load an image. Heres my code:

local angle = 0

function love.load()
    g1 = love.graphics.newImage("1.png") 
end

function love.draw()
    width = 100
    height = 100
    love.graphics.translate(width/2, height/2)
    love.graphics.rotate(angle)
    love.graphics.translate(-width/2, -height/2)
    love.graphics.draw(g1, width, height)
end

function love.update(dt)
    love.timer.sleep(10)
    angle = angle + dt * math.pi/2
    angle = angle % (2*math.pi)
end

Could anyone show me an simple example of rotating an image in love2d?


Solution

  • https://love2d.org/wiki/love.graphics.draw

    You may be better off using the fourth argument, shown as 'r' to rotate images, such as:

    love.graphics.draw(image, x, y, math.pi/4)
    

    It's not worth the trouble of using the translate functions for a single draw, and keeping those for when you're batching many draws at once.