[SOLUTIONS] PITCH YAW ROLL
2006 October 20Scroll down for the English version.
Vor kurzem habe ich angefangen, mir eine neue Science Fiction Serie anzusehen (Battlestar Galactica). Bei den Kampfszenen ist mir endlich aufgefallen, was mich an allen Weltraumsimulationen, die ich bisher gespielt habe (X, X2, Star Trek Starfleet Academy, Wing Commander, WC Privateer, WC:Privateer Remake), gestoert hat: Die Steuerung!
Die Steuerung in all diesen Spielen erinnert mehr an ein Auto: Gas geben, lenken und die Bewegungsrichtung hat sich geaendert. Dass das 'falsch' ist, habe ich in einer Szene gemerkt, als Starbuck (Charakter as Battlestar Galactica) vor einem Gegner davongeflogen ist, den Antrieb abgeschalten hat sich dann umgedreht hat. Jetzt ist Starbuck 'rueckwaerts' weitergeflogen, in die Richtung, in die sie vorher beschleunigt hat (Massentraegheit).
Das ist die Steuerung, wie ich sie mir fuer Weltraumspiele wuensche. Also habe ich angefangen, das Internet nach Informationen ueber diese Art der Bewegung abzusuchen - idealerweise mit einer einfachen Implementation zur Veranschaulichung.
Leider fand ich absolut gar nicht dazu. Sicher, viel Mathematik, etliche Hinweise auf Quaternionen und Matrizen. Aber keine Beispielimplementation.
Alles muss man selber machen...
Den ersten Tag habe ich damit verbracht, mit Euler Winkeln herumzuspielen und in saemtliche Probleme, die damit auftreten, zu rennen.
Am zweiten Tag versuchte ich es mit Quaternionen, bin damit damit ueberhaupt nicht vorwaerts gekommen. Also habe ich dies nach etwa einem halben Tag wieder verworfen.
Verschiedene Unterhaltungen im IRC Kanal #gametome auf freenode brachten auch nicht das gewuenschte Ergebnis und auf Google nach 'Pitch Yaw Roll' zu suchen ist aussichtslos.
Irgendwo unterwegs habe ich den Hinweis aufgeschnappt, es mit Vektoren zu machen: Ein Vektor fuer die Vorwaertsachse des Schiffes, ein Vektor fuer die 'Oben'-Achse des Schiffes und ein Vektor fuer die 'Seitwaerts'-Achse des Schiffes. Damit liesse sich Beschleunigungsrichtung, Ausrichtung und Rotation des Schiffes errechnen und umsetzen.
Auf der Suche nach 'rotating a vector' bin ich auf diese Seite gestossen. Der fuer mich wichtige Teil der Seite ist dieser Auszug:
q' := p*sin(x)+q*cos(x)
p und q sind dabei zwei Achsen des Schiffes. Die dritte Achse ist hier nicht mit dabei, da sich bei einer Rotation immer nur zwei Achsen veraendern. Beispiel:
Wenn ich mich nach rechts drehe, aendert sich nichts daran, wo 'oben' ist.
Mit dieser Formal kann ich also nun die Rotation meines Schiffes aendern, allerdings dauerte es noch etwa einen weiteren halben Tag, bis ich eine funktionierende Implementation desselben geschaffen hatte.
Eine Seite, die sich selst Vector Tutorial nennt (wo ist da bitte ein Tutorial?), half mir bei den letzten Schritten, der Berechnung der dritten Achse an Hand von zwei gegebenen Achsen.
Hier ist eine seeeehr einfache Beispiel Implementation in C. Mit den Tasten i und k (runter, rauf), j und l (links, rechts) sowie o und u (im Uhrzeigersinn drehen, gegen den Uhrzeigersinn drehen) kann man die Vektoren veraendern. 'Forward' ist dabei die Richtung, in die man schaut, 'Up' ist 'oben' und 'Right' ist rechts.
Zur Veranschaulichung habe ich auch eine 'richtige' Raumschiffsimulation entwickelt. Die Tasten sind die selben. Dazu kommen noch 'a' und 'y' zum Beschleunigen und Bremsen (oder eher: Schub verstaerken und Schuck verringern), '1' um den Antrieb zu deaktivieren und '2' fuer einen Reset. Escape beendet das Programm. Zum Kompilieren wird OpenGL, eine Implementation von GLUT sowie libGLI benoetigt.
Viel Spass!
EOF
--
A short while ago I started watching a new Science Fiction Series (Battlestar Galactica). During the dogfight scenes I finally realised what I think was 'wrong' with all the space simulations I played so far (X, X2, Star Trek Starfleet Academy, Wing Commander, WC Privateer, WC:Privateer Remake): Spacecraft movement!
In all these games movement reminded me of a car than a spacecraft: Accelerate, steer and the direction in which you're going changes. I realized that this is 'wrong' in a scene where Starbuck (a character from Battlestar Galactica) raced from an attacker, then cut the engines and turned around, now drifting backwards, in the direction she accelerated toward earlier (mass inertia).
That is spacecraft control how i wish it were in space games. So I started searching the Internet for information about this kind of movement - ideally with an example implementation for demonstration purposes.
Unfortunately, I found absolutely nothing like that. Sure, lots of math, tons of pointers to use quaternions and matrices. But no example implementation.
Really, if you want something done...
The first day I wasted by fiddling with Euler Angles and all of the problems they bring.
The second day I tried wrapping my head around the usage of quaternions, with no luck. After half a day I gave up.
Various discussions in the IRC channel #gametome on the freenode network didn't give the desired result either and searching Googl for 'Pitch Yaw Roll' is totally pointless.
Somewhere along the way I got hinted at trying to use vectors: one for the forward direction, one for the up direction and one for the sideways direction. With that, the attitude of the spaceship could be correctly identified.
Searching for 'rotating a vector' I found this site. The for me important part of the site is this:
q' := p*sin(x)+q*cos(x)
p and q are two axes of the ship. The third axis isn't listed here, because during a rotation only two out of three axes change. Example:
Stand in a room and turn around. Your forward axis has changed, as has your sideways axis. But the ceiling is still above you (your up vector).
With this formula I could now rotate my ship freely. But it still took around half a day to get this into a working implementation.
A page calling itself a Vector Tutorial (wherever there may be a tutorial) helped me with the final steps, calculating the third axis from the other two axes.
Here is a very simple implementation of Pitch Yaw Roll in C. Use the keys i and k for down/up movement, j and l for left right, and o and u for clockwise and counter-clockwise rotation. 'Forward' vector is the direction you're looking into, 'up' is where the ships ceiling is and 'right' is... well... right.
For visual understanding I also created a 'real' Space Simulation. The keys are the same with additionally 'a' and 'y' for increasing/decreasing thrust, '1' to cut the engine (thrust=0) and '2' for reset. Press Escape to quit. To compile the program you need OpenGL, an implementation of GLUT and libGLI.
Have a lot of fun!
EOF
Category: blog
Tags: Solutions Weltraumsimulationen Quaternionen Matrizen Vektor space simulations