• Advertisement
Sign in to follow this  

Line intersection help...

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

If you intended to correct an error in the post then please contact us.

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 (QuadInfo[j].InUse)
		{
			if (j != Skip)
			{
				From[0] = QuadInfo[j].QuadVerts[0].x;
				From[1] = QuadInfo[j].QuadVerts[0].y;
				To[0] = QuadInfo[j].QuadVerts[1].x;
				To[1] = QuadInfo[j].QuadVerts[1].y;
				if (SegmentsIntersect(vStart, vFinish, From, To))
					return true;

				From[0] = QuadInfo[j].QuadVerts[1].x;
				From[1] = QuadInfo[j].QuadVerts[1].y;
				To[0] = QuadInfo[j].QuadVerts[2].x;
				To[1] = QuadInfo[j].QuadVerts[2].y;
				if (SegmentsIntersect(vStart, vFinish, From, To))
					return true;

				From[0] = QuadInfo[j].QuadVerts[2].x;
				From[1] = QuadInfo[j].QuadVerts[2].y;
				To[0] = QuadInfo[j].QuadVerts[3].x;
				To[1] = QuadInfo[j].QuadVerts[3].y;
				if (SegmentsIntersect(vStart, vFinish, From, To))
					return true;

				From[0] = QuadInfo[j].QuadVerts[3].x;
				From[1] = QuadInfo[j].QuadVerts[3].y;
				To[0] = QuadInfo[j].QuadVerts[0].x;
				To[1] = QuadInfo[j].QuadVerts[0].y;
				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 (QuadInfo[j].InUse)
		{
			BlockedShot = ScanMe(vStart, QuadInfo[j].newLoc, j);
			if (!BlockedShot)
			{
				return j;
			}
		}
	}
	return -1;
}

Now, I have an array of quads whose vertices are stored in 'QuadVerts' (a CUSTOMVERTEX struct). The quad's actual X/Y location is stored in 'QuadInfo[j].newLoc[2]'. What I'm trying to do is verify a clear trace along a line between one game object (coordinates are stored in 'vStart[2]') and another game object (one of the quads in the array, stored in 'newLoc[2]'). Here's how I do it: 1) Store a 'start' X/Y location in 'vStart[2]'. 2) Loop through the array of quads in the function 'SimpleTrace()'. 3) Send each quad's X/Y location, along with vStart, to the function 'ScanMe()'. 4) 'ScanMe()' also loops through the array of quads, except this function draws a line between each vertex in each quad and tests to see if it intersects with the line between 'vStart' and the tested quad's X/Y location. 5) 'ScanMe()' returns TRUE if anything is detected between vStart and the tested quad's X/Y location. It returns FALSE if there's nothing between vStart and the tested quad. However, my above functions don't seem to do the job very well. Can anybody help me on this? I will give you more information if I didn't post a very good explanation. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement