Airplane first "person" camera?

Started by
8 comments, last by crippeli 18 years, 7 months ago
Hello there chaps. :) I'm wondering how to do an airplane-like camera. By airplane-like I mean it should be able to rotate around the z-axis (when pressing the left and right keys, the plane should "roll"), and it should "pull up" in the direction that the top of the plane faces when you press the down-key. For example, the plane is flying straight forward with the top of the plane facing up. Now you press the right key, and rotate the airplane 180 degrees so that the top is facing the ground (or whatever). When you press the downbutton, the plane should pull towards the ground. This was kind of hard for me to explain (english is not my primary language), but i hope some of you understood what i meant. :) Is there an easy (or hard) way to do this? Thanks in advance. /Crippeli
Advertisement
You have the planes "up" vector... why not use it? Rotating the planes "forward" vector towards "up"

Thats the simple method I suppose... in a realistic system you'd be simulating the flaps and whatnot of the plane and the correct rotation would be a byproduct of your accurate physics simulation.

- Rockoon (Joseph Koss)
I'll try that, thanks. :)

I'm not too good with matrix math, and rotating stuff (beginner), how would I go about rotating the upvector (when rolling) and rotate towards the upvector when pulling up? I'm guessing this has to be done by manually multiplying the vectors with a rotation matrix?

/Crippeli
Quick early-morning answer: Look here for a good description of how to do this, and here for some tutorial code I wrote that implements this technique.

There are various ways to do this - using vectors (as in the above examples), or by maintaining a matrix or quaternion and applying local-axis rotations. I would try to adopt one of the latter solutions eventually, but the vector method might help get you started more quickly. Also, the matrix and vector forms are basically equivalent, so once you're more comfortable with matrix math you should be able to adapt easily.
If you are using OpenGL the *easiest* way is by using GluLookAt. But I don't want to discourage from learning about quaternions, you'll need it later when you have to rotate your "plane" itself!
Short answer:

- Represent your aircraft's world position + orientation with a matrix

- use the 3rd row as the axis to build the rotation matrix for a roll

- use the 2nd row as axis for yaw

- and 1st for pitch!

You do nto have to use quaternions!



If you need an explanation, ask me .. as the explanation could be very lengthy.



Count your blessings before you count your problems.www.wiu.edu/users/muaiq
Thanks for the answers guys. :)
I've read the Dorian Research article (twice), and looked some at jyk's code.
Now, i'm trying to make this work with my own little camera class, but i'm having some trouble. The rolling rotation works fine, but the pitch doesn't quite work the way it should. It rotates around the same axis all the time, and not towards/away from the up axis. Anyway, here is some code. Maybe you can spot what i'm doing wrong. :)

void CCamera::DoRotations(){	float c, s;	CVector tmpUp, tmpFwd, tmpLeft;	Up = CVector(0.0, 1.0, 0.0);	Forward = CVector(0.0, 0.0, -1.0);        Left = CVector(-1.0, 0.0, 0.0);		c = cos(PitchAngle * DEGCONV);	s = sin(PitchAngle * DEGCONV);		tmpFwd = Forward * c - Up * s;	tmpUp = Up * c + Forward * s;		Forward = tmpFwd;	Up = tmpUp;		c = cos(RollAngle * DEGCONV);	s = sin(RollAngle * DEGCONV);		tmpLeft = Left * c + Up * s;	tmpUp = Up * c - Left * s;			Up = tmpUp;	Left = tmpLeft;	}	void CCamera::ApplyCamera(){	DoRotations();	gluLookAt(Position.X, Position.Y, Position.Z, 	          Forward.X, Forward.Y, Forward.Z,	          Up.X, Up.Y, Up.Z);}


Edit: Forgot to mention that I use gluLookAt for the camera. I figured this would work, but i'm probably wrong. :)

RollAngle and PitchAngle are modified from keyboard input.
Any Ideas? :)

/Crippeli
Hehe, it's because i'm resetting the upvector before doing pitch rotations.

*Insert Homer-comments here*

Okay, so now the rotations work. But the rotation "speed" keeps increasing the more i rotate, until it goes so fast it looks choppy. That was why I reset the vectors before rotation (which fixed the acceleration-problem, but messed up the rotation). What should I do about this?

When getting user input and setting the rotation angles I limit them from 0.0 to 360.0. Has this got to do with orthonormalization? Because i haven't implemented that in my code yet. I'm not exactly sure what orthonormalization means. :/

/Crippeli

[Edited by - crippeli on September 6, 2005 5:57:57 AM]
First of all, I think it should probably be:
gluLookAt(    Position.X, Position.Y, Position.Z,     Position.X+Forward.X, Position.Y+Forward.Y, Position.Z+Forward.Z,    Up.X, Up.Y, Up.Z);
Orthonormalization means to make your basis vectors mutually perpendicular and unit length. There's a function in my code which will show you how to do that.

Also, are you applying yaw somewhere? I didn't see it in your code. And, as I take it you've already figured out, the whole idea is to only initialize your basis vectors once, at the beginning of the simulation, not reset them every frame.

If you're still having trouble, perhaps you could post your code in its current form.
Thanks for noticing my mistake with the call to gluLookAt. I haven't tried moving my camera around just yet, so I didn't notice anything weird. :)

As for the yaw rotation I apply it just before the other two, I forgot to paste it when I made that post because it was commented out at the time for some reason.

I figured out that the acceleration of the rotation was just a simple mistake I had made in the input-code. :)

Everything works great now, thanks a bunch for all your help. :)

/Crippeli

This topic is closed to new replies.

Advertisement