Page 1 of 1

[HELP]Rotating circles around the middle and not corner

Posted: Fri Jan 12, 2018 2:41 pm
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


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

Posted: Fri Jan 12, 2018 3:55 pm
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.

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

Posted: Fri Jan 12, 2018 4:04 pm
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.

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

Posted: Fri Jan 12, 2018 5:40 pm
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

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

Posted: Fri Jan 12, 2018 5:43 pm
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