Jump to content
  • Advertisement
Sign in to follow this  
rbx775

simple Translation Question (noob)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there.
Im basically a directX noob, and currently learning from pretty cool internet tutorials.
So i read into moving objects and figured out how to rotate and translate objects in World-axis, but how do i translate objects in Objectspace ?

or to be more specific;
How do i translate my Camera relative to its own Z-axis ?

i want to simulate a plane flying forward, but all i get is a "plane" flying towards the z-World-axis.
Heres my function which handles all translations:

void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

static float forward = 0.0f; forward += 0.01;
static float pitch = 0.0f;
static float roll = 0.0f;

D3DXMATRIX matView;
D3DXMATRIX matMove;
D3DXMATRIX matRotateZ;
D3DXMATRIX matRotateX;

D3DXMatrixTranslation(&matView, 0.0f, 0.0f, forward);

D3DXMatrixRotationZ(&matRotateZ, roll);
D3DXMatrixRotationX(&matRotateX, pitch);


if((!KEY_DOWN(VK_DOWN))&&(KEY_DOWN(VK_UP))) {
pitch-=0.01f;
}

if((KEY_DOWN(VK_DOWN))&&(!KEY_DOWN(VK_UP))) {
pitch+=0.01f;
}

if((KEY_DOWN(VK_RIGHT))&&(!KEY_DOWN(VK_LEFT))) {
roll+=0.01f;
}

if((!KEY_DOWN(VK_RIGHT))&&(KEY_DOWN(VK_LEFT))) {
roll-=0.01f;
}

D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, forward),
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f),
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));

d3ddev->SetTransform(D3DTS_VIEW, &(matView*matRotateZ*matRotateX));

D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45),
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
1.0f,
100.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

meshTeapot->DrawSubset(0);

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);
}

Can you help me solving my problem ?
Any Idea or "pointing into the right direction" is highly appreciated ! (as i dont even know how to search without the specific terms...) smile.png

additionaly: Do you have any other advises for handling Input ? (mine sure isnt the best.)
(or even other no goes you detect in the above ?)

thank you

Share this post


Link to post
Share on other sites
Advertisement
you can have a vector camera. you will have two vectors for your camera, forward and right, representing the cameras +z axis (forward) and +x axis (right). you can use D3DXMatrixRotationYawPitchRoll() to get your rotation matrix with the roll and pitch values (they need to be declared globally so they stay the same value for each frame). you then use D3DXVec3TransformNormal() to update your cameras forward vector, using the world's +z axis (world's forward (0,0,1)) and the rotation matrix. then do the same for the cameras right vector (+x axis for the world, or (1,0,0)). Then just add the vectors to your cameras position. Then you cross the cameras forward and right vectors to get the camera's up vector. So something like this:

void UpdateCamera()
{
D3DXMatrixRotationYawPitchRoll( &rotationMatrix, yaw, pitch, 0 );
D3DXVec3TransformCoord( &Target, &DefaultForward, &rotationMatrix );
D3DXVec3Normalize( &Target, &Target );

// Free-Look Camera
D3DXVec3TransformNormal(&Right, &DefaultRight, &rotationMatrix);
D3DXVec3TransformNormal(&Forward, &DefaultForward, &rotationMatrix);
D3DXVec3Cross(&Up, &Forward, &Right);

Position += moveLeftRight*Right;
Position += moveBackForward*Forward;

moveLeftRight = 0.0f;
moveBackForward = 0.0f;

Target = Position + Target;

D3DXMatrixLookAtLH( &View, &Position, &Target, &Up );
}


This function will give you a free look camera, but you should be able to turn it into your plane camera

You'll actually probably not want to make the pitch and roll global since you will be updating with the camera's forward and right vectors. so instead of using the world forward and right vectors to update the camera's forward and right vectors every frame, you will just use the camera's forward and right vectors to update themselves when calling D3DXVec3TransformNormal()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!