Calculate distance to an object3D

Started by Mr.Marbles, March 23, 2006, 03:43:50 PM

Previous topic - Next topic

Mr.Marbles

This my be a stupid question but does anyone know of a simple way to calculate the distance from the camera's position to an Object3D in the scene?

I've tried the following but the resulting values don't seem right:


SimpleVector v = object.getOrigin();
v.add(object.getTranslation());
distance = v.calcSub(camera.getPosition()).length();


Any suggestions :?:

EgonOlsen

Your formula doesn't take rotations of the camera into account.

dist=obj.getTransformedCenter().calcSub(camera.getPosition()).length();

Mr.Marbles

EgonOlsen,

Quote
dist=obj.getTransformedCenter().calcSub(camera.getPosition()).length();

This is returning the same values as the code that I was using. The reason why it doesn't seem to work right is because objects that are far return smaller values than closer objects. Is this correct?

EgonOlsen

Quote from: "Mr.Marbles"The reason why it doesn't seem to work right is because objects that are far return smaller values than closer objects. Is this correct?
No, that isn't correct of course. But the code is correct, i can't spot anything wrong with it. Are you sure that the error is not somewhere else in your code? Maybe printing out the position of the camera and the transformed centers of the objects in question will help to understand better what's going on here...

Mr.Marbles

The problem is in fact the transformed centers of the objects, they're always reading (0,0,0)  :!:  Why would this be? The objects are in the right positions in the rendered scene. It's probably a timing issue. When should I be calling the code to calculate the distance?

EgonOlsen

Quote from: "Mr.Marbles"The problem is in fact the transformed centers of the objects, they're always reading (0,0,0)  :!:  Why would this be? The objects are in the right positions in the rendered scene. It's probably a timing issue. When should I be calling the code to calculate the distance?
It shouldn't matter when you call it, unless you are resetting the translation every frame or something similar. Why should the centers be at (0,0,0)? Are you calling build() on the objects? Are you setting the center by yourself somewhere in your code and if yes, to which value? Where is the camera located?

Edit: Removed a *not* where it didn't belong

Mr.Marbles

Found the problem :!:  I was calling build() on the objects before setting their initial positions, like this:


// ---- BAD ----//
object.build();
object.translate(initialLocation);
object.translateMesh();
object.rotateMesh();
object.setTranslationMatrix(new Matrix());
object.setRotationMatrix(new Matrix());


And I changed it to this:


// ---- GOOD ----//
object.translate(initialLocation);
object.translateMesh();
object.rotateMesh();
object.setTranslationMatrix(new Matrix());
object.setRotationMatrix(new Matrix());
object.build();


Now the objects have the right positions. But I still don't understand why they were rendered properly before with the bad initialization  :?