# Distance Calculation

How do I calculate the distance between coordinates of a rotated object and eye position ?

I am not the math expert, so please explain with easy formula or codes.

Well i’m not sure i’ve understood the question, but why don’t you use pythagor’s formula:
distance between (x1,y1,z1) and (x2,y2,z2)
= sqrt ((x1-x2)(x1-x2)+((y1-y2)(y1-y2)+((z1-z2)*(z1-z2))
For the sqrt function, include <math.h>.
I don’t see why you talk about a rotated object : rotation preserves distances, so you don’t need to take care of rotations when you are computing distances. Good luck !

What I meant was not just rounding the object at its center.

Say I have a cube at (10,10, 0) and rotate 20 degrees around x axis and 30 degrees for z axis centering (100,100,40).
Then what is the distance between cube and me?

I don’t know if theres any way to access it but thats the info the Z buffer holds.

Otherwise the previously mentioned distance formula will have to be used. Maybe if you use glLookAt to do the movement of the camera you can then do a distance check to the object, Try doing one to each vertex then take the shortest values as the correct distance.

You have to multiply your object initial coordonates by the modelview matrix. Then you obtain your transformed coordinates. You obtain the distance in one soustraction (eyepos - transformCubePos)

Someone taught me how to solve the problem by using the viewing frustum.

It looked like what I wanted.

Thanks everyone.

Ok… if you know coordinates of your object in the window, you can do gluUnProject to find the object coord BEFORE projection. If you just a depth clamped between 0 and 1, you can readback the depth buffer with glReadPixel.