Converting from World space to Camera space

5 replies to this topic

#1oscarfh

New Member

• Members
• 11 posts

Posted 06 December 2008 - 09:13 PM

Hi all,
I am trying to do some work for the University, but I am stuck. I would apperciate any help. (I'm not asking to some do it for me).
My problem is the following:
I have these values:
Target - that is the target point of the camera
Elevation - elevation angle in degrees
Azimuth - azimuth angle in degrees
Distance - distance beetween the camera/eye and the target

I have to generate a homogene 4x4 matrix wich transform from world space to camera space.
I tried to use some Formula that is:

x' = (-sin(azimuth);
cos(azimuth);
0)

y' = (-cos(azimuth)*sin(elevation);
-sin(azimuth)*cos(elevation);
cos(elevation))

z' = (cos(azimuth)*cos(elevation);
sin(azimuth)*cos(elevation);
sin(elevation)).

and the Matrix would be
V=[x',0;y',0;z',0;target,0]

but it is not working.

Does anyone know how a formula to implement, using these arguments, to generate a Matrix wich transform from world space to camera space?
Any help would be appreciated, I am really stuck.

thank you,
Oscar

#2Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 06 December 2008 - 09:48 PM

You didn't mention what kind of camera are you trying to create, but from your parameters I'm guessing an orbiting camera, that is centered on the target point and rotates around it. (BTW, it's azimuth, not azimut.)

Anyway, I think way to do this is to approach it as a sequence of transformations. To create an orbiting camera, you want to first translate so that the target point moves to the origin, then rotate so that the camera's view direction moves to the Z axis, then finally translate along the Z axis by the orbit radius so that the camera arrives at the origin.

You'll need to create a matrix for each of these, then multiply them together. The formulas for individual transformations are here. (Note that if you're using a row vector convention you'll need to transpose the matrices on that page.)
reedbeta.com - developer blog, OpenGL demos, and other projects

#3oscarfh

New Member

• Members
• 11 posts

Posted 06 December 2008 - 10:27 PM

Yes,
I am sorry, I forgot to mention that. It is really an Orbiting Camera.
And thank you for the spell correction, next time I will write it right.

thank you a lot for your help.
Oscar

edit: it endeed worked.. thank you a lot.

#4oscarfh

New Member

• Members
• 11 posts

Posted 08 December 2008 - 02:43 AM

I am sorry for continuing this post, but I have some problems that are similar to this one.
What I need to do now, is get the X and Y axis from the camera and convert to World space coordinates in order to calculate the camera in a "strafe" view. (Strafe as in FPS game).
The calculation of the camera is already done, but how can I get the axis in the camera and transform it to world coordinates? I tried to get the first and second columns of the Modlel View Matrix and calculate with the inverse Matrix of the post above, but it didn't work. What I have as a strange camera that performs loops, or something. I need it to go up, down, strafe left and right.

Any help?

Thank you,
Oscar

#5Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 08 December 2008 - 03:03 AM

You can extract the axes from the inverse of the world-to-camera matrix (i.e. the camera-to-world matrix). Assuming you're using OpenGL, the first column will be the right direction, and the second will be up. The third column is backward.
reedbeta.com - developer blog, OpenGL demos, and other projects

#6oscarfh

New Member

• Members
• 11 posts

Posted 08 December 2008 - 11:55 PM

Hello,
Thank you again for your help.
What I did was: I calculated the World to Camera matrix, and used the first 2 columns, the first one for Left/Right movement and the second for up/down.
I didn't used the inverse, but, to have the same behavior, I had to negate the values in the vectors.