Fetch map (pixel) coordinates of screen coordinates?

Kronicide

Veteran
Veteran
Joined
Jul 7, 2020
Messages
39
Reaction score
18
First Language
Swedish
Primarily Uses
RMMV
Hi!

I'm wondering if there is a simple way of, in a plugin, getting the map (pixel) coordinates of a point on the screen? Let's say that the map is larger than the screen, and that the map extends up and to the left outside of the screen. Then 0, 0 (upper left corner) on the screen may be, say, coordinates 123, 45 relative to the upper left corner of the map. Can I fetch those map coordinates somehow, when I have the screen coordinates?

What I want to do is fix a sprite on the map as it scrolls, without using RMMV's built-in stuff (because I'm trying to do some low-level things). I suspect that I can do this directly with PIXI and container properties, maybe?

Thanks!
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,707
Reaction score
7,365
First Language
German
Primarily Uses
RMMV
Please check the game data options of the control variable command. There are two there that will help you (and you can look up their script equivalents if needed in a plugin), but you have to be carefull with your nomenclature as the way the engine works is different than what you wrote above.

The map xy of control variable command gives you the grid coordinates of the map grid, it will NOT give you pixel numbers.
The screen xy of control variable command will give you pixel coordinates for whatever object you're refering to, based on 0,0 top left of the screen.

from there it is basically mathematics - a grid is 48x48 pixel by default (unless you changed that with a plugin) and you can multiply or add in whatever way to get the numbers you need.
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,492
Reaction score
408
First Language
German
Primarily Uses
RMMV
I believe i ran into that in the past while i tryed to manualy Stick Pictures to the Map(But it never worked as good as the Plugin). But maybe you need it for something else. To get this you need to do:

1. Put an Empty Event in the Top left corner of your Map.
2. Set its Trigger to Parallel.
3. Eventcode: SetVar1 = Screen X of this Event, Var2 = Screen Y of this Event.
4.Start testplay move right or down until map is scrolling. Press F9 and take a look at the Variables Values.
5. The code in the Event continues:
Var1 += 1200 (The Pixel Position where you want it on the Map).
Var2 += 48
Show Picture x = Var1 ; y = Var2

Code:
SetVar1 = Screen X of this Event, Var2 = Screen Y of this Event.
Var1 += 1200  (The Pixel Position where you want it on the Map).
Var2 += 48
Show Picture  x = Var1 ; y = Var2
Iam curious for what thing you want to use this?

Edit: I hope i remembered correctly.
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
942
Reaction score
497
First Language
Javascript
Primarily Uses
RMMZ
Hi!

I'm wondering if there is a simple way of, in a plugin, getting the map (pixel) coordinates of a point on the screen? Let's say that the map is larger than the screen, and that the map extends up and to the left outside of the screen. Then 0, 0 (upper left corner) on the screen may be, say, coordinates 123, 45 relative to the upper left corner of the map. Can I fetch those map coordinates somehow, when I have the screen coordinates?

What I want to do is fix a sprite on the map as it scrolls, without using RMMV's built-in stuff (because I'm trying to do some low-level things). I suspect that I can do this directly with PIXI and container properties, maybe?

Thanks!
Should be as simple as...
Code:
$gameMap.displayX()*$gameMap.tileWidth()
$gameMap.displayY()*$gameMap.tileHeight()
Does this help?
CTB_GetDisplayCooridinates.js
 

Attachments

Last edited:

Kronicide

Veteran
Veteran
Joined
Jul 7, 2020
Messages
39
Reaction score
18
First Language
Swedish
Primarily Uses
RMMV
Thanks for the responses, much appreciated! I'm writing a plugin, so this needs to work for arbitrary resolutions and map sizes.

I didn't know about $gameMap.displayX(), that may come in handy! (There are tons of things I don't know about how RMMV works.)

Here's what I'm doing so far. I'm creating a PIXI container that I fill with square sprites that are supposed to sit on top of the map squares. (At a later stage I use these sprites to highlight certain map squares dynamically.) Here's what it looks like in the test phase.
Skärmklipp 2020-08-08 19.37.38.png

I need to fix this container to the top left corner of the map, rather than the top left corner of the screen. To do this, I use what little I know of RMMV so far, which is the position of the player on the screen and on the map. I can calculate the correct position of my container from these, and a couple of other things.

Here's how I do it. (Most of the messy bits are just adjustments of coordinates.)
JavaScript:
// Calculate sprite offsets for arbitrary (Yanfly) resolutions and map sizes.
var setWindowWidth = typeof Yanfly != 'undefined' ? Yanfly.Param.ScreenWidth : 816;
var setWindowHeight = typeof Yanfly != 'undefined' ? Yanfly.Param.ScreenHeight : 624;
xScreenOff = (setWindowWidth - $dataMap.width * $gameMap.tileWidth()) > 0 ? (setWindowWidth - $dataMap.width * $gameMap.tileWidth()) / 2 + 1 : 0;
yScreenOff = (setWindowHeight - $dataMap.height * $gameMap.tileHeight()) > 0 ? (setWindowHeight - $dataMap.height * $gameMap.tileHeight()) / 2 : 0;

// Create a PIXI ParticleContainer for rendering the cone sprites.
var partCont = new PIXI.particles.ParticleContainer();
partCont.update = function () {
    this.position.x = xScreenOff + ($gamePlayer.screenX() - $gamePlayer.x * $gameMap.tileWidth()) - $gameMap.tileWidth() / 2 < -($gameMap.tileWidth()) ?
        xScreenOff + ($gamePlayer.screenX() - $gamePlayer.x * $gameMap.tileWidth()) - $gameMap.tileWidth() / 2 :
        xScreenOff;
    this.position.y = yScreenOff + ($gamePlayer.screenY() - $gamePlayer.y * $gameMap.tileHeight()) - ($gameMap.tileHeight() - 6) < -($gameMap.tileHeight()) ?
        yScreenOff + ($gamePlayer.screenY() - $gamePlayer.y * $gameMap.tileHeight()) - ($gameMap.tileHeight() - 6) :
        yScreenOff;
}

// Add the particle container to the scene.
SceneManager._scene.addChild(partCont);

This almost works. That is, it works except that for maps that scroll because they are larger than the window, the position of the container is updated pixel-wise when the player moves right or down. Everything ends up in the right place, but it looks ugly. (This happens because while the player is in-between squares, $gamePlayer.screenX() is smaller than $gamePlayer.x * $gameMap.tileWidth(), which is the test for when to update the container position. There is a fix for this, I'm sure.)
 

Kronicide

Veteran
Veteran
Joined
Jul 7, 2020
Messages
39
Reaction score
18
First Language
Swedish
Primarily Uses
RMMV
Here is a hopefully more readable version of the code snippet. I'm sure there is a much saner way of adding a new sprite container as a layer to the game, but I guess I wanted to see if I could do it this way. (Which I almost could.) Any comments about better ways of doing this are very welcome!

JavaScript:
// Calculate sprite offsets for arbitrary (Yanfly) resolutions and map sizes.
// The offset is the difference between the top left corner of the screen and
// the top left corner of the map. This is needed for when the map is smaller
// than the configured screen resolution.
var setWindowWidth = typeof Yanfly != 'undefined' ? Yanfly.Param.ScreenWidth : 816;
var setWindowHeight = typeof Yanfly != 'undefined' ? Yanfly.Param.ScreenHeight : 624;
var xScreenOff = (setWindowWidth - $dataMap.width * $gameMap.tileWidth()) > 0 ? (setWindowWidth - $dataMap.width * $gameMap.tileWidth()) / 2 + 1 : 0;
var yScreenOff = (setWindowHeight - $dataMap.height * $gameMap.tileHeight()) > 0 ? (setWindowHeight - $dataMap.height * $gameMap.tileHeight()) / 2 : 0;

// Create a PIXI ParticleContainer for rendering the square sprites. The update
// function is called every frame. The position of the container is set to the
// screen offset for X and Y plus the difference between the player's screen
// coordinates and map coordinates (adjusted for that the player's screen
// coordinates are not centered in the upper left corner of the player sprite.)
var partCont = new PIXI.particles.ParticleContainer();
partCont.update = function () {
    var screenMapDiffX = ($gamePlayer.screenX() - $gamePlayer.x * $gameMap.tileWidth()) - $gameMap.tileWidth() / 2;
    var screenMapDiffY = ($gamePlayer.screenY() - $gamePlayer.y * $gameMap.tileHeight()) - ($gameMap.tileHeight() - 6);
    this.position.x = xScreenOff + screenMapDiffX < -($gameMap.tileWidth()) ? xScreenOff + screenMapDiffX : xScreenOff;
    this.position.y = yScreenOff + screenMapDiffY < -($gameMap.tileHeight()) ? yScreenOff + screenMapDiffY : yScreenOff;
}

// Add the particle container to the scene.
SceneManager._scene.addChild(partCont);

The test in the last two lines in the update function is there to only add the difference between the map origin and the screen origin (screenMapDiff) when the map is actually scrolling.
 
Last edited:

Kronicide

Veteran
Veteran
Joined
Jul 7, 2020
Messages
39
Reaction score
18
First Language
Swedish
Primarily Uses
RMMV
Should be as simple as...
Code:
$gameMap.displayX()*$gameMap.tileWidth()
$gameMap.displayY()*$gameMap.tileHeight()
So it took me a little too long to figure this out, but
JavaScript:
this.position.x = -($gameMap.displayX() * $gameMap.tileWidth());
this.position.y = -($gameMap.displayY() * $gameMap.tileWidth());
works perfectly. I'm not sure why, because I don't know why $gameMap.displayX() has a positive value when the origin of the map is outside of the screen, to the left. Is $gameMap.displayX() the x-coordinate of the screen origin, on the map's coordinate system? Anyway, this is about a million times simpler than my mad attempt above. Thanks! :^)
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Seamless secret passages just by painting a path with a region id. Simple but slick.
Playing with colour ramps. The first set is the originals, the ones only facing forward in different colour schemes are the new colours.

I present... MAGIC (in progress)

No vaccine till at least spring 2021. Have not seen our parents since Dec 2019. But at least we can phone each other. How is everyone else's situation?

Forum statistics

Threads
102,978
Messages
996,441
Members
134,446
Latest member
vorpal04
Top