delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/03/19:59:34

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Help! Allegro 3D maths problem
Date: Thu, 3 Jul 1997 22:00:05 +0100
Organization: None
Distribution: world
Message-ID: <fEnEVSAVLBvzEw+W@talula.demon.co.uk>
References: <33BB8F5C DOT 1CD3177B AT wayfarer DOT demon DOT co DOT uk>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 40
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dan writes:
>What I'm trying to achieve is rotation around the *camera's* own local
>axes (i.e. front, up and right vectors) no matter where the camera is,
>or at what rotations, kind of like in Descent.

This really isn't an appropriate newsgroup for 3d math questions: this
doesn't have a lot to do with djgpp!

But anyway. It's really quite easy: just store your camera orientation
as a couple of unit vectors (one pointing in front of it, and one
pointing up in the air), and do all your rotations around those (using
Allegro functions, get_vector_rotation_matrix() to build a matrix that
will rotate around a specific vector, and apply_matrix() to actually do
the rotation).

To turn left and right, rotate the front vector around the up vector. To
roll from side to side, rotate the up vector around the front one. To
look up and down, rotate both up and front around a sideways vector
(left or right doesn't matter, but will obviously make you turn in
different directions). Before you throw up your hands in despair saying
"but I don't know the sideways vector", you can calculate it whenever
you need: it's just the cross product of up and infront.

If the up and front vectors start at right angles to each other, this
system will work for any orientation you like, except that after a few
million iterations (or a few thousand if you are using fixed point
maths), rounding errors are likely to result in the two vectors no
longer being quite perpendicular. It's a good idea to adjust them ever
now and again, which is done first by renormalizing them to have a
length of one, and then by generating a sideways vector (cross product
of up and infront) and then using that to calculate a new up vector
(cross product of front and sideways). You'll need to experiment with
the order of the parameters to the cross product routine to make sure
you get the new up pointing in the right direction: if they are the
wrong way round it will point down instead :-)


--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019