# Flight Sim Equations

This topic is 4315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am developing a flight sim game, and have the following code for moving my plane. The problem is, After a few turns, my plane flies backwards! Is there something obvious I am missing? Vector3 location = new Vector3(5f, 5f, 7f); Vector3 angles = new Vector3(0, 0, 0); float speed = 0; private void UpdatePosition() { Vector3 addvector = new Vector3(); addvector.X += (float)(Math.Sin(angles.Z)); addvector.Y += (float)(Math.Cos(angles.Z)); addvector.Z -= (float)(Math.Tan(angles.X)); addvector.Normalize(); location += speed * (addvector / 20); } I am using a vector to store the heading of the plane, and ahve tied the arrow keys to the vector, for example when the left key is pressed angles.Y += 0.25f. This appears to work ok, but after a number of turns (unsure of hwo many) the plane flies backwards. Any ideas? Thanks, Mike

##### Share on other sites
What do you mean the plane files 'backwards'? Does it stop then reverse direction with the nose still pointed forewards?
Does it suddenly flip 180 midair so it flies tailfirst, but stay along the same path?
This could be an issue either with your physics or just with the graphics model; depending on what you meant by it... describe more.

Also you aren't Really using a vector to store the plane's heading; it is a vector data-type sure, but you are only using it as a struct to hold 3 Euler angles. This is not the actual paradigm of using vector's...

Euler angles generally are Not appropriate for a 3d free moving object (like a flightsim)
I'm guessing the culprit is probably in there - the way you represent plane's heading. But I dont see the code you use for Changing the heading, so I don't know the specific details; only that Euler angles are asking for trouble.

##### Share on other sites
Oh, ive never heard of euler angles sorry. I mean it literally flies backwards, with the nose still pointing forward., this is generally just after ive done a loop the loop, but im unsure of this is the only time. Sorry for the confusion.

My keyboard code is here:

private void ProcessKeyboard()
{
KeyboardState keys = Keyboard.GetState();
if (keys.IsKeyDown(Keys.Right))
{
xwing.angles.Y -= 0.035f;
}
if (keys.IsKeyDown(Keys.Left))
{
xwing.angles.Y += 0.035f;
}
if (keys.IsKeyDown(Keys.Down))
{
xwing.angles.Z += 0.035f * ((float)Math.Sin(xwing.angles.Y));
xwing.angles.X += 0.035f * ((float)Math.Cos(xwing.angles.Y));
}
if (keys.IsKeyDown(Keys.Up))
{
xwing.angles.Z -= 0.035f * ((float) Math.Sin(xwing.angles.Y));
xwing.angles.X -= 0.035f *((float) Math.Cos(xwing.angles.Y));
}
{
xwing.speed += 0.25f;
}
if (keys.IsKeyDown(Keys.Subtract))
{
xwing.speed -= 0.25f;
if (xwing.speed <= 0)
{
xwing.speed = 0;
}
}
}

Ive have been (rouglhy) following the tutorial listed here http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Flight_kinematics.php
as this has been something ive struggled to code on my own. Ive contacted the author and others have also mentioned the problem, but only he says he will post a fix as soon as he finds one.

Do you know a better way of doing it? You seem to be very dismissive of euler angles :-) A link or code would be much appreciated.

Thanks for the help,

Mike

##### Share on other sites
Ok... looked at the tutorial link.
Honestly its not a very good tutorial...
Anyone Here have better Tutorial links?
*end*

Anyway I can visualize whats causing the problem. try to visualize with me, got a toy airplane you can hold and move in real life? it will help:

so you have an angle for heading, and an angle for the plane's absolute pitch (relative to the horizion how high the nose is)..changing the heading angle makes the plane (horizontally) turn left and right, while the pitch makes it tilt up and down.
So hold your plane pointed flat and level, with a heading of 0=due north, pitch=0level
now what happens if you try to loop-de-loop, pull the nose up
the arrow tilts upwards... till it is vertical...but! what is the heading now? when it is perfectly doing a tail-stand? (last time i checked, still 0north, but physically, is the plane really pointed north??)
now loop further, now your plane is now leaning backwards(and upside down), the nose pointed opposite from there orignal direction to face 180south. But whats our heading?
Our heading was last set to 0north, and right now, it still is.
Even though the airplane should be facing a new direction(and upside down btw); since we store our heading as a simple angle it could not account for the loop-de-loop.
the loop is controlled by increasing pitch angle; but physically it should result in changes to heading as well -changes we did not account for
this is why your plane gets Messed Up after a loop...
This is a perfect example of why the Euler Angles representation fails. These angles are being stored separatly, but in real life they affect one another as the plane moves in 3d.

whee... that was a lot of typing...
So now you understand where the problem is... and why I dislike Euler Angles for 3d motion.
as for the solution though, thats complicated. If someone else doesnt post a solution I will later... but not right now, its long.

##### Share on other sites
Right... its been a Very long day.
So I'll give a small push in the hope that one of our other experts can take over.

Right now your 3 angles use the ground(absolute coordinate system) as a reference point when you represent the airplane's orientation.
This is what we call Euler Angles btw, where you try to store an orientation as a series of 3 rotations.

In the prior post we learned that this kind of system is not appropriate for a vehicle that has full 3d freedom of motion; the angle representations can go into ranges that suck for them, and updates get wierd.

So rather than try to change our airplane's orientation using that system, we indroduce the concept of a Local Coordinate System.
Rather than push our angles off a ground compass reference, we do it from a Realative reference -the airplane's prior orientaiton itself.
We call this- performing the rotations in the airplane's local coordinate system.
The trick is representing it!

Now as you know, a coordinate system consists of 3 axis, x y and z. The airplane's local system will be very similar; just image that in addition to the Absolute x,y,z axis on the ground, there is a smaller set of them attached to the airplane itself, that flies around and rotates with it.
Thus your airplane class needs 3 vectors: named X,Y,Z
each of those vectors, contains 3 numbers x,y,z which are its coordinate in Absolute coords. (so you know already that a vector can represent a direction, now we are using 3 vectors to represent 3 directions; and these 3 directions correspond to the X,Y,Z local axis of the airplane, as it spins and loops or whatever) This set of 3 vectors, will need to be arranged of course, to be equal length of 1, as well as perpendicular to each other at all times.

*disclaimer, truly formal approaches to this use a Matrix instead of the 3 separated vectors I describe. I do it this way because its easier to visualize*

So we have our XYZ Local system, and it can be located in terms of the xyz Absolute system. You aready know how to rotate in xyz -thats what you were doing previously. Now the question is, how do we perform rotations within XYZ?
And once we do, how does it translate back to the Absolute xyz system of the world so we can draw it?

*sorry im really tired and didnt totally plan on writing a full tutorial*
I'll update this when I have more energy. And any other expert feel free to continue where I left off. If you are so inclined to not wait for me...

In the meantime, brush up on your Vector operations; you will specifically need: cross product, normalization, dot product/vector projection

##### Share on other sites
Thanks so much for all this help. Much appreciated. Still knew to 3d maths, as never covered it in much detail. Thanks for all the help so far, just carry on when you can please! ;-)

Mike

I've just checked back on the site, and the author has updated the entire chapter to use quaternions. I will try this new code and see if its problem fixed. Thanks for all the help anyway, its been very helpful. Ill post back if i have any problems.

If you're interested the new article address is:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2/Flight_kinematics.php
and a forum topic for comments on it is:
http://www.riemers.net/Forum/index.php?var=459&var2=0
[/edit]

1. 1
Rutin
43
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633398
• Total Posts
3011659
• ### Who's Online (See full list)

There are no registered users currently online

×