Rotate From Center

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
DeadInternal
Prole
Posts: 9
Joined: Fri May 25, 2012 1:53 am

Rotate From Center

Post by DeadInternal »

I've been working on making a image rotate to face my mouse. After a while I finally got this to work, but I noticed something strange. I rotate it by doing this.

Code: Select all

function love.draw()
love.graphics.draw(turtle, turtlex, turtley, angletomouse, .1, .1)
--More code
end
Where 'turtle' is a pre made image, turtlex is the x axis and turtley is the y axis of the image. angletomouse is the angle that the image rotates.

For whatever reason, the image is rotated by a corner, not the center of the image causing the image to look strange when rotating up close.

Here's the full script(This is a testing script so the variables won't make much sense with the pictures and such)

Code: Select all

function love.load()
--Set a few basic stuff--
love.graphics.setBackgroundColor(50,50,50)

--Define a load of variables--
turtle = love.graphics.newImage("BLACK-MAGE.jpg")
turtlex = 50
turtley = 50
movespeed = 500
--Background music--
end

function love.update(dt)
--Sets mouse variables--
mousex = love.mouse.getX()
mousey = love.mouse.getY()
angletomouse = math.atan2(turtley-mousey,turtlex-mousex)
--Arrow keys--
if love.keyboard.isDown("right") then
turtlex = turtlex + (movespeed * dt)
elseif love.keyboard.isDown("left") then
turtlex = turtlex - (movespeed * dt)
end

if love.keyboard.isDown("down") then
turtley = turtley + (movespeed * dt)
elseif love.keyboard.isDown("up") then
turtley = turtley - (movespeed * dt)
end

--ASWD keys--
if love.keyboard.isDown("d") then
turtlex = turtlex + (movespeed * dt)
elseif love.keyboard.isDown("a") then
turtlex = turtlex - (movespeed * dt)
end

if love.keyboard.isDown("s") then
turtley = turtley + (movespeed * dt)
elseif love.keyboard.isDown("w") then
turtley = turtley - (movespeed * dt)
end

--Quit the game with esc--
if love.keyboard.isDown("escape") then
love.event.quit()
end

end

function love.draw()
--Draw the turtle--
love.graphics.draw(turtle, turtlex, turtley, angletomouse, .1, .1)
love.graphics.print(angletomouse, 250, 0)

--Redefine the player variable--
player = {xaxis=turtlex, yaxis=turtley, width=turtle:getWidth(), height=turtle:getHeight()}
love.graphics.setColor(255, 255, 255)

end
Attachments
TestingRotation.love
Testing rotation file.
(10.72 KiB) Downloaded 155 times
- Active guitarist
- Active programmer
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Rotate From Center

Post by Lynesth »

Hi. You just have to add some origin offset in your love.graphics.draw()
love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
Those we care about are ox and oy, which, for what you wanna do, have to be equal to half your image's width and height respectively.

So it would be more something like :

Code: Select all

--In love.load
turtleW = turtle:getWidth()
turtleH = turtle:getHeight()

--In love.draw
love.graphics.draw(turtle, turtlex, turtley, angletomouse, .1, .1, turtleW/2, turtleH/2)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
User avatar
DeadInternal
Prole
Posts: 9
Joined: Fri May 25, 2012 1:53 am

Re: Rotate From Center

Post by DeadInternal »

Thanks! I guess I should pay more attention to the arguments in things like that.
- Active guitarist
- Active programmer
User avatar
Lynesth
Prole
Posts: 30
Joined: Sun May 16, 2010 8:47 am

Re: Rotate From Center

Post by Lynesth »

You're welcome ;)
I'm always happy to be corrected if needed. I still have a lot to learn.
By the way, excuse my english.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest