[HELP]Rotating circles around the middle and not corner

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

[HELP]Rotating circles around the middle and not corner

Post by nice »

Hello everyone!

I'm currently making a "solarsystem" for fun and to get a better understanding of how Löve2D works.
I've just hit a roadblock in where I try to make my planets rotate around a point that's supposed to be in the middle of the screen and for some reason the rotation is set in the upper left corner of the window.
Is there anyway for my to change the rotations "origin point" to the middle of the screen.

I'm also open for suggestions on how to make the code more efficient
Thanks!

Code: Select all

local angle = 0

function love.load()

  circle = love.graphics.newImage("circle.png")
  circleW = circle:getWidth()
  circleH = circle:getHeight()

  planetX = love.graphics.getWidth()/2
  planetY = love.graphics.getHeight()/2
  sunX = love.graphics.getWidth()/2
  sunY = love.graphics.getHeight()/2

end

function love.update(dt)
  angle = angle - dt * math.pi/2
end

function love.draw()

  -- Sun
  love.graphics.setColor(255, 204, 0)
  love.graphics.draw(circle, sunX, sunY, 0, 1, 1, circleW, circleH)

  love.graphics.rotate(angle)
  -- Mercury
  love.graphics.draw(circle, planetX - 100, planetY, 0, .25, .25, circleW, circleH)


end

:awesome: Have a good day! :ultraglee:
User avatar
SirRanjid
Prole
Posts: 39
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

Re: [HELP]Rotating circles around the middle and not corner

Post by SirRanjid »

That it's rotating around the upper left corner is good because it's the origin(x=0 and y=0). Just translate the origin to the desired position the way you're doing it.

Or if you wanna have rotation and translation in the same line:
planetX = math.cos(angle) * rotation_radius + rotation_center_offsetX
planetY = math.sin(angle) * rotation_radius + rotation_center_offsetY

Where rotation_center_offsetX would be how you initiated planetX in the load function. You can also do this outside the load function.

Next thing you should implement are loops and a table for your planets. So you don't have to have like 2-5 variables per planet floating as upvalues around.

Also I think it's the wrong forum to ask for help here.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: [HELP]Rotating circles around the middle and not corner

Post by ivan »

If you insist on 0,0 being the center of the window:

Code: Select all

w, h = love.graphics.getDimensions()
love.graphics.translate(w/2, h/2)
Also, you probably should offset your circles by a half too:

Code: Select all

love.graphics.draw(circle, sunX, sunY, 0, 1, 1, circleW/2, circleH/2)
Lastly it's always a good idea to program your simulation using predefined units, then convert it to pixels when you want to draw on screen.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: [HELP]Rotating circles around the middle and not corner

Post by nice »

SirRanjid wrote: Fri Jan 12, 2018 3:55 pm That it's rotating around the upper left corner is good because it's the origin(x=0 and y=0). Just translate the origin to the desired position the way you're doing it.

Or if you wanna have rotation and translation in the same line:
planetX = math.cos(angle) * rotation_radius + rotation_center_offsetX
planetY = math.sin(angle) * rotation_radius + rotation_center_offsetY

Where rotation_center_offsetX would be how you initiated planetX in the load function. You can also do this outside the load function.

Next thing you should implement are loops and a table for your planets. So you don't have to have like 2-5 variables per planet floating as upvalues around.

Also I think it's the wrong forum to ask for help here.
I was actually think of using a table for the planets but I wanted to test doing the "dirty" way first and see what happens and I have been looking at the "parametric equation" for a circle but math ain't my strong suit :P
But I'll take a stab at it tomorrow and see if I can get it to work
:awesome: Have a good day! :ultraglee:
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

Re: [HELP]Rotating circles around the middle and not corner

Post by nice »

ivan wrote: Fri Jan 12, 2018 4:04 pm If you insist on 0,0 being the center of the window:

Code: Select all

w, h = love.graphics.getDimensions()
love.graphics.translate(w/2, h/2)
Also, you probably should offset your circles by a half too:

Code: Select all

love.graphics.draw(circle, sunX, sunY, 0, 1, 1, circleW/2, circleH/2)
Lastly it's always a good idea to program your simulation using predefined units, then convert it to pixels when you want to draw on screen.
I just happen to do it "dirty" for now, also I'll keep your suggestions in mind
:awesome: Have a good day! :ultraglee:
Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests