🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Line Collisions

Started by
4 comments, last by Michalson 24 years, 5 months ago
I''m looking for an algorithm to check for a collision between 2 lines (each line is defined by a set of X,Y cordinates). I''ve already tried one that just checks for a point of intersection but I''m having problems with paralelle lines, slope=0 and other special cases. Is there an easier way? I''m programming it in Object Pascal but if you can show me an example in C it would be just as good.
Advertisement
**Newbie Alert** (that''s me)
Good Question.

I would like to hear an answer also. I''m working on my first "Test Engine". Real simple stuff. My map only has one floor level (Y=-1) and all I do for collision detection is something like Bounding-Sphere-Overlaps-Line = Collide. Right now I can only detect collisions with vertical and horizontal lines. I would like to add some more sophisticated collision detection but there seems to be little documentation on the net or in books.

In my LevelWall type I have 4 floats that define the end points of a line. Since my level only has one floor height (and my character can''t jump) I only need to check for collisions in two dimensions.

In my collision detection algorithm I would like to define a sencond line made from two more points.

The first point defined by the players new x,z.

The second point would be made from the players rotation (rad) and his bounding sphere radius.

Then I would like to check to see if the lines intersect.

A detailed answer with some source (simple for my newbie brain) or a link to something good would not suck at all.

:-)
If you just want to know if they intersect, the easiest way would be to think of the coordinates as the left, top and right, bottom edges of a rectangle. Then all you have to do is check to see if the rectangles overlap with a few if statements. If you want to to know where they intersect, you have to use algebra and derive some sort of algorithm. I''m sure there''s one, and I know I''ve already used it, but I don''t have time to look for it.


Domini
Unfortunately I need to be working with true lines (not sprites) which can be at any angle. It''s like a 3D engine, but with only 2 dimensions (if that makes sense).
I made a similar program once, and yes: you''ll have to check those special cases. But don''t worry, there aren''t many of them (only two ones you listed).


-Hans
If the lines have are defined by two (x,y) coordinates, the algorithm will work. It''s not for sprites, but the same principle applies. If you want to check it, draw a few situations so you can visualize what is happening. Just think of the lines as diagonals of a rectangle.



Domini

This topic is closed to new replies.

Advertisement