ZGameEditor forum Forum Index ZGameEditor forum
The tool for creating 64kb games. ZGE Wiki homepage.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

3d rotations for a space sim game

 
Post new topic   Reply to topic    ZGameEditor forum Forum Index -> Your projects
View previous topic :: View next topic  
Author Message
panurge



Joined: 04 Jul 2009
Posts: 13

PostPosted: Thu Jul 15, 2010 1:05 pm    Post subject: 3d rotations for a space sim game Reply with quote

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!



ROTATION.zgeproj
 Description:

Download
 Filename:  ROTATION.zgeproj
 Filesize:  9.56 KB
 Downloaded:  17 Time(s)

Back to top
View user's profile Send private message
Kjell



Joined: 23 Feb 2008
Posts: 854

PostPosted: Thu Jul 15, 2010 1:19 pm    Post subject: Reply with quote

Cool

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

K
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    ZGameEditor forum Forum Index -> Your projects All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group