Page 1 of 1

Rotate From Center

Posted: Thu May 31, 2012 3:02 am
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

Re: Rotate From Center

Posted: Thu May 31, 2012 3:51 am
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)

Re: Rotate From Center

Posted: Thu May 31, 2012 4:00 am
by DeadInternal
Thanks! I guess I should pay more attention to the arguments in things like that.

Re: Rotate From Center

Posted: Thu May 31, 2012 4:34 am
by Lynesth
You're welcome ;)