# Thread: What is a proper way to detect if a line in 3D space, has collided with another object?

1. First off, my collision algorithm will use bounding box for my collision, and a line(vector).
I have been thinking all day about this for several days, and yet, i can't think of a way to do this.

I understand, and have done collision detection between a point, a bounding box.

I now have a problem with vector's collision detection.

Kindly note:
1. I have Camera position.
2. I have the LookAt vector (this is a unit vector).
3. The minx,maxx,miny,maxy,minz,maxz of the bounding box.
4. The line in 3D space i am talking about is the line of the lookAt, passing through the camera position.
5. No collision must be detected for anything facing behind the camera.
You could assume that the line goes on to infinite, but if that is not possible, i would set a high magnitude for the line.

Google didn't come out with what I need.

Anyone care to contribute any suggestions of the methods that could be used?
Small/Big suggestions will help a lot.
Help is greatly appreciated!  2.

3. Such bounding boxes are called axis aligned bounding boxes, often abbreviated AABB. Searching for 3D AABB line collision gives better results: Ray-Box Intersection » www.scratchapixel.com.  4. I've written a pseudo code on how to do it, but i have a problem with the last part on collision detection with a plane.
This is my pseudo code:
In order find if there is an intersection between the ray and the 3D AABB:

The bounding box must have 6 faces and must be a cuboid:
1) The face with the min on x
2) The face with the max on x
3) The face with the min on y
4) The face with the max on y
5) The face with the min on z
6) The face with the max on z

3D AABB object collision function:
a) Test if the origin is in the box.
if min x is smaller than max x,
and min y is smaller than max y,
and min z is smaller than max z,
then the origin is in the box.
b) For each of the AABB faces, test if the ray may intersect with it:
c) Split each face (W) up into its UV coordinate system.
d) Pass in the value of the W face, and it's max and minimum U and V into the plane collision function to test for ray collision for each of these faces.
e) If the ray intercepts any single face of the AABB, there is collision with the AABB.
return true,else false.

AABB face collision function
a) For the U coordinate,
use V = U/V*W + CameraEye to get the V intercept of the AAplane.
b) For the V coordinate,
use U = V/U*W + CameraEye to get the U intercept of the AAplane.
c) If the V intercept is between min V and max V,
and the U intecept is between min U and max U,
then the ray intercepts the plane,
return true,else false
the problem i have is with the UV cartesian plane.
i don't exactly get what i need to calculate the intercept on that axis aligned plane.

anyway, i'm using the forumla y=mx+c to get the point where the ray intercepts the plane.
i don't know what is x in y=mx+c is supposed to mean, and how should i use it in the UV coordinates?

p.s. the pseudo code for the face collision function is messy, as it's nowhere near completion.  5. You need to apply y = m x + b to each coordinate, in which case the y and x variables are badly named, so rename them. You can instead say , and . You have a point at t = 0 (the starting point of your line) and depending on how your ray is defined you'll either have another point which you can set as t = 1, or you'll have the information to calculate the slopes. Either way, you then solve for t when you hit the plane in the one easy coordinate, and then plug that t into the other two coordinates to see where on the plane it hit.

Renaming the coordinates of your box faces is probably not worth doing as your going to have to rename the coordinates of your ray as well, which is going to get confusing unless you're really careful.

Also, your "test if origin is in box" code doesn't make sense as written. min x is always less than max x.  6. can i make the camera the origin, by getting all the new AABB vertices values by adding the old AABB vertices values with the camera position? so that the c (the cartesian origin is now the camera) in y=mx+c is zero?

or did i get the formula confused?

i wonder how to calculate c of the ray, as my camera is not at the origin. its tedious to do, so i was wondering if i could make my camera the origin.  7. You can if you want. I doubt it'll really be all that useful in the end though.  Bookmarks
 Posting Permissions
 You may not post new threads You may not post replies You may not post attachments You may not edit your posts   BB code is On Smilies are On [IMG] code is On [VIDEO] code is On HTML code is Off Trackbacks are Off Pingbacks are Off Refbacks are On Terms of Use Agreement