Camera rotation
Hi there,
I have been working on a custom 3D engine for a while (like everyone else :)) - but this one is in flash. Just bear with me though...
I have a camera object with a 'position' vector and a 'target' vector. Using these two vectors I am trying to figure out the rotation of the camera (in radians) for each of the three axis. The camera actually never 'leans' over, so there is no rotation around the z axis (as yet) but I may add the 'up' vector (orientation vector) at a later stage.
Currently, my code is like this:
// subtract vectors: target-position to get resultant vector
fViewDirection = subtractV( fTarget, fPosition );
// calculate roll, pitch and yaw
fRotation.x = -Math.atan2(fViewDirection.y, fViewDirection.x);
fRotation.y = -Math.atan2(fViewDirection.z, fViewDirection.y);
fRotation.z = -Math.atan2(fViewDirection.x, fViewDirection.z);
I want the angles to be 0,0,0 when the camera is looking down the -ve z axis. The code above gives me some wierd results.
What am i doing wrong?
TIA
Peter
[Edited by - petervullings on July 8, 2004 7:33:49 PM]
Can I ask you why you even need 'rotation' vector? Becouse most engines work the other way around. They use rotation vector to calculate camera matrix and work form there on. Not the other way around :)
You should never let your fears become the boundaries of your dreams.
Hi,
I found out my Math IS correct. The problem was (even though I stated it in my original post) that the z rotation is always 0, yet I am attempting to calculate it. WHat happens is that at certain positions, the z rotation clicks around from 0 to 180 degrees and the scene was flipping upside down!
New code:
// subtract vectors: target-position to get resultant vector
fViewDirection = subtractV( fTarget, fPosition );
// calculate roll, pitch and yaw
fRotation.x = -Math.atan2(fViewDirection.y, fViewDirection.x);
fRotation.y = -Math.atan2(fViewDirection.z, fViewDirection.y);
fRotation.z = 0;
_DarkWIng_ - My engine isn't based on any existing technologies (such as OpenGL) because its in flash and I am doing everything from scratch, calculating the 3d points on the 2d surface, calculating normals and shading, and then drawing it using flash's 2d drawing commands (moveTo, lineTo, beginFill etc).
The reason I don't know the rotation vector up front is because my camera is specified using a 'position' and a 'target'. From this I need to work out the rotation vector. Movement of the camera results from moving the target or the camera position (or both) and so the camera rotation vector needs to be calculated again - the camera is not moved by manipulating the rotation vector directly (such as 'rotate left by 10 deg') but by moving the target ('aim at this new spot') and then working out what the rotation was.
I found out my Math IS correct. The problem was (even though I stated it in my original post) that the z rotation is always 0, yet I am attempting to calculate it. WHat happens is that at certain positions, the z rotation clicks around from 0 to 180 degrees and the scene was flipping upside down!
New code:
// subtract vectors: target-position to get resultant vector
fViewDirection = subtractV( fTarget, fPosition );
// calculate roll, pitch and yaw
fRotation.x = -Math.atan2(fViewDirection.y, fViewDirection.x);
fRotation.y = -Math.atan2(fViewDirection.z, fViewDirection.y);
fRotation.z = 0;
_DarkWIng_ - My engine isn't based on any existing technologies (such as OpenGL) because its in flash and I am doing everything from scratch, calculating the 3d points on the 2d surface, calculating normals and shading, and then drawing it using flash's 2d drawing commands (moveTo, lineTo, beginFill etc).
The reason I don't know the rotation vector up front is because my camera is specified using a 'position' and a 'target'. From this I need to work out the rotation vector. Movement of the camera results from moving the target or the camera position (or both) and so the camera rotation vector needs to be calculated again - the camera is not moved by manipulating the rotation vector directly (such as 'rotate left by 10 deg') but by moving the target ('aim at this new spot') and then working out what the rotation was.
Quote: Original post by petervullings
My engine isn't based on any existing technologies (such as OpenGL) because its in flash and I am doing everything from scratch, calculating the 3d points on the 2d surface, calculating normals and shading, and then drawing it using flash's 2d drawing commands (moveTo, lineTo, beginFill etc).
Wow, it looks very interesting. I wish you good luck because this is a really ambitious project and a very different thing.
If you suceed, I think you could be a real winner!
Previously "Krohm"
Hi Krohm - thanks for the kudos, but its been done before! Check out this one (in flash) - http://www.illogicz.com/flashmx/3dengine/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement