Sign in to follow this  
dontKnowWhatToPick

3d character position in a 2d map

Recommended Posts

Hi all I have a character moving around in a 3d world and I also have a 512x512 texture that is the 2d representation of the 3d world, that is, the top view map of the world. As i move my character around I would like to see its current 3d position converted to a 2d map position but im not sure how this can be achieved. Some ideas on how this can be done would be very appreciated Happy thanks

Share this post


Link to post
Share on other sites
What he is saying is that, given you are using an X,Y,Z coordinate system, you must find the correlation between a unit in the 3d world and a unit on the mini map.

Say your 3d map is 1000 units wide in the X direction and 1000 units wide in the Y direction, and your mini-map is 512x512. 1000/512 will give you the size correlation. Then, when you want to show the character on the mini-map, you would take it's X and Y coordinates in the 3d world, and divide them by the quotient before. This will give you an X and Y coordinate on the scale of your mini map.

Share this post


Link to post
Share on other sites
Does your minimap represent the entire 3D world or just a small area close to the player?

If your minimap is small version of the entire world then it is a matter of getting a mapping/scaling factor. However if you are wanting to create a minimap of just the area close to the player (similar to what I did in my ghost toast game) then what you probably want to do is take the players position and plant it in the centre of the minimap.

Based on the forward and right direction that the player is facing, you can define the Y and X axis of your minimap accordingly. Once you have these two directional vectors you need a scaling factor to define how much of the 3D world should be visible in the minimap.

You can now generate your minimap wrt the players current position.

Share this post


Link to post
Share on other sites
Quote:
Original post by dontKnowWhatToPick
what if the units are not the same in both axis and is more like 1000x350 for example?


Then just scale each dimension accordingly (world X to map X and world Y to map Y).

Share this post


Link to post
Share on other sites
If your world is WORLD_WIDTH by WORLD_HEIGHT, and your minimap is MINI_WIDTH by MINI_HEIGHT, then, if you're showing the whole world in the minimap, you can place the player (at PLAYER_WORLD_X, PLAYER_WORLD_Y) in the minimap like so:

PLAYER_MINI_X = MINI_WIDTH * ( PLAYER_WORLD_X / WORLD_WIDTH )
PLAYER_MINI_Y = MINI_HEIGHT * ( PLAYER_WORLD_Y / WORLD_HEIGHT )

What the math is doing is taking the player's world coordinates and dividing them with the world width/height such that you end up with a (parameterized)
position that is from 0 (left side) to 1 (right side). Now that you have the position as a %, you can multiply by the minimap's dimentions to get the position with respect to the minimap.

Share this post


Link to post
Share on other sites
Also, if you'll be updating the minimap often, you should keep track of the MINI_WIDTH/WORLD_WIDTH ratio (as well as the height ratio) and use that to multiply the player position when you need to so you don't divide all the time.

ex:

loadMap()
{
....
// Calculate world to minimap ratios
WORLD_TO_MINIMAP_RATIO_WIDTH = MINI_WIDTH/WORLD_WIDTH;
WORLD_TO_MINIMAP_RATIO_HEIGHT = MINI_HEIGHT/WORLD_HEIGHT;
...
}

Then when you need to update the minimap, you can get the position of any unit/player like so:


DrawMiniMap()
{
...
// Get the minimap coords...
PLAYER_MINIMAP_X = PLAYER_WORLD_X * WORLD_TO_MINIMAP_RATIO_WIDTH;
PLAYER_MINIMAP_Y = PLAYER_WORLD_Y * WORLD_TO_MINIMAP_RATIO_HEIGHT;
...
}



Share this post


Link to post
Share on other sites
If your world is 700x1200 meters and your minimap is 512x512 pixels, then you want a conversion ratio that gives:

Width:
meter 0 -> minimap pixel 0
meter 700 -> minimap pixel 511

and Height:
meter 0 -> minimap pixel 0
meter 1200 -> minimap pixel 511

This is the classic off by one/fence post problem: you have 512 pixels from 0 to 511, and you have 701 meter "posts" (from 0 to 700) that you want to map.

so you'd use the ratios: 511/700 and 511/1200

If you try this out, you'll see that being at 0,0 gives:

X = 0 * (511/700) = 0
Z = 0 * (511/1200) = 0

so pixel 0,0 in the minimap. Then, being at 700,1200 gives:

X = 700 * (511/700) = 511
Z = 1200 * (511/1200) = 511

so pixel 511,511 in the minimap

Also, depending on what data types you use to store the ratios or how you do your math you may get rounding errors that prevent perfect alignment which is most notable at the edges. The cleanest way I can think of doing it without this problem is to do the multiplication first, then the divide.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this