PDA

View Full Version : distance calculating with box

 JokerMartini06-13-2011, 06:29 PMI'm trying to calculate the end of the environment based on the camera. Is there a way to find the furthest distance of the box property of a given object? Right now I'm doing distance \$camera.pos \$plane001.pos Which does not help because the pivot for the plane is at 0,0,0 when in reality its furthest point could be 100,0,0 I want to make this a universal tool so making it somehow calculate the furthers distance or corner of the box property of the targeted object would work best I think. Unless anyone has some other ideas I'd be glad to hear them and figure something out here. http://forums.cgsociety.org/attachment.php?attachmentid=162459&stc=1
denisT
06-13-2011, 07:28 PM
the most simple way is to find the longest distance from the camera to one of 8 corners of the object's bounding box.
if you want to do it more accurate check the distance to every vertex of the object.

JokerMartini
06-13-2011, 08:32 PM
How do I get all 8 corners?
From what I'm reading I can only get the max and min. Opposite corners from one another.....

denisT
06-13-2011, 08:44 PM
How do I get all 8 corners?

I can only get the max and min...

is it not enough?

JokerMartini
06-14-2011, 03:47 PM
I actually went through and set that all up lastnight and it's not going to really work for what I'm trying to do. Because if the object is really tall then it throws off the distance for z depth.
I'll figure something out here.

Swordslayer
06-14-2011, 04:25 PM
If you don't want to check vertex by vertex excluding those out of camera cone (which still is not a perfect solution anyway), you can get the "maximum" z coordinate in the camera space – (nodeGetBoundingBox obj camera.transform)[1].z – and to be on the safe side divide it by cosinus of a half of camera's FOV angle.

3rd Dimentia
06-15-2011, 03:02 AM
I've been working on something very similar to this on and off for a week or so. Mine is for calculating scene extents for setting an upper and lower limit for a Zdepth pass. I'm doing 2 passes per frame. I'm first calculating all the objects who's bounding box extents are smaller than the object's CENTER (not pivot point) distance from camera and testing the distance from cam to the Bbox corners. Then all the objects that are larger, like groundplanes I'm doing a ray-mesh test after subdividing the camera view. (brute force raycaster style) . The latter is quite slow to do so limiting it to objects that are too large to nicely test the Bbox corner distance was my way to get around that. The thing with the way you seem to be testing could cause problems as the distance to the corners of the mesh behind the camera might be a lot further away from the camera than the geo actually visible thru the cam. That is, if you're trying to do the same thing as I am.

Anyway, to answer your question about how to get the bounding box corners... here's my function that does that.

fn getBBoxCorners obj =
(
bb = nodeGetBoundingBox obj obj.transform
xyz = bb[2] - bb[1]
bBoxCorners = #()
for x = -.5 to .5 do for y = -.5 to .5 do for z = -.5 to .5 do append bBoxCorners (([ (xyz[1]*x) , (xyz[2]*y) , (xyz[3]*z) ]-(obj.pos-obj.center)) * obj.transform )
bBoxCorners
)

3rd Dimentia
06-15-2011, 03:05 AM
Oh I forgot to say that to start with on each frame I collect all objects that are actually visible from the camera for that frame.

3rd Dimentia
06-15-2011, 03:38 AM
OK, I just tested that function and it doesn't seem to work 100%. I've just had a look at it and I had the order of the transform multiply and the position offset 'round the wrong way.
This seems to work now. Even on objects that are non-uniformally scaled, even skewed.

fn getBBoxCorners obj =
(
bb = nodeGetBoundingBox obj obj.transform
xyz = bb[2] - bb[1]
bBoxCorners = #()
for x = -.5 to .5 do for y = -.5 to .5 do for z = -.5 to .5 do append bBoxCorners ((([ (xyz[1]*x) , (xyz[2]*y) , (xyz[3]*z) ]) * obj.transform )-(obj.pos-obj.center))
bBoxCorners
)

CGTalk Moderation
06-15-2011, 03:38 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.