Jump to content
  • Advertisement
Sign in to follow this  

line segment colision

This topic is 3756 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

I'm new to this kind of programming, someone can help me with colision between 2 line segments, and know where they intersect (they are determined by 2 points) Thanks.

Share this post

Link to post
Share on other sites
Here is a function that finds the intersection point between 2 line segments in 2D. The function will return FALSE if line segments do not intersect.

A, B = points on line 1
C, D = points on line 2
Q = intersection point

BOOL IntersectionLineSegments(vec *Q, vec A, vec B, vec C, vec D)
vec U = B - A;
vec V = D - C;
vec W = C - A;

float det = U.x*V.y-V.x*U.y;

if (det == 0)
return FALSE; // parallel, no intersection

float t = (W.x*U.y-U.x*W.y) / det;
float s = (W.x*V.y-V.x*W.y) / det;

if ((t < 0.0f || t > 1.0f) || (s < 0.0f || s > 1.0f))
return FALSE; // outside line segment

*Q = C + V*t; // intersection point

return TRUE; // line segments intersect

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!