Search code examples
xnaprojectionarea

XNA: projection area of ​​each quad's array element


I have an array of quads, each having it’s own position, defines the coordinates of the vertices. I have scene's projection on the plane of the camera. I want to know projection area of ​​each quad's array element. In fact, i wont to know by projection pixel which object it belongs.


Solution

  • You can project each vertex onto the screen with the Viewport.Project method.

    After that you have the quad's vertices in screen coordinates. All that remains is determining, of the point in question belongs to that polygon. This can be done in different ways.

    One option is to determine the side of the point relative to each polygon edge. This works beacuse the polygon is convex.

    previousSide := 0
    for each edge in polygon
        d1 := edge.p2 - edge.p1 //direction vector of edge
        d2 := pointInQuestion - edge.p1 //direction of first edge point to point in question
        side = d1.x * d2.y - d1.y * d2.x //cross product
        if side * previousSide < 0 then return false //different sides
        previousSide := side
     next
     return true