3d rotations for a space sim game

Post screenshots, binaries and projectfiles of the projects you have made with ZGE that you want to share!

Moderator: Moderators

Post Reply
User avatar
panurge
Posts: 22
Joined: Sat Jul 04, 2009 11:08 pm

3d rotations for a space sim game

Post by panurge »

Maybe someone of you will remeber Lucasart's Tie Fighter, one of the greatest 3d space simulation games ever (something similar to the old Elite series). I was in love with this game, and i always dreamed to make something similar.

So, i wonder if someone here ever had the following problem, that naturally arises when dealing with tridimensional rotations:

Let TO, UP and LEFT be three unitary vectors, mutually orthogonal. TO is the direction we are facing. UP and LEFT are the direction above us and at our left. Let's assume that, initially, TO=(1,0,0), LEFT=(0,1,0) and UP=(0,0,1), so they are initially oriented as the fixed X, Y, Z axis.

We wish to calculate an arbitrary rotation around these axis. For exemple, if we want to YAW, then the axis of rotation is UP. In order to PITCH, we rotate around LEFT, and to ROLL we rotate around TO.

It is not sufficient, naturally, to use the rotations about the fixed axis, because in the general case this leads to horrid shitness. Anyway, the following matrices can come in handy:


1 0 0
0 c -s <--- rotation around X
0 s c

c 0 s
0 1 0 <--- rotation around Y
-s 0 c

c -s 0
s c 0 <--- rotation around Z
0 0 1

Where c=cos(phi) and s=sin(phi), and phi is the angle of rotation.

What if the rotation axis is not X, Y, or Z?

Let n=(nx,ny,nz) be the axis of rotation. If v=(vx,vy,vz) is an arbitrary vector, we wanna find a matrix A such that v*A=v', where v' is the vector resulting from the rotation around n.

Solution: define c=cos(phi) and s=sin(phi), and let Id be the identity matrix. Consider the matrix UH defined as follows

0 -nz ny
nz 0 -nx <---UH
-ny nx 0

and define

exp(UH)=Id + s*UH + (1-c)*(UH^2).

This expression is equal to the 'natural' definition of exponentiation (infinite sum of (matricial) powers divided by factorials etc etc), but only uses the first two powers of UH.

then v*exp(UH)=v'.

Believe it or not, it works perfectly. So now we can manage three vectors and perform any rotation around them!

The main problem here is putting the camera at the center of the triad and make it rotate properly... This seems not easy.

A solution may be to keep the camera fixed and make everything else rotate around the origin when needed. The above formulae will then be used when calculating each object (enemy ships, etc) orientations.

Anyway, graphical rotations of objects will still need to be calculated, so we can't simply avoid the problem of converting all this stuff into Something.Rotation.X, Y and Z (unless we decide to construct every model vertex by vertex, then applying our rotation matrix to each one!)

(right now i'm thinking about a method to avoid ruining oneself's life with these issues... i'll come back later if i see that my idea works)

Hope someone will find this stuff useful.

PS. The reference book i used is "Rotations, Quaternions, and Double Groups" by Simon L. Altmann.

Anyway...

Here's my current project. (directional buttons control YAW and PITCH. Press A and then left or right in order to ROLL). Please forgive the not-so-elegant code!
Attachments
ROTATION.zgeproj
(9.56 KiB) Downloaded 740 times
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Post by Kjell »

8)

Nice! Smart & simple solution + good to have somebody on the ZGE forums that actually enjoys reading mathematical literature :wink:

K
Post Reply