Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

Help finding tile under cursor

This topic is 4859 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 have a ISO map editor that runs in windowed mode. I can blt that map and I can blt the tiles to choose from. I also can figure out what tile I am on for the tile to choose from My issue is when I go to place that tile I need to figure out what tile I am on to place the tile picked Can any one help with this? Thanks [Edited by - kingpinzs on August 21, 2005 3:51:36 PM]

Share this post


Link to post
Share on other sites
Advertisement
If you are using a class for creating the tiles you could add a function to your class like : IsTilePicked(mouse_x_coord, mouse_y_coord)

If the coord of your mouse are within the borders of your tile coordinates then return true else return false;

Share this post


Link to post
Share on other sites

bool tile::isTilePicked(int mouseX, int mouseY)
{
mouseTileX = mouseX / TILE_WIDTH;
mouseTileY = mouseY / TILE_HEIGHT;

return ( (this->x == mouseTileX) && (this->y == mouseTileY) );
}


I believe that should work. It is important to realize that those are integer divides and that the left-most tile is tile 0.

So, mouseTileX is the number of tiles in the x direction that the mouse is. Because of the integer division, if the mouse is 1.5 tiles in (i.e. at pixel 15, with tile width of 10), the program will count it (correctly) as being on tile 1.

To find out how far into the tile the mouse is, just use mouseX % TILE_WIDTH and mouse& % TILE_HEIGHT.

Hope that helps.

Share this post


Link to post
Share on other sites
I am using that.


case WM_LBUTTONUP:
{
POINT ptMouse;
ptMouse.x = LOWORD(lParam);
ptMouse.y= HIWORD(lParam);

int x,y;

RECT rcZone;

SetRect(&rcZone,0,0,800,600);

if(PtInRect(&rcZone,ptMouse))
{

x=(ptMouse.x-50)/128;
y=(ptMouse.y-47)/128;
Mapy[y][x] = TileSlected;

}
}



But sence my map is iso > cant just use the screen coords to find the tile and sence it can scroll the tiles are always changing.

so how would I do it with iso.

I can use the equation to do it with square tiles just fine.

Share this post


Link to post
Share on other sites
google "point in polygon".

also, you can split the iso square into 2 triangles and do 2 "point in triangle" tests - google that too.

Share this post


Link to post
Share on other sites
Or you could perform an appropriate transformation of the coordinates, and then do an ordinary rectangle test.

Share this post


Link to post
Share on other sites
I am reading isometric game programing with DX7 and it has a wayto make the mouse map . But I feel it is over kill and there should be a simpler way to do it.

the program already knows the cords for every tile or they would not be able to display the tiles.

So my question now is how to see if my cursor coords match any of the tile coords?

has any one made a iso 2d game that your mouse can pick the tiles and if socan any one share there ideas on how to acomplish this?

thanks

Share this post


Link to post
Share on other sites

inline bool PointIsInTriangle(int x1,int y1,int x2,int y2,int x3,int y3,int x,int y) // found is some website, thanks to unknown author.
{
int a,b,c;
a = (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1);
b = (x3 - x2) * (y - y2) - (y3 - y2) * (x - x2);
c = (x1 - x3) * (y - y3) - (y1 - y3) * (x - x3);
if ((a>=0 && b>=0 && c>=0) || (a<0 && b<0 && c<0))
return true;
return false;
}

int rectX=(MouseX+Xoffset)/TILE_W; // Xoffset is X the camera position
int rectY=((MouseY+Yoffset)/TILE_H)*2; // Yoffset is the Y camera position
int MouseInRectX=(MouseX+Xoffset)%TILE_W;
int MouseInRectY=(MouseY+Yoffset)%TILE_H;
int RegionDX,RegionDY;

if (PointIsInTriangle(0,0,TILE_W/2,0,0,TILE_H/2,MouseInRectX,MouseInRectY))
{
RegionDX=-1;
RegionDY=-1;
}
else
if (PointIsInTriangle(TILE_W/2,0,TILE_W,0,TILE_W,TILE_H/2,MouseInRectX,MouseInRectY))
{
RegionDX=0;
RegionDY=-1;
}
else
if (PointIsInTriangle(0,TILE_H/2,TILE_W/2,TILE_H,0,TILE_H,MouseInRectX,MouseInRectY))
{
RegionDX=-1;
RegionDY=1;
}
else
if (PointIsInTriangle(TILE_W,TILE_H/2,TILE_W,TILE_H,TILE_W/2,TILE_H,MouseInRectX,MouseInRectY))
{
RegionDX=0;
RegionDY=1;
}
else
{
RegionDX=0;
RegionDY=0;
}
xTile=rectX+RegionDX;
yTile=rectY+RegionDY;




can some one change this equation so it could be used for iso?

I found it going through old post on these forumes.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!