XNA Creators Club Online
Page 1 of 1 (19 items)
Sort Posts: Previous Next

Polygon Union/Intersection

Last post 10-28-2008 12:55 PM by zanders. 18 replies.
  • 09-01-2008 8:03 AM

    Polygon Union/Intersection

    Some people might remember this thread i made about a polygon union and intersection which is a central part to the level editor for the game i'm making.

    I ended up writing my own algorithm for this, and being as there doesn't seem to be anything like this out there I thought I might as well share it with the world :). The code is a bit shaky and sometimes doesn't work correctly, and can only operate on simple polygons without holes.

    It is fast enough to be run in realtime on xbox.

    Here it is!

    Could anyone give any hints on how to further improve this? I'm learning this programming stuff in my free time so i'm not brilliant at making code robust. :P

  • 09-01-2008 6:57 PM In reply to

    Re: Polygon Union/Intersection

    If you google for "polygon clipping" and "polygon triangulation" you'll find that this is a very well researched area, with lots of papers published in literature going back to the '60s.

    So, I think it's great that you're sharing code (it's a great opportunity to learn from feedback, too!) but I think saying "there is nothing like that out there" is not correct.


    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 09-01-2008 8:23 PM In reply to

    Re: Polygon Union/Intersection


    jwatte:

    I think saying "there is nothing like that out there" is not correct.

    Oops! What I really meant to say is that I couldn't find any implementations of a polygon clipper algorithm out there written in C#; every sample I found so far has been written in C++ and is beyond my capabilities to convert into C#.

    The algorithm definetely needs improvement though, as it sometimes it doesn't work as intended and gives incorrect output data. Could someone help me improve things?

  • 09-05-2008 3:57 AM In reply to

    Re: Polygon Union/Intersection

    I've done this in C# before using another game engine.  It was to load Half-Life .map files and build the geometry from planes and perform CSG to combine the shapes.  If you care to look, it's open source (though not ported to XNA, but I could easily do that).  AmmunitionSDK
  • 09-05-2008 6:12 PM In reply to

    Re: Polygon Union/Intersection

    That site appears to be down at the moment...


    The ZBuffer News and information for XNA

    Please read the forum FAQs - Bug reporting
  • 09-05-2008 8:10 PM In reply to

    Re: Polygon Union/Intersection

    The ZMan:
    That site appears to be down at the moment...

    My server died...   ..currently putting new ram in and rebooting.   Try it again....

  • 09-08-2008 10:18 PM In reply to

    Re: Polygon Union/Intersection

    Aeon:
    I've done this in C# before using another game engine.  It was to load Half-Life .map files and build the geometry from planes and perform CSG to combine the shapes.  If you care to look, it's open source (though not ported to XNA, but I could easily do that).  AmmunitionSDK

    Thanks! I'm downloading it now. Could you point me to the specific part of the SDK that does the union/intersection?
  • 09-09-2008 7:56 PM In reply to

    Re: Polygon Union/Intersection

    in Brush.cs in the TVMap section there is a function called _Build.  This takes a list of planes and constructs the geometry from their intersection points.  The math to get the intersection of 3 planes is....
    1             float fDet; 
    2             float[ MN = new float[9] { P1.Normal.x, P1.Normal.y, P1.Normal.z, P2.Normal.x, P2.Normal.y, P2.Normal.z, P3.Normal.x, P3.Normal.y, P3.Normal.z }; 
    3             float[ IMN = new float[9] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; 
    4             float[ MD = new float[3] { P1.Dist, P2.Dist, P3.Dist }; 
    5  
    6             IMN[0] = MN[4] * MN[8] - MN[5] * MN[7]; 
    7             IMN[3] = -(MN[3] * MN[8] - MN[5] * MN[6]); 
    8             IMN[6] = MN[3] * MN[7] - MN[4] * MN[6]; 
    9  
    10             fDet = MN[0] * IMN[0] + MN[1] * IMN[3] + MN[2] * IMN[6]; 
    11  
    12             if (fDet == 0.0f) 
    13                 return false
    14  
    15             IMN[1] = -(MN[1] * MN[8] - MN[2] * MN[7]); 
    16             IMN[4] = MN[0] * MN[8] - MN[2] * MN[6]; 
    17             IMN[7] = -(MN[0] * MN[7] - MN[1] * MN[6]); 
    18             IMN[2] = MN[1] * MN[5] - MN[2] * MN[4]; 
    19             IMN[5] = -(MN[0] * MN[5] - MN[2] * MN[3]); 
    20             IMN[8] = MN[0] * MN[4] - MN[1] * MN[3]; 
    21  
    22             fDet = 1.0f / fDet; 
    23  
    24             IMN[0] *= fDet; 
    25             IMN[1] *= fDet; 
    26             IMN[2] *= fDet; 
    27             IMN[3] *= fDet; 
    28             IMN[4] *= fDet; 
    29             IMN[5] *= fDet; 
    30             IMN[6] *= fDet; 
    31             IMN[7] *= fDet; 
    32             IMN[8] *= fDet; 
    33  
    34             V.x = IMN[0] * MD[0] + IMN[1] * MD[1] + IMN[2] * MD[2]; 
    35             V.y = IMN[3] * MD[0] + IMN[4] * MD[1] + IMN[5] * MD[2]; 
    36             V.z = IMN[6] * MD[0] + IMN[7] * MD[1] + IMN[8] * MD[2]; 
    V is the vector of the intersection
  • 09-09-2008 10:11 PM In reply to

    Re: Polygon Union/Intersection

    If it uses planes, then it can only deal with convex polygins.
    Jon Watte, Direct3D MVP kW X-port 3ds Max .X exporter kW Animation source code
  • 09-10-2008 7:31 PM In reply to

    Re: Polygon Union/Intersection

    Sadly that is of little use. :(
    My algorithm finds the union and intersection of simple 2D polygons without holes.

    So again I cheekily ask if there is anybody else out there who can help me improve it?
  • 09-12-2008 11:45 PM In reply to

    Re: Polygon Union/Intersection

    Here you go... the algorithm gets a little hairy http://boolean.klaasholwerda.nl/bool.html Sadly under the (IMO) Nasty GPL...


    The ZBuffer News and information for XNA

    Please read the forum FAQs - Bug reporting
  • 09-14-2008 4:03 PM In reply to

    Re: Polygon Union/Intersection

    Brilliant! Just what I needed.... its complicated though.