Distance between camera and objects


I work in cycling’74 MAX/MSP/JITTER so im used to applying objects that have already been made my cycling’74.

Now the opengl objects MAX provides wheren’t good enough for me anymore. I need a way to calculate the distance between the camera and points in a 3d space.

I will work in c++ to solve this problem, maybe you got some pointers for me where to look for this algoritme.

The data i got
x,y,z position of the camera
x,y,z,angle of the camera

x,y,z positions of objects 1…n

Now i need to find a way to sort this data in proper way so the most far away object becomes first in my list.

I thought i could simple use a absolute distance between the camare position and the objects position using a square root of x y and z each to the power of two.

But objects behind the camera are also rated like the ones in front so that wouldn’t work.

Do you fellowprogrammers got any tips on how to handle this?

After some more thinking i only need to know a way to check if an object is behind the camera.

When multplying these distances by -1 i can sort the list just fine. But how do i check if an objects is in front or behind the camere. distance won’t do because thats an absolute value.

  1. use qsort. It’s pretty fast. If you want it to be even faster, write your own qsort (so the comparison function can be inlined).
  2. Don’t use the square root. The ordering of the square of the distance is the same as the distance itself.
  3. Calculate the squared distance and whether or not the object is in view of the camera before you actually sort it. If the object is not in view, you may not want to sort it at all.

Squared distance: you already know how to calculate this, I’m sure.

In front/behind the camera: transform the distance vector by the inverse of the rotation matrix of the camera’s rotation. Since you’re not interested in two of the components, you only need the Z components from the matrix.
Assume a matrix:

 /  xx  xy  xz  \
 |  yx  yy  yz  |
 \  zx  zy  zz  /

xx being the X coordinate of the X unit vector,
xy being the X coordinate of the Y unit vector,
Normally you calculate the transformed vector by multiplying the x component with the X unit vector, the y component with the Y unit vector and the z component with the Z unit vector, and adding up those three resulting vectors.
But for front/back, you’re only interested in zx, zy and zz.
Which means the visibility check becomes:
dxzx + dyzy + dz*zz > 0
(or <0 depending on lefthanded/righthanded)

Because the position and rotation of the camera can also be in the z direction i should use the entire calculation wether an object is in front of behind the camera.

Right? :slight_smile:

I know im no hero in linear algebra. I tried to work out the above discription. I came no futher than
sd = dxdx + dydy + dzdz

Could you help me a bit futher with rotation matrices and this distance vector, the distance is only one number right?
Or is it the 3 delta’s i calculated?

And the rotation matrix, how can i get that from an x y z position and an x, y, z between 0 and 1 and an angle?

First day on 3d in years :slight_smile:

So i worked out my algoritm:

ax angle of rotation of camera over x
ay angle of rotation of camera over xy
az angle of rotation of camera over x

make an empty list
for each object in space
if ((-ADE+BF)dx+(ADF+BE)dy+(AC)dz)>0 then
sd = dxdx + dydy + dzdz
add sd to list

sort list.

I believe after a half of day’s work that’s it.