Page 1 of 2

Challenge rotating around a point

Posted: Tue Jan 11, 2022 3:38 am
by JimOfLeisure
I see this is a common question, and I've looked at the wiki and a bunch of forum posts, but for some reason I can't figure this out.

This is a physics simulation where a ball rolls down a bumpy hill, and the viewer can change the angle of the hill. But really the ground is level, and only a camera and gravity are rotating. So the ball just travels indefinitely towards a greater X and in a small range of Y.

I'm trying to center the scene rotation on the ball, but I can't manage it. I can only make it rotate from x=0,y=0 or from the top-left of the translated screen. In the former case the ground diverges away from the camera quickly.

The .love is attached. I am using a self-made camera module that just does some translation and rotation before the ball and current ground segments are drawn. Here is a snippet of that. What blows my mind is that it only kind-of works if rotate is first...I would think the translate (which keep in mind may stretch X towards infinity) would come before the rotation, then the second translate to shift the scene so the ball–which will be drawn at camera.x, camera.y–is a bit off-center. But that's when things get really messed up.

Code: Select all

camera = {
    x = 0,
    y = 0
}

local graphics = love.graphics
local push = graphics.push
local pop = graphics.pop
local translate = graphics.translate
local rotate = graphics.rotate
local scale = graphics.scale
local x_offset = graphics.getWidth() / 2 - 100
local y_offset = graphics.getHeight() / 2 - 100

function camera:set(angle)
    push()
    rotate(-(angle - (math.pi / 2)))
    translate(-self.x, -self.y)
    translate(x_offset,y_offset)
end

function camera:unset()
    pop()
end

return camera
A slightly older version that is no different with respect to the camera and ball is playable online at https://jimofleisure.itch.io/bumpy-road .

Re: Challenge rotating around a point

Posted: Tue Jan 11, 2022 5:17 pm
by kicknbritt
Yea camera rotation, movement and zoom stuff is usually pretty nasty to deal with. (And also tends to get worse the more you try to fix it lol)
I spent a few minutes looking at your code and thought maybe I could use my camera library to do it, but I guess mine isn't
able to rotate about a point either. Have you tried looking for a camera library for love2d?
There are tons of them out there, I'm sure one has the functionality you need.

Re: Challenge rotating around a point

Posted: Tue Jan 11, 2022 5:26 pm
by pgimeno

Code: Select all

function camera:set(angle)
    push()
    translate(x_offset,y_offset)
    rotate(-(angle - (math.pi / 2)))
    translate(-self.x, -self.y)
end
You were close :)

Re: Challenge rotating around a point

Posted: Tue Jan 11, 2022 6:17 pm
by Gunroar:Cannon()
Nobody:
pgimeno:
Image
*You were close*

Nice :)

Re: Challenge rotating around a point

Posted: Tue Jan 11, 2022 6:35 pm
by kicknbritt
Gunroar:Cannon() wrote: Tue Jan 11, 2022 6:17 pm Nobody:
pgimeno:
Image
*You were close*

Nice :)
Man is a legend

Re: Challenge rotating around a point

Posted: Tue Jan 11, 2022 6:38 pm
by Gunroar:Cannon()
kicknbritt wrote: Tue Jan 11, 2022 6:35 pm Man is a legend
Who, pgimeno or Escanor? :crazy:

Re: Challenge rotating around a point

Posted: Tue Jan 11, 2022 7:12 pm
by pgimeno
But he really was close!

Original code (comment added):

Code: Select all

function camera:set(angle)
    push()
    rotate(-(angle - (math.pi / 2)))
    translate(-self.x, -self.y)
    translate(x_offset,y_offset) -- this just needs to be moved up
end
Fixed code:

Code: Select all

function camera:set(angle)
    push()
    translate(x_offset,y_offset) -- moved here from the bottom
    rotate(-(angle - (math.pi / 2)))
    translate(-self.x, -self.y)
end

Re: Challenge rotating around a point

Posted: Wed Jan 12, 2022 6:23 am
by JimOfLeisure
pgimeno wrote: Tue Jan 11, 2022 5:26 pm

Code: Select all

function camera:set(angle)
    push()
    translate(x_offset,y_offset)
    rotate(-(angle - (math.pi / 2)))
    translate(-self.x, -self.y)
end
You were close :)
Thanks! That did it!

It makes sense now that I/you did it, but that seems completely backwards from what I originally expected. That does exactly what I wanted.

Re: Challenge rotating around a point

Posted: Wed Jan 12, 2022 9:22 am
by darkfrei
And if I need the scale too?

Re: Challenge rotating around a point

Posted: Wed Jan 12, 2022 12:49 pm
by zorg
JimOfLeisure wrote: Wed Jan 12, 2022 6:23 am
pgimeno wrote: Tue Jan 11, 2022 5:26 pm

Code: Select all

function camera:set(angle)
    push()
    translate(x_offset,y_offset)
    rotate(-(angle - (math.pi / 2)))
    translate(-self.x, -self.y)
end
You were close :)
Thanks! That did it!

It makes sense now that I/you did it, but that seems completely backwards from what I originally expected. That does exactly what I wanted.
You move the coordinate system instead of the object, basically; the result of that first transform is going to be that (0,0) will be at the point of rotation; since the rotate function rotates around (0,0), it will now work fine.