by Crazy Star

Posted on August 26, 2006 at 7:10 AM

This is a collection of scripts to check if primitives overlap (not a lib like those you use for drag and drop...). I hope you will find this useful or report any bug that you might find or suggest that I improve something in some way. There is a bug that I haven't bothered to fix because it has no real significance and has nothing to do with the scripts; a bug that makes the radius of circles change slightly when you move them around.

-SpectreNectar

`float linx,liny;`

float abs2(float x)

{

if (x<0) {return -x;}

return x;

}

int LinesCross(float x0,float y0,float x1,float y1,float x2,float y2,float x3,float y3)

{

float d=(x1-x0)*(y3-y2)-(y1-y0)*(x3-x2);

if (abs2(d)<0.001) {return -1;}

float AB=((y0-y2)*(x3-x2)-(x0-x2)*(y3-y2))/d;

if (AB>0.0 && AB<1.0)

{

float CD=((y0-y2)*(x1-x0)-(x0-x2)*(y1-y0))/d;

if (CD>0.0 && CD<1.0)

{

linx=x0+AB*(x1-x0);

liny=y0+AB*(y1-y0);

return 1;

}

}

return 0;

}

Source: http://www.gamedev.net/community/forums/topic.asp?topic_id=380428&whichpage=1?And a better point in triangle script:

`// Compute vectors `

v0 = C - A

v1 = B - A

v2 = P - A

// Compute dot products

dot00 = dot(v0, v0)

dot01 = dot(v0, v1)

dot02 = dot(v0, v2)

dot11 = dot(v1, v1)

dot12 = dot(v1, v2)

// Compute barycentric coordinates

invDenom = 1 / (dot00 * dot11 - dot01 * dot01)

u = (dot11 * dot02 - dot01 * dot12) * invDenom

v = (dot00 * dot12 - dot01 * dot02) * invDenom

// Check if point is in triangle

return (u > 0) && (v > 0) && (u + v < 1)

Source: http://www.blackpawn.com/texts/pointinpoly/default.html
These won't give you collisions on the boundary though but that's only a matter of changing > to >= and < to <= (except for the abs thing in LinesCross) and combining it with my scripts if endpoints overlaps.

If I ever make a version 2 it will have ellipses , possibly slanted, and why I might even add in 3d collisions just for the heck of it.

Thanks you russians guys for spreading my examples :D

Comments are currently disabled on games.

but the library nav bar was nice.

10/10 cause of the nav bar.

Mind if I use it in a (possible) vector-based game I'm about to start working on?

10/10