(Solved) Player screen X and Y giving wrong numbers when zoomed in

SOLUTION:

If you are using a map zoom plugin like MultiTweaks or Arthran's port of the Zoom function of it and have this same issue, here is the solution:

Instead of just using the screenX and screenY, you need to use:

If screen is 1248x720, tile size is 16 and zoom ratio is x3:
$gamePlayer.screenX() is actually: (($gamePlayer.screenX() - 624) / (16/3) * 16) + 624
$gamePlayer.screenY() is actually: (($gamePlayer.screenY() - 360) / (16/3) * 16) + 360


If using any other zoom ratio or screen res, update values accordingly:
(($gamePlayer.screenX() - (screenHeight/2)) / (tileSize/zoomFactor) * tileSize) + (screenHeight/2)
(($gamePlayer.screenY() - (screenWidth/2)) / (tileSize/zoomFactor) * tileSize) + (screenWidth/2)


The game registers the center coordinates (half screen size X and Y) correctly, but only moves left/right/up/down each tile by your tile size / your zoom ratio, rather than your full tile size. This math calculates the missing amount and adds it, resolving the problem keeping the X and Y relative to your actual player location on screen.

==================================================

Hey folks,

I am trying to get the player's coordinates on the screen so that I can render some images relative to the player sprite even when they are not centered on screen. I figured that $gamePlayer.screenX() and $gamePlayer.screenY() would give my exact position on the screen, however this doesn't seem to be working with the map zoom set to 3x using MultiTweaks (or any other means of zooming in that I have tested).

In this screenshot, the NPC I am talking to shows text with those 2 variables (screen X and Y), but the numbers don't make sense. When I render something using those variables, it is still generally around the center of the screen even when I am down in the corner like in the pic.

Example:
xy.JPG


The screen is 1248x720, so this coordinate doesn't make sense. It does look about right when I am centered, and it changes as I move out of center, but not to my actual position on the screen.

If I turn off my zoom plugin, things work properly:
test2.JPG



Any idea what could be going on here?

Thanks!

Update:
Here is a demo project showcasing the issue:
DOWNLOAD
Unless I'm mistaken : $gamePlayer.screenX() and $gamePlayer.screenY() are not affected by the zoom. (Unless taken into account by the plugin)
Meaning they will return the data of the "unzoomed" map.

You would need to apply the zoom factor to those coordinates.

So for example, if you zoom x 3, you would need to apply a /3 to screenX and screenY to return the actual data.
Since everything is 3 times bigger, in both X and Y scale, you need to divide the real X and Y screen value by 3 to "scale it" to the zoom.

However on your 1st image it shows X 472 Y 400, which makes no sens since the character is almost on the edge of the left side of the screen.

Try moving 1 square to the right and see what the difference in X is
If you are using the custom move system, X should be 48 more in normal conditions,
but if the zoom x 3 is taken into account X should be only 16 more.

Looks like some debug is needed, I'm unsure how to solve this right now.
If it update correctly we can try and debug this further, otherwize there is an issue in the logic (probably the plugin)
Unless I'm mistaken : $gamePlayer.screenX() and $gamePlayer.screenY() are not affected by the zoom. (Unless taken into account by the plugin)
Meaning they will return the data of the "unzoomed" map.

You would need to apply the zoom factor to those coordinates.

So for example, if you zoom x 3, you would need to apply a * 3 to screenX and screenY to return the actual data.

However on your 1st image it shows X 472 Y 400, which makes no sens since the character is almost on the edge of the left side of the screen.

Try moving 1 square to the right and see the difference in X
If you are using the custom move system, X should be 48 more, if the zoom x 3 is taken into account X should be 144 more.
Looks like some debug is needed, I'm unsure how to solve this right now.

Here are some more screenshots at consistent coordinates to help troubleshoot:

Zoomed:
Spoiler
pos1.JPG

pos2.JPG

pos3.JPG

pos4.JPG


Non-Zoomed:
Spoiler
apos1.JPG
apos2.JPG
apos3.JPG
apos4.JPG

The crazy thing is that my game is using a 16x16 tilemap, and in both cases, moving one space does change the value by 16. It's just that the values themselves make no sense.

If I set an image to render at player X +100 and player Y -100, it will always render in that location relative to my character when not zoomed in, but when zoomed it is always somewhere sort of around the middle of the screen.
Unzoomed seem to be the accurate coordinates, so the issue lies in the zoom method and how it calculates the X and Y positions.

Can you try and put your character to the utmost right position (when zoomed), I have a feeling its going to show more than the actual X size of the map.
The coordinates are updating wrong as it should increase by 16/3 when zoomed.

I also suspect X +100 and Y -100 to be "scaled" based on the zoom lvl.
Can you try a x2 zoom and see if instead of in the middle it lays between the player and the middle?
Also, does it stay in the middle even when your player is on the right side of the screen?

Maybe share a video of gameplay with your image active to see how it behaves when you move.


(Sorry if I ask alot, I'm trying to understand the issue)
Unzoomed seem to be the accurate coordinates, so the issue lies in the zoom method and how it calculates the X and Y positions.

Can you try and put your character to the utmost right position (when zoomed), I have a feeling its going to show more than the actual X size of the map.
The coordinates are updating wrong as it should increase by 16/3 when zoomed.

I also suspect X +100 and Y -100 to be "scaled" based on the zoom lvl.
Can you try a x2 zoom and see if instead of in the middle it lays between the player and the middle?
Also, does it stay in the middle even when your player is on the right side of the screen?

Maybe share a video of gameplay with your image active to see how it behaves when you move.


(Sorry if I ask alot, I'm trying to understand the issue)

Here are some videos with a test image locked to the player X/Y. As you can see, it is weird with the zoom haha.



I tried multiplying or dividing the X/Y by 3, but neither worked.

Here are some screenshots at 2x zoom:

Spoiler
b1.JPG
b2.JPG
b3.JPG
b4.JPG

The 4th one is centered, and shows the expected value (my screen center +16 X)

Utmost right screenshot with 3x zoom:

Spoiler
c1.JPG

The fish image shows where it thinks my character supposedly is. Very odd.
Not at the comp at the moment - If I recall, your .screenX() and .screenY() is based off of the current canvas and not the map.

Have you tried
Code:
$gamePlayer._realX * $gameMap.tileWidth()
?
I see now.

The picture position is actually fine when zoomed, but whenever you reach a border (top, bottom, left, right) the image is being offset.
This happens because the "camera" doesn't move anymore, while the player is moving.

In the beginning of the video (zoomed) your picture shows where you want it to be, but as you reach the bottom left, it starts to offset.
If you just run around the center of the map, the picture's position stays where you want it, correct?

You might want to scale the picture up when zoomed. ^^ (if you zoom x 3, scale the picture x 3)

I suspect it is setting the position of the picture based on its size, so since the picture is 3 times smaller compared to the player and everything else when zoomed, this might be the issue.
Not at the comp at the moment - If I recall, your .screenX() and .screenY() is based off of the current canvas and not the map.

Have you tried
Code:
$gamePlayer._realX * $gameMap.tileWidth()
?

This is what it does with that:



Still weird, but a different weird! Haha


I see now.

The picture position is actually fine when zoomed, but whenever you reach a border (top, bottom, left, right) the image is being offset.
This happens because the "camera" doesn't move anymore, while the player is moving.

In the beginning of the video (zoomed) your picture shows where you want it to be, but as you reach the bottom left, it starts to offset.
If you just run around the center of the map, the picture's position stays where you want it, correct?

You might want to scale the picture up when zoomed. ^^ (if you zoom x 3, scale the picture x 3)

I suspect it is setting the position of the picture based on its size, so since the picture is 3 times smaller compared to the player and everything else when zoomed, this might be the issue.

When zoomed, it is fine when exactly in the center of the screen. Its position becomes more "off" as I stray further from the center of the screen when I get near any map edges.

The size doesn't have any effect - it is just a bigger picture moving around the same way.
This is what it does with that:



Still weird, but a different weird! Haha


Try :
JavaScript:
$gamePlayer._realX * ($gameMap.tileWidth() / zoomfactor)

I saw Multitweaks was sizing tileWidth according to the zoom
So /2 if zoom is 2 and /3 if zoom is 3, etc...
Try :
JavaScript:
$gamePlayer._realX * ($gameMap.tileWidth() / zoomfactor)

I saw Multitweaks was sizing tileWidth according to the zoom
So /2 if zoom is 2 and /3 if zoom is 3, etc...

That doesn't work unfortunately.

I've put together a demo project to make testing a bit easier:

Ehm..
1721841488479.png

Huh, that's weird. it runs fine for me.

Maybe it zipped up weird or something.

Here is a re-upload:
DOWNLOAD
My bad, it's been a while since I updated my MZ xD

So my basic idea was correct, when you get to a border, the image position is moving 3x slower than the player, meaning this must be adjusted.

Here is a screenshot in normal conditions
1721844095292.png


And here is a screenshot when one step more south :
1721844136635.png


As you can see, the Y position of the player increased by 16 (TileHeight), but the image position changed by 16/3, or 5.33

This seem to be controlled by the zoomFactor

I'll check what I can find to determine if the camera is moving or not and adapt this behavior based on if it does or not.
My bad, it's been a while since I updated my MZ xD

So my basic idea was correct, when you get to a border, the image position is moving 3x slower than the player, meaning this must be adjusted.

Here is a screenshot in normal conditions
View attachment 312453

And here is a screenshot when one step more south :
View attachment 312455

As you can see, the Y position of the player increased by 16 (TileHeight), but the image position changed by 16/3, or 5.33

This seem to be controlled by the zoomFactor

I'll check what I can find to determine if the camera is moving or not and adapt this behavior based on if it does or not.

Thanks for looking into it!

So it seems that it doesn't need its ccords multiplied by the zoom factor, but more like:
((Center X/Y + (distance from the center in tiles) * (16 / Zoom factor) ).. or something?
Double posting because I solved it, and want to bump with the solution!

This works:
If screen is 1248x720, tile size is 16 and zoom ratio is x3:
$gamePlayer.screenX() is actually: (($gamePlayer.screenX() - 624) / (16/3) * 16) + 624
$gamePlayer.screenY() is actually: (($gamePlayer.screenY() - 360) / (16/3) * 16) + 360


If using any other zoom ratio or screen res, update values accordingly:
(($gamePlayer.screenX() - (screenHeight/2)) / (tileSize/zoomFactor) * tileSize) + (screenHeight/2)
(($gamePlayer.screenY() - (screenWidth/2)) / (tileSize/zoomFactor) * tileSize) + (screenWidth/2)


Thanks to @Artille for doing the real work!
Thanks for helping to solve this xD

I was trying to actually rewrite some of the plugin functions. 😊😊

I'm not sure how you got the right formula though, care to explain?


Edit: The main post explanations are very usefull! Thanks for explaining.
Thanks for helping to solve this xD

I was trying to actually rewrite some of the plugin functions. 😊😊


I'm not sure how you got the right formula though, care to explain? 😮

It was thanks to you figuring out that instead of moving the full 16px as it should, it was moving 5.33 (or tileHeight / zoomRatio).

If being 10 tiles away from the center would normally mean moving 10 * 16 px, and it was only actually adding 10 * 5.33 instead, then all that was needed was to figure out what that missing amount was and adding it to the player X.

So, if I am 10 steps left from the center, and the game still registers the center value properly as half of my screen width (624), but says I am now at x = 677 when it should say 784 (moving me 53.3 instead of 160 px), then I can subtract 624 to find out how many tiles I moved from center and then figure out the missing value by just multiplying that by 3 (correcting the weird math) and then adding the 624 back.

Hopefully that makes sense, haha. In short, it is just figuring out the missing amount that was "supposed" to be added by doing some math to figure out how farm from center we've moved, then adding it.
It was thanks to you figuring out that instead of moving the full 16px as it should, it was moving 5.33 (or 16 / zoomRatio).

If being 10 tiles away from the center would normally mean moving 10 * 16 px, and it was only actually adding 10 * 5.33 instead, then all that was needed was to figure out what that missing amount was and adding it to the player X.

So, if I am 10 steps left from the center, and the game still registers the center value properly as half of my screen width (624), but says I am now at x = 677 when it should say 784 (moving me 53.3 instead of 160 px), then I can subtract 624 to find out how many tiles I moved from center and then figure out the missing value by just multiplying that by 3 (correcting the weird math) and then adding the 624 back.

Hopefully that makes sense, haha. In short, it is just figuring out the missing amount that was "supposed" to be added by doing some math to figure out how farm from center we've moved, then adding it.
Makes sens ^^
I'm still going to take a look at the plugin to see if I can make it work normally without the need to rewrite the values.

Was fun working on! 😊
Makes sens ^^
I'm still going to take a look at the plugin to see if I can make it work normally without the need to rewrite the values.

Was fun working on! 😊

Yeah, it sounds like the plugin might just be setting screenX() weirdly somewhere, causing the issue. This at least gives some context to what the plugin error is if you can find it!

I am surprised I even figured out an equation to solve for it, I am not a math guy lol.

Original thread: https://forums.rpgmakerweb.com/threads/solved-player-screen-x-and-y-giving-wrong-numbers-when-zoomed-in.170579/