DX9 - Spaceship type game - HELP!!!!

This topic is 4967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Im making a game where you fly a space ship. Ther eis a skybox and the camera sits behind the spaceship. What im trying to do is when you rotate the spaceship down, the camera will constantly sit behind the spaceship. I have three vectors for the spaceship, Up, Right and Foreward. When I rotate the spaceship, I use the D3DXMatrixRotateAxis() function to roate around the vectors. My problems is that I need to adjest the other two vectors when i rotate around one of the axis and I need to the camera to basically do the same thing but sit behind the spaceship. The final effect would be that the spaceship does not appear to move, but its actually rotating. [Edited by - Mastadex on February 12, 2005 4:47:23 PM]

Share on other sites
I don't know exactly how you want to do this, but I had a similar problem once and found the best solution to be leaving the moving object (ie spaceship) stationary, then your 'rotate' function just rotates the world matrix around the ship. Again this might not be for you but it is always worth thinking about which approach is superior for your problem. Doing this can make calculations involving the ship very simple instead of always requiring a transform into world space.

-Jay

Share on other sites
Maybe you are talking about having a camera following the spaceship (behind it), but when the spaceship rotates to left, for example, you can see its lateral (showing that it is really a 3D object). So, after the rotation the camera returns behind the spaceship.
Well, if were that, you could implement a delay between the spaceship's movement and camera's movement.

Share on other sites
Thats an Idea, and I miht implement the delay thing. Although right now, im trying to comprehend what functions to use to get the Vectors turned the way I want it along with the space ship.

D3DxMatrixRotateAxis is a big part of this, where I have three vectors for an object, and i rotate around those. but those vectors are supposed to move and rotate. its a big pain in the butt right now.

Share on other sites
Quote:
 My problems is that I need to adjest the other two vectors when i rotate around one of the axis and I need to the camera to basically do the same thing but sit behind the spaceship.

I think I understood...

You have three vectors for the spaceship: Up, Right and Foreward.
When you use the D3DXMatrixRotateAxis function to rotate around one vector, just apply the same transformation to the other two vectors. The same thought is valid for the camera vectors.

For example:

Initially you have (global variables):
/////////////////////////////////////////////

// vectors for the spaceship
D3DXVECTOR3 Up (0.0f, 1.0f, 0.0f);
D3DXVECTOR3 Right (1.0f, 0.0f, 0.0f);
D3DXVECTOR3 Foreward (0.0f, 0.0f, 1.0f);

// vector from the spaceship to the camera
D3DXVECTOR3 ShipCam (0.0f, 0.0f, -50.0f);

//////////////////////////////////////////////

// Suppose that you want to rotate around Up:
D3DXMATRIXA16 matR;
D3DXMatrixRotateAxis( &matR, &Up, angle );

// Use matR matrix to transform the other vectors:
D3DXVec3TransformCoord( &Right , &Right, &matR );
D3DXVec3TransformCoord( &Foreward, &Foreward, &matR );
D3DXVec3TransformCoord( &ShipCam, &ShipCam, &matR );

// spaceship position
D3DXVECTOR3 shipPos ( px, py, pz ); // input the translation values

// camera position
D3DXVECTOR3 camPos = shipPos + ShipCam;

Apply that same transformation (with the matR matrix) to each axis from the camera and specify them directly in the view matrix (see its layout in DX SDK - search for D3DXMatrixLookAtLH). Of course, there are other solutions.

Share on other sites
Oh wow.

Thanks alot this sure makes my life a heck of alot easier.

Share on other sites
Im still having trouble trying to get this to work. Is there a way I can do all three axis at the same time?

Here is what I have so far...It Kinda Works....

void RotateVector(float X, float Y, float Z)
{
// Something we need
D3DXMATRIX matRotX;
D3DXMATRIX matRotY;
D3DXMATRIX matRotZ;

// Clear the Rotation Matricies
D3DXMatrixRotationYawPitchRoll(&matRotX, 0, 0, 0);
D3DXMatrixRotationYawPitchRoll(&matRotY, 0, 0, 0);
D3DXMatrixRotationYawPitchRoll(&matRotZ, 0, 0, 0);

// Assign Changed Values
vX = X;
vY = Y;
vZ = Z;

// Rotate Around the Vectors
D3DXMatrixRotationAxis(&matRotX, &Left, vX);
D3DXMatrixRotationAxis(&matRotY, &Up, vY);
D3DXMatrixRotationAxis(&matRotZ, &Foreward, vZ);

// Change in X
D3DXVec3TransformCoord(&Foreward, &Foreward, &matRotX);
D3DXVec3TransformCoord(&Up, &Up, &matRotX);

// Change in Y
D3DXVec3TransformCoord(&Foreward, &Foreward, &matRotY);
D3DXVec3TransformCoord(&Left, &Left, &matRotY);

// Change in Z
D3DXVec3TransformCoord(&Left, &Left, &matRotZ);
D3DXVec3TransformCoord(&Up, &Up, &matRotZ);

// Normalize
D3DXVec3Normalize(&Left, &Left);
D3DXVec3Normalize(&Up, &Up);
D3DXVec3Normalize(&Foreward, &Foreward);

matRotation = matRotX * matRotY * matRotZ;
}

[Edited by - Mastadex on February 12, 2005 2:52:22 PM]

Share on other sites
Quote:
 Im still having trouble trying to get this to work. Is there a way I can do all three axis at the same time?

... problems with the first solution?! [smile]

Don't worry, I programmed a code for you:

Global variables:
// Position for the spaceshipD3DXVECTOR3 ObjectPosition (0.0f, 0.0f, 0.0f);   // initial position// Axes for the spaceshipD3DXVECTOR3 objXaxis (1.0f, 0.0f, 0.0f);D3DXVECTOR3 objYaxis (0.0f, 1.0f, 0.0f);D3DXVECTOR3 objZaxis (0.0f, 0.0f, 1.0f);// Matrix to transform the spaceshipD3DXMATRIXA16 matObject;

When you initialize the stuffs (D3D, meshes etc), also initialize the matObject matrix by using:

D3DXMatrixIdentity( &matObject );

Finally, in the rendering function:
   // For clearing the explanation, the code was not optmized   D3DXMATRIXA16 matWorld;   D3DXMatrixIdentity( &matWorld );   g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );   // Render static meshes here (meshes from the scenes)   Terrain->RenderMesh();   // this is just an example   // Angles   FLOAT rotX = 0.0f;   FLOAT rotY = 0.0f;   FLOAT rotZ = 0.0f;   // Get data from the keyboard ///////////////////////////////////////////////   // Rotating -----------------------------------------------------------------   if ( Left == true )      rotY = 0.01f;       // you will define these values (0.01f and -0.01f)   if ( Right == true )      rotY = -0.01f;   if ( Up == true )      rotX = 0.01f;   if ( Down == true )      rotX = -0.01f;   if ( SpinZ_Left == true )      rotZ = 0.01f;   if ( SpinZ_Right == true )      rotZ = -0.01f;   // Translating --------------------------------------------------------------   if ( Go == true )      ObjectPosition += objZaxis * 5.0f;     // you will define these values (5.0f)   if ( Back == true )      ObjectPosition -= objZaxis * 5.0f;   if ( Slide_Right == true )      ObjectPosition += objXaxis * 5.0f;   if ( Slide_Left == true )      ObjectPosition -= objXaxis * 5.0f;   if ( Slide_Up == true )      ObjectPosition += objYaxis * 5.0f;   if ( Slide_Down == true )      ObjectPosition -= objYaxis * 5.0f;   /////////////////////////////////////////////////////////////////////////////   D3DXMATRIXA16 matX, matY, matZ, matRes;   D3DXMatrixRotationAxis( &matX, &objXaxis, rotX );   D3DXMatrixRotationAxis( &matY, &objYaxis, rotY );   D3DXMatrixRotationAxis( &matZ, &objZaxis, rotZ );   matRes = matX * matY * matZ;      // Rotating the spaceship's axes   D3DXVec3TransformCoord( &objXaxis, &objXaxis, &matRes );   D3DXVec3TransformCoord( &objYaxis, &objYaxis, &matRes );   D3DXVec3TransformCoord( &objZaxis, &objZaxis, &matRes );   // Important: Update the matObject matriz   matObject =  matObject * matRes;	   D3DXMATRIXA16 matTransObj, matTransRes;   D3DXMatrixTranslation( &matTransObj, ObjectPosition.x, ObjectPosition.y, ObjectPosition.z );   matTransRes = matObject * matTransObj;   g_pd3dDevice->SetTransform( D3DTS_WORLD, &matTransRes );   // Render the spaceship here   SpaceShip->RenderMesh();   // this is just an example   // Now we define the camera's vectors based on the object's axes   D3DXVECTOR3 vLookatPt = ObjectPosition;   D3DXVECTOR3 vEyePt    = vLookatPt - objZaxis * 1800.0f;   // define the distance between the camera and the spaceship (1800.0f)   D3DXVECTOR3 vUpVec    = objYaxis;   D3DXMATRIX matView;   D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );   g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );   D3DXMATRIXA16 matProj;   D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 10000.0f );   g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

Good Luck! [smile]

Share on other sites
Hmm,

I coded it exactly as you told me to but im getting REALLY weird results with rotation. I have no implemented translation yet.

i hardcoded the location of the space ship and my camera, just to test out the rotation of the space ship. this is _REALLY_ confusing me right now....

1. 1
2. 2
3. 3
Rutin
21
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 12
• Forum Statistics

• Total Topics
632568
• Total Posts
3007119

×