2
votes

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?

5

5 Answers

6
votes

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

You might 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 also a fairly bad idea to use the translate functions, you're much better off giving an x and y position for each image, unless you are doing something like offsetting an entire map, in which case it could save computation time (Or at least the length and complexity of your code). (Of course there are other situations where it comes in very useful).

3
votes

Your code worked perfectly for me, aside from a small unrelated issue (love.timer.sleep uses seconds in LÖVE 0.8.0).

We will be able to help you better, and perhaps reproduce your error, if you provide us with more information.
When you say

I can't seem to get it to work when I try to load an image

..what is the result? Is the image a white box? Does the application crash? Is there nothing on the screen?

All of these imply a image loading issue, rather than a rotation issue. Although, it could be the case that the image is rotating off of the screen.


If you continue to use translate, rotate, and scale (which is usually a good idea), I recommend you take a look at the push and pop functions.
They allow you to 'stack' transformations so you can render sub elements.

Example uses are rendering a GUI (each child pushes its translation and then renders the children) and drawing sprites on a scrolling map (the camera translates the entire map and then does for entity in entities do push() entity:draw() pop() end. Each entity can translate and rotate in local coordinates (0,0 = centre of sprite)).

0
votes
love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )

the R is the rotation.. why don't you just set it to a variable and change it as you please? ... I'm new to programming so I may be wrong but this is how I would do it.

0
votes

Example of rotating at center of image using LOVE 11.3 (Mysterious Mysteries):

function love.draw()
    love.graphics.draw(img, 400,300, wheel.r, wheel.sx, wheel.sy, wheel.w / 2,  wheel.h / 2)
end

function love.update(dt)
    wheel.r = wheel.r + dt  
end

function love.load()
    wheel = {x = 0, y = 0, w = 0, h = 0, sx = 0.5, sy = 0.5, r = 0, image = "wheel.png"}
    img = love.graphics.newImage(wheel.image)
    wheel.w = img:getWidth()
    wheel.h = img:getHeight()               
end
0
votes

Normaly the axis for rotating is the upper left corner. To center the axis to the middle of an image you have to use the parameters after the r parameter to half of width and half of height of the image.