Challenge rotating around a point

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
JimOfLeisure
Prole
Posts: 9
Joined: Wed Dec 29, 2021 8:01 pm

Challenge rotating around a point

Post 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 .
Attachments
bumpy-road.love
(30.78 KiB) Downloaded 131 times
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Challenge rotating around a point

Post 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.
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: Challenge rotating around a point

Post 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 :)
User avatar
Gunroar:Cannon()
Party member
Posts: 1139
Joined: Thu Dec 10, 2020 1:57 am

Re: Challenge rotating around a point

Post by Gunroar:Cannon() »

Nobody:
pgimeno:
Image
*You were close*

Nice :)
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
kicknbritt
Citizen
Posts: 96
Joined: Sat May 30, 2015 2:15 am
Location: Chicago, IL/Anchorage,AK

Re: Challenge rotating around a point

Post by kicknbritt »

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

Nice :)
Man is a legend
"I AM THE ARBITER!!!" *Pulls out Energy sword and kills everything*
User avatar
Gunroar:Cannon()
Party member
Posts: 1139
Joined: Thu Dec 10, 2020 1:57 am

Re: Challenge rotating around a point

Post by Gunroar:Cannon() »

kicknbritt wrote: Tue Jan 11, 2022 6:35 pm Man is a legend
Who, pgimeno or Escanor? :crazy:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: Challenge rotating around a point

Post 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
JimOfLeisure
Prole
Posts: 9
Joined: Wed Dec 29, 2021 8:01 pm

Re: Challenge rotating around a point

Post 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.
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Challenge rotating around a point

Post by darkfrei »

And if I need the scale too?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Challenge rotating around a point

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 3 guests