Advertisement

Lines collision test

Started by June 23, 2003 01:21 AM
5 comments, last by radekluner 21 years, 7 months ago
Does anybody know how can I test collision betwen 2 lines. I know their coordinates and that''s all. Please help me
With all due respect, you should solve this problem on your own. Its pretty trivial and you will be doing this type of thinking in graphics programming 'alot'. Think about it for a while or even draw it out on scratch paper. If you can't immediately solve it in 3d, then try it in 2d first. If you can't do 2d, then do it in 1d. All the solutions are extremely similiar and not difficult.

EDIT: If you still can't figure it out and want a hint then just post.


[edited by - haro on June 23, 2003 2:31:54 AM]
Advertisement
pretty simple formula:

intersection = STFW
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
I want only answer, no less, no more.
I know how can solve it but I need better faster algorithm.
I tried solve it with goniometrics functions but it''s to slow.
There is another algorithm based on 2 equations, but it don''t work at all time.







If you''re using goniometry to determine if two lines collides then you have no clue what you''re doing. THINK about the problem.

Here, I''ll tell you exactly how to do it since you refuse to make the problem as trivial as it is:

Set each linear equation into terms of a constant. Extract coefficients of all variables opposite the constant. Insert these coefficients into a matrix with a number of columns equivalent to the number of coefficients and a number of rows equivalent to the count of intersecting linear equations. We will call the first matrix "A". Create another matrix with 1 row and columns equivalent to the count of coefficients. The members of this matrix will be the respective variables for the coefficients. We will call this matrix "B". Finally create a third matrix equivalent in dimensions to B. In this matrix you will insert the constant values from the original equation rearrangements.

Now, calculate the determinate of matrix "A". If the determinate is non-zero then we can assume there is a location where the two linear equations are are equal. Otherwise we assume their is no collision. Now calculate the multiplicitive inverse of A. Once you have the multiplicitive inverse of A multiply it times the matrix B. The result of this multiplication will be the coordinates of your collision.

There you go!! I told you how to do it, step by step.
There''s a Sweet Snippet with code concertning intersection of two lines. It may be too slow for you to use but you can use it as a base for your own algo...

____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10. (source available)
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
you can solve it geometrically, and very simply.

It falls down to doing a line-plane test. You need to test one of the line with a plane containg the other segment, and the cross product of the two segment directions

line1(p, d) // position, directionline2(p, d)vector N = line1.d x line2.d // normal between the linesVector H = N x line1.d       // plane containing segment, and Nfloat  h = line1.p * Hfloat t2 = (h - (line2.p * H)) / (line2.d * H) // intersection of line2 with the planeVector P2 = line2.p + d * t2 // point on line2float d = ((line1.p - line2.p) * N) / (N*N) // 'distance' between lines. the sign might be opposite, and watch out, it's actually "distance / N.Maghitude()". Then it gets normalised in the equation below.Vector P1 = P2 + N * d // point on line 1



[edited by - oliii on June 24, 2003 9:13:50 PM]

Everything is better with Metal.

This topic is closed to new replies.

Advertisement