Oct 30, 2004, 02:20 PM

Anybody know of a way to get a good collision between a square and a square that is rotated 45 degrees? I'm currently using the idea of checking the current coordinate versus the last coordinate.

You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Oct 30, 2004, 02:20 PM

Anybody know of a way to get a good collision between a square and a square that is rotated 45 degrees? I'm currently using the idea of checking the current coordinate versus the last coordinate.

Oct 30, 2004, 03:23 PM

SimReality/Nick Wrote:Anybody know of a way to get a good collision between a square and a square that is rotated 45 degrees? I'm currently using the idea of checking the current coordinate versus the last coordinate.when I get back home, I can post code for you about checking arbitrary quads with arbitrary quads... would that be better?

Oct 30, 2004, 06:56 PM

I'll vouch for the usefulness of skyhawk's code. I am using most of it in my current project and it works great.

Oct 30, 2004, 09:02 PM

Dave Eberly's http://www.magic-software.com/SourceCode.html code has collision for moving and rotating rectangles, spheres, polygons, and lots of good collision/intersection stuff. I use it all the time.

Oct 30, 2004, 09:45 PM

skyhawk Wrote:when I get back home, I can post code for you about checking arbitrary quads with arbitrary quads... would that be better?

That'd be great. I'd really appreciate it. By your use of the word quads does that imply it works with rectangles as well?

Oct 30, 2004, 11:01 PM

SimReality/Nick Wrote:That'd be great. I'd really appreciate it. By your use of the word quads does that imply it works with rectangles as well?rectangles are a right and proper subset of quads.

oka, first you need to define your square into adpoints and adbpoints (or you can change the variables to anything. all they are are:

Code:

`float adpoints[4][2];`

float adbpoints[4][2];

also, bxp and xp and byp and yp are redundants of adpoints, they are needed to pass to the pnpoly function.

Code:

`vector a;`

vector b;

vector c;

vector d;

// my quad.a my quad.b

a.x=adpoints[0][0];a.y=adpoints[0][1]; b.x=adpoints[1][0];b.y=adpoints[1][1];

// other quad.a otherquad.b

c.x=adbpoints[0][0];c.y=adbpoints[0][1]; d.x=adbpoints[1][0];d.y=adbpoints[1][1];

if ( SegmentsIntersect(a,b,c,d) ) goto lablyes;

c.x=adbpoints[2][0];c.y=adbpoints[2][1];

if ( SegmentsIntersect(a,b,d,c) ) goto lablyes;

d.x=adbpoints[3][0];d.y=adbpoints[3][1];

if ( SegmentsIntersect(a,b,c,d) ) goto lablyes;

a.x=adpoints[2][0];a.y=adpoints[2][1];

c.x=adbpoints[0][0];c.y=adbpoints[0][1]; d.x=adbpoints[1][0];d.y=adbpoints[1][1];

if ( SegmentsIntersect(b,a,c,d) ) goto lablyes;

c.x=adbpoints[2][0];c.y=adbpoints[2][1];

if ( SegmentsIntersect(b,a,d,c) ) goto lablyes;

d.x=adbpoints[3][0];d.y=adbpoints[3][1];

if ( SegmentsIntersect(b,a,c,d) ) goto lablyes;

for(x=0;x<4;x++)

if(pnpoly(4,xp,yp,bxp[x],byp[x])) goto lablyes;

for(x=0;x<4;x++)

if(pnpoly(4,bxp,byp,xp[x],yp[x])) goto lablyes;

return 0;

lablyes:

// I had an exception thing here collision RESPONSE, but if you don't need collision response , the obvious optimization can be made.

return 1;

Code:

`BOOL SegmentsIntersect(vector a1, vector a2, vector b1, vector b2)`

{

float a1yb1y = a1.y-b1.y;

float a1xb1x = a1.x-b1.x;

float a2xa1x = a2.x-a1.x;

float a2ya1y = a2.y-a1.y;

float crossa = a1yb1y*(b2.x-b1.x) - a1xb1x*(b2.y-b1.y);

float crossb = a2xa1x*(b2.y-b1.y) - a2ya1y*(b2.x-b1.x);

if ( crossb == 0 ) return NO;

if ( abs(crossa)>abs(crossb) || crossa*crossb<0 ) return NO;

crossa = a1yb1y*a2xa1x - a1xb1x*a2ya1y;

if ( abs(crossa)>abs(crossb) || crossa*crossb<0 ) return NO;

return YES;

}

int pnpoly(int npol, float *xp, float *yp, float x, float y)

{

int i, j, c = 0;

for (i = 0, j = npol-1; i < npol; j = i++) {

if ((((yp[i]<=y) && (y<yp[j])) ||

((yp[j]<=y) && (y<yp[i]))) &&

(x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))

c = !c;

}

return c;

}

**iDevGames Forums:**http://www.idevgames.com/forums/index.php**:**

Powered By MyBB, © 2002-2018 MyBB Group