0

I am pretty new to XNA 4.0 and have a problem I can't seem to figure out. So far I have been detecting mouse clicks by creating rectangles and checking the rectangle.contains method, but what if I don't want to check inside a rectangle?

In one part of my map I have a triangle button that I want to let the user click, but I want to only count it as a click if it is actually inside the triangle. If I create a rectangle around the triangle the user will be able to click outside the texture area, if you know what I mean.

How do I go about doing this?

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101

1 Answers1

1

You will have to use a Point in Polygon Algorithm for more complex shapes:

The check for convex polygons (your triangle) is simpler than for concave ones (see first linked article).

If you have to do lots of checks and are hitting performance limits, consider using some kind of hierarchy, a Quadtree or an LOD system. For example, you can calculate an additional bounding rectangle for very complex polygons and only do the expensive in-check with the polygon if the point lies inside the rectangle.

Community
  • 1
  • 1
Lucius
  • 3,705
  • 2
  • 22
  • 41