Advertisement

How I convert a location to a map coordinate

Started by July 27, 2000 07:16 PM
-1 comments, last by Leprosy 24 years, 4 months ago
I know the question has been asked a billion times of how to find out what tile point x, y is in and the suggest using mouse surfaces etc etc. I know this code might be fairly unoptimizaed but it does work, and I end up using it alot so I figure i''d post it and if anyone needs to use it they can. px and py is the point that you want to find out which tile its in. coordinates is a struct that has a simple x, y. struct coordinates { int x; int y; }; the code returns the map tile number x, and y that the point resides in. Remember when using this code to account for the location of the window relative to the world. ie if you want to find out the tile your mouse is in you need to call the function with a mousex//location of mouse on screen// + windowx //location of window in world. the world originates in the upper left corner... and dont forget to account for your indenting of the map when you draw it. "sometimes we indent the tiles an minus 16 or 32 pixels to mask black space from the diamond shape. If anyone finds this helpful I have some more functions that are extremely reusable like find the direction between to points etc.. which I can post
    

coordinates WhereXWhereY( int px, int py)
{

// first we need to find what square the tile is in

	int even_x = px / tilewidth; // the tile that the point would be in if it was dead center in the diamond.

	int even_y = py / (tileheight);
	int wherex;  // return variables for map location

	int wherey;
// now we find what are of the square the point is in to

// determine what diamond it is in

// example 

// 1 /\ 2

//  /3 \ 

// 4\  /5

//---------

// if the point falls inside the square of 3 it count be in any 

// one of 5 diamonds

	if ( (px % tilewidth) < (tilewidth /2)) {
		// LEFT HALF

		if ((py % tileheight < (tileheight /2))) {
			//TOP HALF

			if ( py % tileheight == (tileheight/2)) 
			{
				wherex = even_x;
				wherey = even_y *2;

			} 
			else if ( (tilewidth/tileheight) > ( ((px) % tilewidth) / (tileheight/2 - py % tileheight) ) )
			{
				wherex = even_x-1;
				wherey = even_y * 2 -1;
			} else
			{
				wherex = even_x;
				wherey = even_y * 2;
			}
		} else {
			//BOTTOM HALF

			if ( py % tileheight == (tileheight/2)) 
			{
				wherex = even_x;
				wherey = even_y *2;

			} 
			else if  ( (tilewidth/tileheight) > ( ((px) % tilewidth) / ((py %tileheight) - (tileheight /2))  ) )
			{
				wherex = even_x-1;
				wherey = even_y * 2 +1;
			}
			else
			{
				wherex = even_x;
				wherey = even_y *2;
			}
		}
	} else {
		// RIGHT HALF

		if ((py % tileheight < (tileheight /2))) {
			//TOP HALF

			if ( py % tileheight == (tileheight/2)) 
			{
				wherex = even_x;
				wherey = even_y *2;
			} 
			else if ( (tilewidth/tileheight) > ( (tilewidth-(px % tilewidth)  ) / (tileheight/2 - py % tileheight) ) ) 
			{
				wherex = even_x;
				wherey = even_y *2 -1;
			} else
			{
				wherex = even_x;
				wherey = even_y *2;
			}
		} else {
			//BOTTOM HALF

			if ( py % tileheight == (tileheight/2)) {
				wherex = even_x;
				wherey = even_y *2;
			} 
			else if ( (tilewidth/tileheight) > ( (tilewidth-(px % tilewidth)) / ((py % tileheight)-tileheight/2) ) ) 
			{
				wherex = even_x;
				wherey = even_y *2 +1;
			} else
			{
				wherex = even_x;
				wherey = even_y *2;
			}
		}
	}
	coordinates return_value;
	return_value.x = wherex;
	return_value.y = wherey;
	return(return_value);
}

    
Hope it helps Lep

This topic is closed to new replies.

Advertisement