3

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?

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
Dallox
  • 487
  • 5
  • 8
  • 19

5 Answers5

8

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.

Ashley Davies
  • 1,873
  • 1
  • 23
  • 42
  • 1
    "It's also a fairly bad idea to use the translate functions" - on what grounds do you claim this? – Deco May 23 '12 at 07:19
  • 1
    I recommend removing that part of the answer. The translate functions work very well. They are just more cumbersome to draw images than just using the parameters in ´love.graphics.draw´. – kikito May 23 '12 at 15:42
  • I did mention there's a lot of times it comes in useful. I'm simply pointing out it can muck up a lot of things if you're offsetting the entire coordinate system. – Ashley Davies May 23 '12 at 19:06
  • Of course, if you're using push and pop as Deco suggests, then there's no real difference and it could make your code a lot simpler. – Ashley Davies May 23 '12 at 19:08
3

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

Deco
  • 5,112
  • 1
  • 16
  • 17
  • The program just crashes. You see one second the image and then it crashes. And i will look into push and pop – Dallox May 23 '12 at 09:49
  • Eventually i got it to work, but alil diffrent i used this: http://pastebin.com/DjQNej9e But for some reason when i want to keep rotating the image it simply crashes – Dallox May 23 '12 at 10:10
  • `while true do stuff end` will freeze your program. The loop will run for ever and it will never reach the end of your `love.draw` function, meaning an image will never be presented to the screen (and your application will lock up). – Deco May 24 '12 at 03:07
1
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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RyHam
  • 11
  • 1
0

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
Exel Gamboa
  • 936
  • 1
  • 14
  • 25
0

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.

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15