2

I just need a method to tell me whether an axis aligned bounding box in 3D intersects a line segment (not a ray) or not. I do not need the points of intersection.

The box is defined by 2 opposite corners, and the line segment by its start and end points, something like this:

Boolean intersection(Vector3 boxStart, Vector3 boxEnd, Vector3 segmentStart, Vector3 segmentEnd){...}

I've done a lot of research, and havent been able to find a code (in C# or Java hopefully) that I can understand or at least use. I need the method and not a library that will do the job...

My problem is that it needs to be 100% precise, and if the segment just touches the box (i.e. they share a single point), it has to return false. For example, if the segment is one of the edges of the box or passes though a corner, they DO NOT intersect.

Thanks

Nicolas
  • 2,297
  • 3
  • 28
  • 40
  • 1
    http://www.gamedev.net/topic/338987-aabb---line-segment-intersection-test/. If you want to exclude the "just touching" cases, use `<` and `>` tests. If you want to include them, use `<=` and `>=` tests. – Merlyn Morgan-Graham Aug 14 '11 at 03:55
  • @Merlyn the only thing I don't understand about that is the epsilon. I had found it, but it seems to me that it's not 100% accurate, and I dont know what epsilon to use... – Nicolas Aug 14 '11 at 08:50
  • "The epsilon is to deal with degenerate cross products, which can occur if the segment is nearly parallel to a box axis." - If you can detect the degenerate case without wasting a lot of cycles, then you could avoid using epsilon. – Merlyn Morgan-Graham Aug 14 '11 at 19:07
  • What if the segment is absolutely parallel? The "nearly parallel" case does not worry me that much... How do i define epsilon if I'm going to use it? just a double epsilon = 0.000000000001; ? – Nicolas Aug 14 '11 at 19:15
  • 1
    In C#, `double.Epsilon` and `float.Epsilon` are already defined. Not sure for Java... – Merlyn Morgan-Graham Aug 15 '11 at 08:03

1 Answers1

2

In Java, either of the intersects() methods is a candidate; but due to implementation limitations, you'd need to test it with Line2D.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Yeah but that is in 2D... How do I use that for 3D? – Nicolas Aug 14 '11 at 16:53
  • Ah, the [question changed](http://stackoverflow.com/posts/7054210/revisions) while I was researching the original. I wondered why other useful answers had been deleted. Does your chosen 3D library have a relevant primitive? – trashgod Aug 14 '11 at 17:54
  • You might look at the `getIntersection()` method used in this [Java 3D tutorial](http://www.google.com/url?sa=t&source=web&cd=2&ved=0CB4QFjAB&url=http%3A%2F%2Fwww.java3d.org%2Ftutorial.doc&rct=j&q=java%203d%20line%20intersection%20cube&ei=ZQ9IToXjGYXDgQe-r4jPDA&usg=AFQjCNEdlXXCnZIadjPBMq1ruEJINRCt6g&sig2=YFMnBKX2hiEOlUJeqV9KVQ). – trashgod Aug 14 '11 at 18:22