You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers. No questions about C++ programming or topics which are answered in the tutorials!
Im trying to deterimine the distance between the camera position and a bounding box. I want to know if the bounding box is in front or behind the camera.
//Determine the distance from the camera to the closest edge of the bounding box
AbsoluteTransformation.transformBoxEx(boundingBox);
pDriver->getTransform(irr::video::ETS_VIEW).transformBoxEx(boundingBox);
This way if i understand correctly:
boundingBox.MinEdge.Z <- should return the distance to the closest edge of the bounding box
boundingBox.MaxEdge.Z <- should return the distance to the furthest edge of the bounding box
would this be the right way of doing this or does anyone know of a better way?
I don't think I found an easy way to do this. One way you will probably be able to use is to get a vector to the center of the bounding box. Then reverse the vector and normalize it. Then you can multiply this new vector by the boundingBox.getExtent()/2 to get to the edge of the bounding box in that direction. Since the extent is the max distance between edges we divide by two to get the distance from center. Now you have a point on the box that is aligned between the start and the center of the bounding box. This isn't the closest point but is relatively close, more precision by adding all of the corners and testing the distance to those as well. This is how I would do it at least. Someone else may have a better solution for you.
To check if the bounding box is in front or behind, get the camera's view frustum's bounding box and check if the other bounding box is inside or intersects with it (built in irrlicht functions for those). If the object is inside the view frustum it is in front otherwise it is off to the side or behind (somewhere you can't see).
The min edge and max edge are the points that make up the box and should not be used as the closest and furthest points because they are relative to themselves and not based on the camera. These are just the two opposite corner points that create a box, you can use these and make a bunch of test points on the boxes actual edges and test the distance to each of these points to find which is smallest but I find that a waste of resources and time
If you need to know if an boject is IN FRONT or BEHIND the camera, the simplest way is to do a dot product between the view direction (camera target - camera position) and the vector defined between the camera position and the bounding box position (that is, object position-camera position). If it is positive, it is in front of the camera, else, is behind.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt