Mail Archives: djgpp/1997/08/29/21:06:06
I wrote a little prog that used Allegro's fixed point routines to
simulate moving a person in 3d space (well a point really). It does this
by creating the vectors, one facing the player's fowards direction, one
facing the side and one facing up. To move the player, they were merely
transformed along these lines. To turn the player, they get rotated along
the various axes. But something was strange.. when rotating the player's
fowards axis (by turning right say), the player's XYZ got all screwed up.
I later changed the program to floating point math and Allegro's rotation
routines (same code, jsut added '_f_ etc), and it worked fine.. what's
up!? Anyone who can figure out what's going on will be
appreciated..thanks
Compile with gcc vect.c -ovect.exe -lalleg
Use Right key to rotate, '+' key to move fowards
==Begin vect.c==
/* Movement prog.
*
*/
#include <stdio.h>
#include <allegro.h>
typedef struct DIR_VECT
{
fixed x;
fixed y;
fixed z;
} DIR_VECT;
/* This vector always points fowards */
DIR_VECT fv = { 0, 0, (1 << 16) };
/* This vector always points to the side */
DIR_VECT sv = { (1 << 16), 0, 0 };
/* This vector always points up */
DIR_VECT uv = { 0, (1 << 16), 0 };
/* This vector is the person's xyz */
DIR_VECT player = { 0, 0, 0 };
void DispVect(DIR_VECT *v);
void MovePlayer(DIR_VECT *p,DIR_VECT *f);
void RotRigh(void);
int c;
char main(void)
{
allegro_init();
install_keyboard();
do
{
c = readkey();
c = c >> 8;
if( c == KEY_RIGHT )
RotRigh();
else if( c == KEY_PLUS_PAD )
MovePlayer(&player,&fv);
else if( c != KEY_ESC )
c = 0;
if( c )
DispVect(&player);
}while( c != KEY_ESC );
exit(0);
}
/* This rotates the player's Fowards vector to the right. Player XYZ
will not change */
void RotRigh(void)
{
DIR_VECT t; // Temp
MATRIX m = identity_matrix; // Empty
t = fv;
get_rotation_matrix(&m,uv.x,uv.y,uv.z);
apply_matrix(&m,t.x,t.y,t.z, &fv.x,&fv.y,&fv.z);
normalize_vector(&fv.x,&fv.y,&fv.z);
t = sv;
apply_matrix(&m,t.x,t.y,t.z, &sv.x,&sv.y,&sv.z);
normalize_vector(&uv.x,&uv.y,&uv.z);
}
/* Pass the players XYZ and their fowards vector. Move fowards one unit.
*/
void MovePlayer(DIR_VECT *p,DIR_VECT *f)
{
p->x += f->x;
p->y += f->y;
p->z += f->z;
}
void DispVect(DIR_VECT *v)
{
printf("X: %2.2f | Y: %2.2f | Z: %2.2f\n",fixtof(v->x),
fixtof(v->y), fixtof(v->z));
}
=end vect.c=
- Raw text -