HomeForumsWhat's newResources 
 
 
Determining the Location of your Polygon?
slinga - May 16, 2007
 slinga May 16, 2007
Hey all,

I have been playing around with the SGL library the last few days and I have a small demo game almost done. I'm doing an asteroids (12 player of course) clone.

Each player is represented by a single triangle. By following the SGL tutorials I was able to get the player to move, rotate etc. The problem I have now is how do I determine the location of all three vertices? I need this for collision detection.

I tried something like this:

Code:
  
void dbgBlasteroid(blasteroid* someBlasteroid)

{

    char temp[256];

    FIXED a, b, c;

    unsigned int i = 0;

    a = someBlasteroid->pos[X];

    b = someBlasteroid->pos[Y];

    c = someBlasteroid->pos[Z];

    slPrint("                                                      ", slLocate(5,10));

    slPrintFX(a, slLocate(10,5));

    slPrintFX(b, slLocate(10,6));

    slPrintFX(c, slLocate(10,7));

    for(i = 0; i < someBlasteroid->polygon->nbPoint; i++)

    {

        slPrintFX(someBlasteroid->polygon->pntbl[i][0], slLocate(1,i+10));

        slPrintFX(someBlasteroid->polygon->pntbl[i][1], slLocate(10,i+10));

        slPrintFX(someBlasteroid->polygon->pntbl[i][2], slLocate(20,i+10));

        //slPrintFX(someBlasteroid->polygon->pntbl[1], slLocate(10,i+10));

        //slPrintFX(someBlasteroid->polygon->pntbl[2], slLocate(10,i+10));

    }

}
POLYGON is of type PDATA. This approach sort of works, this information gives me the original values of the vertices, but not the new values.

How can I get the current values of the vertices? Basically, I need to know the last coordinates the polygon was displayed. Without them I cannot do collision detection...

 antime May 17, 2007
Likely you will have to calculate the exact positions manually. SGL can't modify the original vertex data or all objects would be destroyed in seconds due to rounding errors, and it probably doesn't keep the transformed polygons around due to memory restrictions.

 RockinB May 18, 2007
Slinga, you should have a look at slCalcVector() and slCalcPoint().

They use (or can be used with) the current matrix, so call them at the same position when calling slPutPolygon().

 slinga May 20, 2007
Thanks RockinB,

slCalcPoint() worked perfectly. I was able to get the points of the triangle as I drew it to the screen and now collision detection seems to work.

I need to plan some optimizations now. Doing 3x3 line intersections tests per triangle (there can be up to 12 players) is killing the saturn's cpu...oh well I will figure it out. Thanks again.

 antime May 20, 2007
Before actually testing if the triangles intersect you should do a bounding box/sphere test to quickly reject the obviously nonintersecting cases. Google also gives lots of papers with 2D triangle intersection tests that claim to be faster than plain line-line intersections which you can adapt.


You could also try something radically different, like drawing the first ship into an offscreen buffer, then drawing the other one as half-transparent. If they intersect there will be pixels of the transparent colour in the buffer. Might work OK if the ships are not too big, but you have to choose the colour so that transparent over background is different from transparent over the other ship.