#Global positions stop holding up if camera has a zoom?

1 messages · Page 1 of 1 (latest)

cyan slate
#

Hey guys, I've been stuck on something for over a day now. I have a UI canvas layer that follows the camera, and another object in the game (on its own canvas layer). It's basically a ux layer, and a layer containing game objects, including cards. I want a ux element to follow the card on the screen.

I've got it mostly working, but for some reason when the camera's zoom isn't 1, the ux element becomes offset from the card, depending on the zoom amount?! I'm very lost and have tried a lot of stuff. Has anyone come across this before? Does somebody that knows more know what's up? This is the process code attached to a "cardFollower" node (its position is meant to follow the position of the card):

        + get_viewport_rect().size/2 \
        - (cam.get_screen_center_position() * cam.zoom.x)```
#

this is what that looks like

#

it follows well, it's just the offset (that I think is coming from the second line of code, the viewport rect one) isn't properly considering cam zoom. Just multiplying it by cam.zoom.x doesn't seem to work? am I missing something?

cyan slate
#

WOAHHHH I GOT IT

#
                    - cam.get_screen_center_position() * cam.zoom.x \
                    + get_viewport_rect().size/2```
#

so I just did the math backwards from scratch a 100th time and this time i finally like, understood why I was doing it

#

so you get the card's global_position and subtract screen center - then I had godot print out that cardpos - screenCenter

#

and when you zoom in, the edge of the screen is a smaller global position than before! Obviously I guess? So on zoom = 1, when you move the card to the left edge of the screen, the distance from screen center to the card in global (in x) is 600

#

but when you zoom in to 1.5, and move the card to the edge of the screen, the global distance from screen center to edge of screen is now 400!

#

so take that whole 400 and multiply it by zoom, to make it 600 again

#

(cardpos - screenCenter) * camZoom

#

then just add back the viewport rect because the camera's coordinate system has 0,0 in the top left, and the global position is from the center of the screen

#

and bing bang boom that works!!