We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everybody !
I'm having big problems to understand the maths behind camera rotation for short animations of terrain or planet flight by (like a kind of spatial vessel)
Following Daniel Shifman's lessons about vector I use vectors for position, velocity, acceleration and lookAt (and later implement applyForce to velocity to create some nice camera movements) I use the Processing camera() method
It's very basic but it works, except concerning rotations. I want my camera to rotate on Z axis and I really don't know how to do. It's not as simple as calling rotateZ(rad_angle)
I don't rotate on X or Y axis because I presume I should be able to achieve rotations by computing a new lookAt vector correctly.
Is the solution to play_with/compute/tweak the up vector ? And how ? I'm totally at loss...
Note : I've digged into quesycam, peasycam, OCD but that not helped me that much - and frankly I really want to code my own Camera class from a learning point of view.
Note 2 : sorry if this post is not very clear, that's very hard to formulate questions about a confusing subject ;-)
Answers
post your entire code
OK before we come to "Rotate a camera around Z axis":
here is a standard version where the camera position rotates in the X/Z plane around a fixed center (a Lamp post). Camera Y position (its height) is fixed.
The camera moves on a circle, calculated with cos and sin.
It looks as if the bar (Lamp post) rotates, but in fact only the camera position rotates.
The bar is fixed and the camera look at (Center) is fixed.
The camera position itself moves. UP vector is also fixed.
Forget the lamp post just look at function
CheckCameraSinusLevel()
.https://www.processing.org/reference/camera_.html
Based on this very simple code here comes
"Rotate a camera around Z axis".
The lamp post has not changed, it does not rotate.
Only camera position moves.
Rotating again but with a fixed Z and rotating in the X/Y plane. I think.
Since when the camera is directly above and directly under the Lamp post the camera would flip up side down we have to compensate this by saying up = up * -1; or something similar.
Forget the lamp post just look at function CheckCameraSinusLevel().
to explain the cos and sin formula here is a circle example.
Where the ellipses are shown on the circle circumference, the camera is placed in the examples above!
If you don't understand this 2D sketch you will have difficulties to understand the 3D sketches above...
(maybe I swapped cos and sin above, not sure what this means though)
This just means a phase change of a multiple of 90 degrees. Check trig identities here for instance: http://www2.clarku.edu/~djoyce/trig/identities.html. As a demonstration consider:
sin(a+b)=sin a x cos b + cos a x sin b
where x denotes multiplication. If we assigned b=90 deg then the above reduces to:sin (a+90)=sin a x cos(90) + cos a * sin(90)
Since
sin(90)
is 1 andcos(90)
is 0, then we havesin(a+90) = cos(a)
So switching your sin and cos just means you are introducing a phase shift. For a coder, it means that the reference for angles is not the standard observed in a Cartesian plane. For instance, using the code above:
Angle zero degrees is with respect to (wrt) the positive X axis. Increasing the angle means your angle increases CCW as in a proper Cartiesian coordinate system. However, as you know in Processing, the Y axis is inverted so the angle increases CW instead.
If you switch the sin and cos terms above, your zero degree reference is wrt the positive Y axis (Positive Y axis points down). The angle increases CCW.
So, in summary, you want to make sure you are defining your functions based on your provided coordinate system.
Kf
Paganini, are you talking about z axis as into / out of the screen, like roll?
That would require changing the up vector. The usual is using the y axis (0, 1, 0) but you can replace that with (sin a, cos a, 0) where a is the angle of rotation.
Thanks for you answers, I think I can formulate what I want to do in a simpler and better way :
1) I want to simulate a simplified plane flight - Me being inside the plane, pilot view
2) As shown here I need pan, tilt, roll : http://www.conitec.net/beta/aentity-pan.htm
3) koogs can you confirm that panning, tilting and rolling the camera is all about applying transformations to the UP vector ?
3) BUT what confuse me is that I don't want only to pan, tilt and roll the camera but also the trajectory of the plane. I mean if I only tilt I would "see" more sky but the trajectory of the plane will be still at the same altitude - I want to still look straight in front of me but having the plane going up on the slope.
4) The movements of the plane must combine, it could roll and tilt and turn left or right at the same time.
5) So, is it a UP vector AND velocity vector update question ?
6) If yes, what are the easier solutions to apply these transformations ? I know nothing about quaternions and transformation matrices. Any library I could use ? I search the Internet for hours but found no code to borrow, or that I could understand
7) Should I watch again and again https://youtube.com/watch?v=qMq-zd6hguc ? Is the answer there ? Applying angular motion on every axis ?
All of this is not for coding a realistic plane simulation but only small sequences of fake flights over landscapes, or in space that will be put together later in a video editing software (there is not graphic representation of the cookpit - the camera is at the nose of the plane)
My life would be easier if I wasn't so bad at maths ;-)
Do you have any code on this? A landscape?
No, just rolling is, and that's all you mentioned in the original question. (Chrisir, please note)
Panning a camera would involve moving the camera position and the look at by equal amounts.
But a plane is more about roll, pitch and yaw... Using gamestudio jargon is probably a bad idea given that you aren't using gamestudio, it'll just confuse people.
Then this should've been your original question.
Quick demo
plane doesn't really fly up and down but only forward left / right
use wasd plus mouse
I'm learning as i go. If I had the capacity to ask the "right" questions, I would find the answers by myself.
Chrisir I ran your code that seems a very good base to understand camera movements... Thanks a lot !
;-)