Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No. (Not up and including version 3 at least)</p> <p>XNA has bounding volumes such as <em>frustums</em> or <em>boxes</em> but it has no notion of polygons.</p> <p>A simple, fast and effective way of performing point in polygon with XNA can be found <a href="http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/" rel="nofollow noreferrer"><del>here</del></a>. I've recently implemented this and it was excellent.</p> <p>You know the point of your object, all you need to do is create a polygon surrounding this object - using vectors would be the best and easiest method. Then perform the point in polygon check. </p> <p>Here is the example code of my implementation. The default point class in XNA is used. Polygon is a simple class which contains a collection of vectors making up the polygon.</p> <pre><code>/// &lt;summary&gt; /// Point in polygon check. /// &lt;/summary&gt; /// &lt;param name="point"&gt;The point.&lt;/param&gt; /// &lt;param name="polygon"&gt;The polygon.&lt;/param&gt; /// &lt;returns&gt;True if point is inside, false otherwise.&lt;/returns&gt; /// &lt;see cref="http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/"/&gt; public bool PointInPolygon(Point point, Polygon polygon) { bool inside = false; foreach (var side in polygon.Lines) { if (point.Y &gt; Math.Min(side.Start.Y, side.End.Y)) if (point.Y &lt;= Math.Max(side.Start.Y, side.End.Y)) if (point.X &lt;= Math.Max(side.Start.X, side.End.X)) { float xIntersection = side.Start.X + ((point.Y - side.Start.Y) / (side.End.Y - side.Start.Y)) * (side.End.X - side.Start.X); if (point.X &lt;= xIntersection) inside = !inside; } return inside; } </code></pre> <p>The Polgyon class is very basic, in semi pseudo form:</p> <pre><code>class Polygon { public List&lt;Line&gt; Lines { get; set; } } public class Line { public Vector2 Start; public Vector2 End; } </code></pre> <p>The polygon class could very easily just store a collection of vectors, but I introduced a line class as lines were required elsewhere.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload