• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
sgdimitris

Intersection of two line segment in 3 dimensions

7 posts in this topic

Hello to all users,
I have a simple question for more of us.
I am developing a project and I need to calculate if two line segment intersect.
is there any mathematical way to do that?
With more details:
Segment_1 is from point A to point B, where A=(X_a, Y_a, Z_a) and B=(X_b, Y_b, Z_b)
Segment_2 is from point C to point D, where C=(X_c, Y_c, Z_c) and D=(X_d, Y_d, Z_d)
Is there any condition between the points coordinates that determines if the two segments intersect?
Thanks in advance
0

Share this post


Link to post
Share on other sites
When doing computational line-line tests in 3D using floating point, the typical path is as follows:

1. Compute the closest pair of points on both lines. See e.g. [url="http://clb.demon.fi/MathGeoLib/docs/Line_ClosestPointLineLine.php"]MathGeoLib[/url]. ([url="http://clb.demon.fi/MathGeoLib/docs/Line.cpp_code.html#59"]link to code[/url]).
2. If their distance is smaller than some arbitrary small threshold value (e.g. 0.001f), consider the lines intersecting, false otherwise. [url="http://clb.demon.fi/MathGeoLib/docs/float3.cpp_code.html#341"]Vector distance[/url].

The threshold value effectively transforms the two lines into infinitely long cylinders, and tests them for collision.

Edit: As pointed out below, just noticed the question was for determining intersection between a pair of line <b>segments</b>. For that case, the same method above is used, but computing the closest point pairs in #1 requires clamping the points onto the line segment range. See [url="http://clb.demon.fi/MathGeoLib/docs/LineSegment_ClosestPoint.php"]LineSegment::ClosestPoint(LineSegment)[/url] ([url="http://clb.demon.fi/MathGeoLib/docs/LineSegment.cpp_code.html#162"]code[/url]) in MathGeoLib. Edited by clb
1

Share this post


Link to post
Share on other sites
Calculate the following values, where x means cross product:

T1 = B - A
M1 = A x B
T2 = D - C
M2 = C x D

Then the two lines intersect if and only if

T1 * M2 + T2 * M1 = 0

where * means the dot product.

Of course, in floating-point calculations, you're unlikely to see a value of exactly zero, so you'd want to instead check for values very close to zero.

If you'd like to know more about where the above stuff comes from, look into Grassmann algebra and homogeneous coordinates. Here's a presentation:

http://www.terathon.com/gdc12_lengyel.pdf
1

Share this post


Link to post
Share on other sites
Aren't those both line-line tests and not line segment-line segment tests?
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1337525908' post='4941647']
Aren't those both line-line tests and not line segment-line segment tests?
[/quote]

Yes, you're right. I must have been to sleepy to see the word "segment".
1

Share this post


Link to post
Share on other sites
Loosely speaking, a primitive is generally defined by an equation(s), and the set of solutions to that equation(s) defines where a specific instance of that primitive exists.

Two instances of primitives overlap where their two sets of solutions overlap (ie. literally, [url="http://en.wikipedia.org/wiki/Intersection_%28set_theory%29"]intersect[/url]).

That said, do you really think that two line segments in 3D are likely to ever intersect? They really wouldn't be likely to do so, if you think about it, which is why clb gave you the hint to go a different route and instead get the shortest "co-perpendicular" line segment that spans between them. Here's a nice link explaining the general idea, in English (and codelish), for two line segments in 3D:

[url="http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/"]http://local.wasp.uw...try/lineline3d/[/url]

Bourke has some seriously great things on his website. Note how he explains that both a line and a line segment can be defined parametrically by two points and a scalar, in the exact same form. Like Bourke says, the scalar has an interval of [0, 1] for the line segment, and an interval of [eqn](-\infty, \infty)[/eqn] for the line. Same difference.

In general, just remember that it's the set of solutions that defines where a specific instance of a primitive exists, and that the overlap of solutions is where two specific instances of primitives intersect. If you still don't quite get what I mean, then know that you'd likely get a better sense of it if you work through a [url="http://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection"]line-circle/sphere intersection test[/url] . Don't be confused by the fact that they define the line using a point and a unit vector; it's basically the same thing that Bourke did, but in a slightly different dressing. You'd be very well off to figure out how to convert between the two forms. Tinker.

Once you really get the idea in general, you'd likely be able to write your own intersection tests on the fly. Edited by taby
0

Share this post


Link to post
Share on other sites
Indeed, as other posters said the exact intersection between two lines is very easy (simple algebra), but due to floating point errors etc two lines in 3D are very unlikely to intersect exactly. As far as line *segments*, once you find the intersection of the infinite lines, just check the intersection point against each line segment to see if it's in range. For example, if you turn the line equation into a parametric equation based on t, where t=0 is one endpoint and t=1 is the other endpoint, then any point where t<0 or t>1 is not on the line segment.
1

Share this post


Link to post
Share on other sites
[quote name='jefferytitan' timestamp='1337548994' post='4941724']
Indeed, as other posters said the exact intersection between two lines is very easy (simple algebra), but due to floating point errors etc two lines in 3D are very unlikely to intersect exactly.
[/quote]

That's true regarding floating point precision, and thanks for adding that, because it is technically important. That said, it could go both ways, because sometimes there may be non-intersecting lines that are erroneously found to be intersecting because the non-zero shortest distance between them was whittled down to zero due to limited precision. What I'm saying is that you bring up a good point, and I thank you for that.

I should be more specific a lot of times: I was more referring to how the parallel postulate doesn't really apply in 3D as it does in 2D. Most cases in 3D are skew lines, where the lines are not parallel but yet still fail to intersect. It's the extra, third spatial dimension that makes the critical difference here. Now, two non-parallel planes in 3D do intersect, and it's pretty much because the extra, third spatial dimension is accompanied by the extra, second dimension inherent to the primitive in question (ie. the plane is 2D, not 1D like the line is). I honestly wish I knew the name of the theorem / postulate / axiom behind this, but it's escaping me. Maybe it's just called [url="http://answers.yahoo.com/question/index?qid=20070308212519AAo62Pw"]the generalized parallel postulate for Euclidean space[/url], or [url="http://en.wikipedia.org/wiki/Skew_lines#Skew_flats_in_higher_dimensions"]something[/url], I [url="http://en.wikipedia.org/wiki/Ignorance_is_bliss_%28disambiguation%29"]dunno[/url]. I guess the general rule of thumb is: For a pair of nD "plane" primitives, you need more than 2n embedding dimensions for the primitives to be able to be skew. So, for a pair of 1D lines you'd need a 3D space for them to be skew, and for a pair of 2D planes you'd need a 5D space for them to be skew, and for a pair of 3D hyperplanes you'd need a 7D space for them to be skew, etc, etc.

Sorry, just going around clarifying stuff that I wrote earlier. It was my fault that I didn't do so earlier. Edited by taby
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0