Jump to content
  • Advertisement
Sign in to follow this  
stu_pidd_cow

Calculating cursor tile...

This topic is 3092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a basic 2D tiled game, and for the editor I want to have a box around the tile that the cursor is currently hovering over. I'm having no trouble drawing the box, the trouble is converting the cursor coordinate to the tile coordinates. Here's the thing: the tiles are retro-looking, so one pixel in screen coordinates is NOT one pixel in the tile coordinates. I've tried getting this to work but it is a little off. I'm not sure how well you'll understand it, so I've tried commenting it well. Here's my code so far:
// POS is a struct containing an X and Y coordinate
POS EDITOR_WIN::GetCursorBox()
{
	// the pixel coordinates of the client area of the window where the tiles are
	RECT temp;
	GetWindowRect( D3DWindow, &temp );
	UINT32 WindowDimensionX = temp.right - temp.left;
	UINT32 WindowDimensionY = temp.bottom - temp.top;

	// get the point where the cursor is
	POINT tpoint;
	GetCursorPos( &tpoint );

	// if cursor is outside window, return error
	if ( tpoint.x < temp.left || tpoint.y < temp.top ||
		 tpoint.x > temp.left + temp.right || tpoint.y > temp.top + temp.bottom )
		return POS( UINT32_ERROR, UINT32_ERROR );

	// get how many tiles are on the screen for both x and y
	UINT32 TilesX = Terrain->GetSizeX();
	UINT32 TilesY = Terrain->GetSizeY();

	// how many pixels the tile image has on it
	UINT32 ImageResX = 16;
	UINT32 ImageResY = 16;

	// calculate the size (in screen pixels) of each tile
	float PixelsPerTileX = (float)WindowDimensionX / TilesX;
	float PixelsPerTileY = (float)WindowDimensionY / TilesY;

	// amount of pixels in each pixel seen on the tile
	UINT32 PixelsPerImagePixelX = (UINT32)( PixelsPerTileX / ImageResX );
	UINT32 PixelsPerImagePixelY = (UINT32)( PixelsPerTileY / ImageResY );

	// which tile pixel the cursor is on
	UINT32 CursorScreenImagePixelsX = ( tpoint.x - temp.left ) / PixelsPerImagePixelX;
	UINT32 CursorScreenImagePixelsY = ( tpoint.y - temp.top ) / PixelsPerImagePixelY;

	// the tile that the cursor if hovering over
	return POS( CursorScreenImagePixelsX / TilesX / 2,
				CursorScreenImagePixelsY / TilesY / 2 );
};



This code displays the box and does thing well, but the further away from the tile at 0,0, the less accurate it gets. Here is a pic of what it looks like. The red box is what I'm after. But when I done this, the cursor was actually in the box above. Obviously my math isn't my strong area, so hopefully someone can push me in the right direction. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Wow, that code looks way more complex than what you need. I think this is your situation, let me know if its right or not:

1) You have a tilemap you want to select from with regular sized tiles.
2) This tilemap may be scaled on the screen because of zoom.
3) You want the tile under the mouse/cursor.

If so, the code you supplied is rather obtuse. Knowing the tile size and pixels of the cursor coordinates from the top left and a scale factor, this is all you need:

TileX = WindowCursorX / (TileSizeX * MapScaleX);
TileY = WindowCursorY / (TileSizeY * MapScaleY);

Obviously, if MapScale is a float value you would have to do some casting. Perhaps like so:

TileX = (int)((float)WindowCursorX / ((float)TileSizeX * MapScaleX));
TileY = (int)((float)WindowCursorY / ((float)TileSizeY * MapScaleY));

I gave up on trying to see exactly what you are doing up there, so if I'm wrong let me know.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!