gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > 3D rotation matrix stuff

#147889 - iainprice - Sat Dec 29, 2007 11:10 pm

I am making a 3d drive around game but am having problems with nice rotations.......

main loop:

[code]
while(1)
{
scanKeys();
u16 keys = keysHeld();
if((keys & KEY_UP)) rotateX += 3;
if((keys & KEY_DOWN)) rotateX -= 3;
if((keys & KEY_LEFT)) rotateZ -= 3;
if((keys & KEY_RIGHT)) rotateZ += 3;
if((keys & KEY_A)) thrustspeed -= 0.01f;
if((keys & KEY_B)) thrustspeed += 0.01f;

//first - player
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
RenderMD2Model(frame,0);
glPopMatrix(1);

//second, third and track
glPushMatrix();
glRotateY(rotateX);
glRotateZ(rotateZ);
glTranslatef(thrustspeed,0.0,0.0);
glMatrixMode(GL_MODELVIEW);

RenderMD2Model(frame,1);

RenderMD2Model(frame,2);

RenderTrack();

glPopMatrix(1);
glFlush(0);
}

[/code]

with the current order of things.... it draws the player, then rotates the track and other players so it appears that the player is rotating. If you move forwards it only goes along the axis, not the direction of the rotated matrix........

I know I could keep the track and players stationary and move the main player and camera but I thought that would be more maths intensive....

any ideas, comments.. suggestions?

cheers.

#147890 - Lick - Sat Dec 29, 2007 11:16 pm

You'll need to keep track of the angle the camera/player is facing, and with that angle, use trigonometry (sinus, cosinus) to calculate the distance of x and y when the player presses forward. (Note: forward doesn't mean x+1 or y+1, but it's often x+0,.. AND y+0,..!)
_________________
http://licklick.wordpress.com

#147892 - iainprice - Sun Dec 30, 2007 12:11 am

So keep a player.x, .y and .z for position and an angle and a distance moved.... then calculate a new x,y,z......

hmmm thought I might have to but wanted to try just using matrix stuff as it is quite fast and accurate.....

#147897 - tepples - Sun Dec 30, 2007 2:13 am

As I understand it, the hardware "matrix stuff" is designed for use with the graphics engine (the "view" in MVC design), not the physics engine (the "model"). Keeping the physics and graphics separate will make it easier for you to maintain the software, especially on other platforms where you will need to rewrite the graphics.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#147908 - sajiimori - Sun Dec 30, 2007 7:21 am

That's some weird code! Here, start with this:
Code:

// A matrix that can be uploaded straight to the hardware.
struct Matrix43
{
   Matrix33 axes;  // rotation
   Vector3 trans;  // position
};

// A 3D object.
struct Object
{
   MD2* model;
   Matrix43 mtx;
};

// Given the orientation of a camera in world space,
// produce a matrix that can be uploaded to the hardware
// to view the world from the camera's perspective.
void calculateViewMtx(const Matrix43* cam, Matrix43* inv)
{
   // Set inv->axes to the transpose of cam->axes,
   // since we want the whole world to appear rotated
   // by the opposite of the camera's rotation.

   // Set inv->trans to the opposite of cam->trans,
   // for similar reasons.

   // Multiply inv->trans by inv->axes, since rotating
   // the camera in place should cause all other objects
   // to rotate around it.  Try seeing what happens if
   // you skip this step!
}

void drawScene(
   const Matrix43* cam,
   const Object* objs,
   int numObjs)
{
   Matrix43 inv;
   calculateViewMtx(cam, &inv);

   for(int i = 0; i < numOjbs; ++i)
   {
      // Load inv as the current matrix.
      // Multiply the current matrix by objs[i].mtx.
      // Draw objs[i].model.
   }
}