www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: Mr.Marbles on March 23, 2006, 03:43:50 pm

Title: Calculate distance to an object3D
Post by: Mr.Marbles on March 23, 2006, 03:43:50 pm
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:

Code: [Select]

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


Any suggestions :?:
Title: Calculate distance to an object3D
Post by: EgonOlsen on March 23, 2006, 04:29:22 pm
Your formula doesn't take rotations of the camera into account.
Code: [Select]

dist=obj.getTransformedCenter().calcSub(camera.getPosition()).length();
Title: Calculate distance to an object3D
Post by: Mr.Marbles on March 23, 2006, 04:39:27 pm
EgonOlsen,

Quote
Code: [Select]

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?
Title: Calculate distance to an object3D
Post by: EgonOlsen on March 23, 2006, 06:16:19 pm
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...
Title: Calculate distance to an object3D
Post by: Mr.Marbles on March 23, 2006, 06:57:17 pm
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?
Title: Calculate distance to an object3D
Post by: EgonOlsen on March 23, 2006, 07:07:38 pm
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
Title: Calculate distance to an object3D
Post by: Mr.Marbles on March 23, 2006, 07:39:21 pm
Found the problem :!:  I was calling build() on the objects before setting their initial positions, like this:

Code: [Select]

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


And I changed it to this:

Code: [Select]

// ---- 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  :?