# Line intersection help...

This topic is 5159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, this 2D intersect problem is really making my head ache...
bool SegmentsIntersect(float *a1, float *a2, float *b1, float *b2)
{
float a1yb1y = a1[1]-b1[1];
float a1xb1x = a1[0]-b1[0];
float a2xa1x = a2[0]-a1[0];
float a2ya1y = a2[1]-a1[1];

float crossa = a1yb1y*(b2[0]-b1[0]) - a1xb1x*(b2[1]-b1[1]);
float crossb = a2xa1x*(b2[1]-b1[1]) - a2ya1y*(b2[0]-b1[0]);
if ( crossb == 0 ) return false;
if ( fabs(crossa)>fabs(crossb) || crossa*crossb<0 ) return false;

crossa = a1yb1y*a2xa1x - a1xb1x*a2ya1y;
if ( fabs(crossa)>fabs(crossb) || crossa*crossb<0 ) return false;

return true;
}

bool ScanMe(float* vStart, float* vFinish, int Skip)
{
float From[2], To[2];

for (int j = 0; j < MAX_QUADS; j++)
{
{
if (j != Skip)
{
if (SegmentsIntersect(vStart, vFinish, From, To))
return true;

if (SegmentsIntersect(vStart, vFinish, From, To))
return true;

if (SegmentsIntersect(vStart, vFinish, From, To))
return true;

if (SegmentsIntersect(vStart, vFinish, From, To))
return true;
}
}
}
return false;
}

int SimpleTrace(float* vStart)
{
bool BlockedShot;

for (int j = 0; j < MAX_QUADS; j++)
{
{
if (!BlockedShot)
{
return j;
}
}
}
return -1;
}

##### Share on other sites
Whoops, sorry about the wild goose chase. Everything works in the above code, except in my game I have a resolution multiplier that I forgot to implement. After I plugged it in everything worked fine. One word - D'OH!

Thanks again for all the help! [smile]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 14
• 18
• 16
• 11
• 9