Jump to content
  • Advertisement


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

David Rostowsky

Pixel Perfect Tile Finding

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

Hi, I''m trying to figure out if there is some fancy math formula I can chug my mouse x,y through to figure out which tile Im in with pixel perfect accuracy. Im using 64x32 isometric tiles. Thanks in advance! Dave

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

The following code is in Delphi-Pascal, but the concept is mathematical, so it should be easy to convert.


Eddy Sterckx

Function PointInPolygon(CheckPoint : TPoint; PollyArray : Array of TPoint) : Boolean;
X0, Y0, X1, Y1, X2, Y2 : Integer;
i : Integer;
LastPointIndex : Integer;
// Decide whether or not a point lies within a convex polygon
// Polygon coordinate points must be given in clockwise fashion

Result := True; // default : point is in polygon until otherwise proven

X0 := CheckPoint.X;
Y0 := CheckPoint.Y;

LastPointIndex := High(PollyArray);

// take every "line" in turn and check on which "side" of that line lies
// the point to check. If for every line-pair the point to check is on
// the "inside" than the point lies in the polygon.
For i := 0 to LastPointIndex do
{First point of the line}
X1 := PollyArray.X;
Y1 := PollyArray[i].Y;
{Next point}
If i <> LastPointIndex Then
X2 := PollyArray[i+1].X;
Y2 := PollyArray[i+1].Y;
X2 := PollyArray[0].X;
Y2 := PollyArray[0].Y;
// this line does it all !
Result := (Y1 * X2) - (X1 * Y2) - (Y0 * X2) + (X0 * Y2) + (Y0 * X1) - (X0 * Y1) <= 0;

If Result = False Then // optimize a bit

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!