Archived

This topic is now archived and is closed to further replies.

having some problems to get what tile the mouse is over...

This topic is 5129 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

well, I looked in TANSTAAFL tutorial, and I saw that I need to see where the mouse is in a specific rect, and then add according to what section of the rect is in... here is my code, and it works, but not all the time, and I dont know why... point in triangle function(I think the problem is here
inline bool PointIsInTriangle(int x1,int y1,int x2,int y2,int x3,int y3,int x,int y)
	{
		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;
	}
here I find what tile is it
int TileWidth=Map->TileWidth;
	int TileHeight=Map->TileHeight;

	int rectX=(GetMouseX())/TileWidth;
	int rectY=(GetMouseY())/TileHeight;
	int MouseInRectX=GetMouseX()%TileWidth;
	int MouseInRectY=GetMouseY()%TileHeight;
	int RegionDX,RegionDY;
	if (PointIsInTriangle(0,0,TileWidth/2,0,0,TileHeight/2,MouseInRectX,MouseInRectY))
	{
		RegionDX=-1;
		RegionDY=-1;
	}
	else
	if (PointIsInTriangle(TileWidth/2,0,TileWidth,0,TileWidth,TileHeight/2,MouseInRectX,MouseInRectY))
	{
		RegionDX=0;
		RegionDY=-1;
	}
	else
	if (PointIsInTriangle(0,TileHeight/2,TileWidth/2,TileHeight,0,TileHeight,MouseInRectX,MouseInRectY))
	{
		RegionDX=-1;
		RegionDY=1;
	}
	else
	if (PointIsInTriangle(TileWidth,TileHeight/2,TileWidth,TileHeight,TileWidth/2,TileHeight,MouseInRectX,MouseInRectY))
	{
		RegionDX=0;
		RegionDY=1;
	}
	else
	{
		RegionDX=0;
		RegionDY=0;
	}
	xTile=rectX+RegionDX;
	yTile=rectY+RegionDY;

Share this post


Link to post
Share on other sites