Overlap Lib 1.0

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.


EDIT: Here's a better line intersect script:
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)
            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

melee-master commented August 26, 2006 at 7:10 AM
Wow, very good. That menu window was an extremely nice touch too.
contrendo commented August 26, 2006 at 7:55 AM
Awesome! 9/10
marbs commented August 26, 2006 at 8:53 AM
That is really cool, and its presented excellently. That libray navigator bar is worthy of an example if its own. 10/10
sirxemic commented August 26, 2006 at 10:05 AM
Nice! 9/10
Knightwing50 commented August 26, 2006 at 1:54 PM
Very good and like marbs said presented excellently. 9/10
Eternal commented August 26, 2006 at 2:01 PM
abacus commented August 26, 2006 at 7:01 PM

but the library nav bar was nice.

10/10 cause of the nav bar.
Sgtpepper commented August 27, 2006 at 7:11 PM
Very nice I liked it and it was well presented. 10
Misconstruct commented August 27, 2006 at 8:49 PM
Very professional. 10/10
fprefect111 commented August 28, 2006 at 11:53 AM
Nicely presented, very clever. [=)]
Mind if I use it in a (possible) vector-based game I'm about to start working on?
punimer_gm commented October 15, 2006 at 1:46 PM
i dont see why u r cheering... its only colision effects
Flea1991 commented December 08, 2006 at 4:12 PM
I love it!