Skip to main content
gamedev.net
🔒 Locked

move forward

Started by hello_there 4 replies 200+ views
Original Post
hello_there
hello_there
whats the equation for moving the player forward,backward with respects to the camera and the one for moving left,right. i used to know but i have forgoten.
____________How could hell be worse?
hello_there
hello_there
that doesn''t move forward with respects to the camera. eg say the camera''s at 45 degrees i want it so when the player press forward the player moves in that 45 degree direction.
____________How could hell be worse?
NE1
NE1
You just have to take into account the orientation of the camera and transform your forewards/backwards vector by that matrix.

In my D3D camera class - I do it something like this:


    
void CCamera::MoveForewards( float fDistance )
{
D3DXMATRIX mat;
// Our foreward vector

D3DXVECTOR3 vForwards( 0.0f, 0.0f, 1.0f );
// Get camera rotation/orientation matrix from

// the quaternion (my camera class uses them)

D3DXMatrixRotationQuaternion( &mat, &m_qRotation );
// Here we transpose the matrix in order to

// transform the vForewards vector correctly

D3DXMatrixTranspose( &mat, &mat );
// Now transform the vForewards vector by

// the transposed matrix

D3DXVec3TransformCoord( &vForwards, &vForwards, &mat );
// Just add the this vector to the camera position

// and there we have our new position

m_vPosition += vForwards * fDistance;
// A flag indicating that we need to calculate the

// camera(view) matrix

m_bNeedsUpdate = TRUE;
}


I hope you can understand fairly easily from the above code what I am doing.....

And yes - I am using MoveForewards/MoveBackwards methods even though I can specify a negative number for the fDistance in the MoveForewards function - Its just more intuitive for me to say:
"m_camera.MoveBackwards( 1.0f )"
Instead of:
"m_camera.MoveForewards( -1.0f )"
See what I mean?..... Just my personal whim....

[edited by - NE1 on January 27, 2003 2:41:28 AM]
Trienco
Trienco
in case you do have the matrix for the camera and you mean forward in camera space:

ObjTransform(3,0)=ObjTransform(3,0)+(CamTransform(2,0)*distance);
ObjTransform(3,1)=ObjTransform(3,1)+(CamTransform(2,1)*distance);
ObjTransform(3,2)=ObjTransform(3,2)+(CamTransform(2,2)*distance);

ObjTransform and CamTransform are the world matrices for your object and the camera. if you want to move the cam they''re of course the same. for up/down or left/right replace CamTransform(2,r) with (1,r) or (0,r). be clever and use an enum like enum Axis {X=0, Y, Z} and simply write CamTransform(axis, r);
f@dzhttp://festini.device-zero.de
Ouranos
Ouranos
I think the easiest way to do it is with a look at matrix..

lookat.x = sinf( cameraangle ) + cameraposition.x;
lookat.z = cosf( cameraangle ) + cameraposition.z;

Then to move the camera..

cameraposition.x += sinf( cameraangle ) * velocity;
cameraposition.z += cosf( cameraangle ) * velocity;

That''s to move forward, for backwards movement just subtract. As for strafing, you just take the camera angle in the position equations and add or subtract PI/2 radians. All you have to do now is build a look at matrix based on the these 2 vectors and the up vector. I believe you can use D3DXMatrixLookatLH to do this.

insert witty signature here
-------------------------http://www.roachpuppy.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.