• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Forward Vector for Object Movement

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

## Recommended Posts

Hi, I''m trying to move an object along a forward vector and I''m obviously missing something because it goes haywire. // inside FrameMove() static float fYaw = 0.0f; static float fPitch = 0.0f; // adjust yaw and pitch with arrow keys // ''w'' moves forward // regenerate vectors // calculate yaw rotation matrix around arbitrary up axis D3DXMatrixRotationAxis( &m_matYaw, &m_vUp, fYaw ); // transform forward and right vectors against yaw matrix D3DXVec3TransformCoord( &m_vForward, &m_vForward, &m_matYaw ); D3DXVec3TransformCoord( &m_vRight, &m_vRight, &m_matYaw ); // calculate pitch matrix around new right vector D3DXMatrixRotationAxis( &m_matPitch, &m_vRight, fPitch ); // again, transform vectors against matrix D3DXVec3TransformCoord( &m_vForward, &m_vForward, &m_matPitch ); D3DXVec3TransformCoord( &m_vUp, &m_vUp, &m_matPitch ); // if user hit ''w'' m_vPosition += m_vForward * m_fElapsedTime; // or backward, subtract and assign... // calculate translation matrix D3DXMatrixTranslation( &m_matTrans, m_vPosition.x, m_vPosition.y, m_vPosition.z ); // concatenate matrices D3DXMATRIX matTransform; D3DXMatrixIdentity( &matTransform ); D3DXMatrixMultiply( &matTransform, &matTransform, &m_matYaw ); D3DXMatrixMultiply( &matTransform, &matTransform, &m_matPitch ); D3DXMatrixMultiply( &matTransform, &matTransform, &m_matTrans ); // set the transform m_pd3dDevice->SetTransform( D3DTS_WORLD, &matTransform ); Thanks for taking the time to read it. Regards, break_r

##### Share on other sites
Whoa, that's some pretty complicated move code. Here's my move code that works really well, so read it and check for differences:

static real dir_angle = 0.0f;vector3 Velocity = SceneMgr->GetDirection() / 2.0f;vector3 FinalVel = vector3( 0.0f, 0.0f, 0.0f );vector3 pos, dir;pos = SceneMgr->GetPosition();dir = SceneMgr->GetDirection();if( want_to_strafe_left ){	vector3 Norm;   D3DXVec3Cross( &Norm, &vector3( 0.0f, 1.0f, 0.0f ), &Velocity );   Norm = -Norm;   FinalVel += Norm;}if( want_to_strafe_right ){	vector3 Norm;   D3DXVec3Cross( &Norm, &vector3( 0.0f, 1.0f, 0.0f ), &Velocity );   FinalVel += Norm;}if( want_to_move_forward ){   FinalVel += Velocity;}if( want_to_move_back ){   FinalVel -= Velocity;}if( MState.X || MState.Y )  // MState contains relative mouse movements since last frame{	matrix4x4 transform;	SceneManager* SceneMgr = SceneManager::getPtr();	real HorizAngle, VertAngle;	HorizAngle = MState.X * (90.0f / 800.0f);	VertAngle  = MState.Y * (90.0f / 600.0f);	{	   matrix4x4 A, B;	   D3DXMatrixRotationY( &A, D3DXToRadian( -dir_angle ) );	   D3DXMatrixRotationX( &B, D3DXToRadian(  VertAngle ) );	   D3DXMatrixMultiply( &transform, &A, &B );	   D3DXMatrixRotationY( &A, D3DXToRadian(  dir_angle + HorizAngle ) );	   D3DXMatrixMultiply( &transform, &transform, &A );	   dir_angle += HorizAngle;	   D3DXVec3TransformCoord( &dir, &dir, &transform );	}   // this line keeps the cursor in the middle of the screen   // rect is GetWindowRect(), client is GetClientRect()   SetCursorPos( rect.left + 3 + (client.right / 2), rect.top + 23 + (client.bottom / 2) );}// do collision checking against all primitives// pos + FinalVel is the new position; pos is the old position// dir also contains updated mouse look informationpos = pos + FinalVel;// 'pos' is final position, 'dir' is final direction (vector3)

Hope this helps! If the mouse look code is unclear, let me know and I'll explain it more.

Chris Pergrossi
My Realm | "Good Morning, Dave"

[edited by - c t o a n on February 9, 2004 1:52:26 AM]

##### Share on other sites
Looks as if your code is always rotating about the x- and y-axes. I have similar code that works around those axes to orient the object and move it around. However, I''m looking for rotation about an arbitrary axis.

##### Share on other sites
D3DXMatrixRotationAxis( D3DXMATRIX* pOut, D3DXVECTOR3* pAxis, float Angle)

##### Share on other sites
Well, what the code does is rotate the object back into a basis aligned with the world basis, which allows you to use the basic X,Y axis rotation functions to transform the direction accordingly.

Chris Pergrossi
My Realm | "Good Morning, Dave"

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015444
×