2d line intersection giving me grief

Started by
6 comments, last by Damocles 21 years, 8 months ago
I posted this on the tail of the other thread I started, but it didn''t seem to get much response so now I''m hoping maybe someone will be kind enough to help me sort out this little problem I''m having. Having gone over and over and over and over my code, I have come to the conclusion that the problem must lie in my 2d line intersection code as it''s the only part of the code that I don''t understand 100% Basically, I use two boxes for the collision boundaries of the player object. In the picture below, the white lines represent the collision shapes and the red lines indicate where the collisions actually occur. As you can see, the upper left corner is screwed. The collisions are occuring too far out So here''s my line intersection detection code:
  
bool ACollision::LineIntersect(float ax, float ay, float bx, float by,
				float cx, float cy, float dx, float dy)
{
	// line intersection test

	float s = (cy - dy) * (bx - ax) - (cx - dx) * (by - ay);
	if (s == 0)
	{
		// Lines don''t intersect

		return false;
	}
	s = ((cy - ay) * (bx - ax) - (cx - ax) * (by - ay)) / s;
	if (s < 0 || s > 1)
	{
		// Lines don''t intersect

		return false;
	}
	return true;
}
  
The two lines run from (ax,ay) to (bx,by) and from (cx,cy) to (dx,dy). Any idea where the problem might lie? I don''t fully understand how those two tests can detect all the possible line intersections, but that''s what was posted in another thread about 2d line intersection and was said to have been tested and worked.
------------------------------------------[New Delta Games] | [Sliders]
Advertisement
I''ve always used vector-based line collision detection, so I''m not familiar with the parametric version. I think there might be an error there.
Zipster, I''ve never used/seen vector based intersection before, could you post an example please (I can easily convert from line to vector in my code)
------------------------------------------[New Delta Games] | [Sliders]
I think your first test for parallel lines should be:

float s = (cy - dy) * (bx - ax) - (dx - cx) * (by - ay);

where cx-dx was changed to dx-cx.

I don''t understand your second test.
Nope, that didn''t help any. In fact it made it worse The collisions using your replacement test now still have the upper left corner problem and the lower line collisions seem to have moved downards.
------------------------------------------[New Delta Games] | [Sliders]
I based that on the C code in this tutorial:

http://geometryalgorithms.com/Archive/algorithm_0104/algorithm_0104B.htm#intersect2D_SegSeg()

Your second test looks like their skewed line test at the bottom, except they do one test for each line.
Check section 1.03 of comp.graphics.algorithms FAQ.

http://www.faqs.org/faqs/graphics/algorithms-faq/

I wrote a 2d line intersection function based on that and it works.

Jack



[edited by - JackNathan on August 21, 2002 3:34:30 PM]
Vector-based line collision involves calculating the line normals, and through a series of dot products, determining whether the points on line A are on opposite sides of line B, and the points on line B are on opposite sides of line A. If both conditions are true, then the lines intersect.

If you want to find out exactly where the lines intersect, and not just if they exist, it''s a bit more involved, and the parametric code is actually a lot faster and cleaner.

This topic is closed to new replies.

Advertisement