Rotating an image on its center

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
- AnTo MégA -
Prole
Posts: 1
Joined: Thu Jun 02, 2022 2:10 pm

Rotating an image on its center

Post by - AnTo MégA - »

Hello,

I drew an image and rotated it by increasing its rotation by 1 degree converted in radians every second, or at least that's what I think I am doing feel free to correct me on that if I am wrong

Here's my code for that so that you can see better :

Code: Select all

local data = {}

function love.load()
  data.sprite = love.graphics.newImage("Lizard.png")

  data.width = data.sprite:getWidth()
  data.height = data.sprite:getHeight()
  data.x = love.graphics.getWidth() / 2 - data.width / 2
  data.y = love.graphics.getHeight() / 2 - data.height / 2
  data.rotation = math.rad(0)
  data.scaleX = 1
  data.scaleY = 1
  data.originX = data.x + data.width / 2
  data.originY = data.y + data.height / 2
end

function love.update(dt)
  data.rotation = data.rotation + 1 * dt
end

function love.draw()
  love.graphics.setColor(1, 1, 1)
  love.graphics.draw(data.sprite, data.x, data.y, data.rotation, data.scaleX, data.scaleY, data.originX, data.originY)

  -- Rectangle Hitbox
  love.graphics.rectangle('line', data.x, data.y, data.width, data.height)

  -- Center
  love.graphics.circle('fill', data.x + data.width / 2, data.y + data.height / 2, 4)
end
So with that the image rotates around its top-left corner (it goes off the screen when over, under and to the left of its original position however does not go that far when to the right, stays visible), even though I drew it with with its x and y origins being at its center.
Also it rotates far from it, like I drew the rectangular hitbox and a small circle at the center and it is shwown and rotates at a distance way outside of it, and I don't know why
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Rotating an image on its center

Post by ReFreezed »

The origin is in image space, not screen space, and determines what position in the image should render at the specified xy coordinates. The default origin is (0, 0) which refers to the top left corner of the image - you want the origin to be the center of the image, i.e. (data.width/2, data.height/2). Change originX=data.x+data.width/2 to just originX=data.width/2, and same with originY, to draw the image centered at the specified xy coordinates.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Rotating an image on its center

Post by pgimeno »

edit: oops, ReFreezed had everything covered
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Rotating an image on its center

Post by darkfrei »

pgimeno wrote: Fri Jun 03, 2022 12:16 am edit: oops, ReFreezed had everything covered
Please keep the explanation, two explanations is 20% better than just one :)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Rotating an image on its center

Post by pgimeno »

What I said is that the position is where you place the origin, not where you place the top left corner, so in addition to that change you also need this one:

Code: Select all

-- Change this:
  data.x = love.graphics.getWidth() / 2 - data.width / 2
  data.y = love.graphics.getHeight() / 2 - data.height / 2
-- to this:
  data.x = love.graphics.getWidth() / 2
  data.y = love.graphics.getHeight() / 2
juankax
Prole
Posts: 3
Joined: Mon Jun 20, 2022 3:53 pm

Re: Rotating an image on its center

Post by juankax »

hello pgimeno,
only left corner??
Founder of Eventos BCN and Chofer BCN
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests