Jump to content


camera movements


  • You cannot reply to this topic
3 replies to this topic

#1 Pushapjit

    Member

  • Members
  • PipPip
  • 34 posts

Posted 16 March 2006 - 09:18 AM

I am making 3d virtual model of a city. I’m using directx9 APIs for this.
Following UpdateCamera() function is used for camera interactivicty. I want my camera to move along the ground. My problem is that the camera moves at the same level irrespective of the ground.
//------------------------------------------------------------------------
//Name: UpdateCamera()
//Desc: This function takes input from the user and update the scene according to user input.
//------------------------------------------------------------------------
VOID CMyD3DApplication::UpdateCamera()
{
FLOAT fElapsedTime;

if( m_fElapsedTime > 0.0f )
fElapsedTime = m_fElapsedTime;
else
fElapsedTime = 0.05f;

FLOAT fSpeed = 15.0f*fElapsedTime;
FLOAT fAngularSpeed = 1.0f*fElapsedTime;

// De-accelerate the camera movement (for smooth motion)
m_vVelocity *= 0.9f;
m_fYawVelocity *= 0.9f;
// m_fPitchVelocity *= 0.9f;

// Process keyboard input
if( m_bKey[VK_RIGHT] ) m_vVelocity.x += fSpeed; // Slide Right
if( m_bKey[VK_LEFT] ) m_vVelocity.x -= fSpeed; // Slide Left
if( m_bKey[VK_UP] ) m_vVelocity.z += fSpeed; // Move Forward
if( m_bKey[VK_DOWN] ) m_vVelocity.z -= fSpeed; // Move Backward
if( m_bKey['E'] ) m_fYawVelocity += fSpeed; // Turn Right
if( m_bKey['Q'] ) m_fYawVelocity -= fSpeed; // Turn Left
// if( m_bKey['Z'] ) m_fPitchVelocity += fSpeed; // Turn Down
// if( m_bKey['A'] ) m_fPitchVelocity -= fSpeed; // Turn Up



// Update the position vector
D3DXVECTOR3 vT = m_vVelocity * fSpeed;
D3DXVec3TransformNormal( &vT, &vT, &m_matOrientation );
m_vPosition += vT;
if( m_vPosition.y < 1.0f )
m_vPosition.y = 1.0f;

// Update the yaw-pitch-rotation vector
m_fYaw += fAngularSpeed * m_fYawVelocity;


// Set the view matrix
D3DXQUATERNION qR;
D3DXQuaternionRotationYawPitchRoll( &qR, m_fYaw, NULL, 0.0f );
// D3DXMatrixAffineTransformation( &m_matView, 1.25f, NULL, &qR, &m_vPosition );
D3DXMatrixAffineTransformation( &m_matOrientation, 1.25f, NULL, &qR, &m_vPosition );
D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );

m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );


}

#2 bignobody

    Valued Member

  • Members
  • PipPipPip
  • 155 posts

Posted 16 March 2006 - 02:46 PM

Pushapjit said:

My problem is that the camera moves at the same level irrespective of the ground.

That's because you are continually setting your camera's Y position to 1.0f.


   if( m_vPosition.y < 1.0f )

        m_vPosition.y = 1.0f;



If you want to have your camera follow the terrain, you need to find the height of the terrain at the point below your camera and then adjust the camera's Y position accordingly. How you do this depends on how you store your terrain heightmap, but it shouldn't be too difficult.

Regards,
-bignobody
notsoftgames.com - Creator of Shlongg!

#3 Pushapjit

    Member

  • Members
  • PipPip
  • 34 posts

Posted 17 March 2006 - 08:38 AM

how i should find the height of terrian.

m_vPosition.y = m_vPosition.y + terrian.height;

#4 bignobody

    Valued Member

  • Members
  • PipPipPip
  • 155 posts

Posted 17 March 2006 - 02:27 PM

That is the right idea, but again it depends on how you store your terrain heightmap data. If it's just a simple 2D array, calculate what x and y position your camera is over (based on it's current position) and then use that value as your y offset.

Once you get that working, you can get a little fancier by finding the current terrain triangle your camera is over and interpolating between those 3 points, which should yield smoother movement.

Regards,
-bignobody
notsoftgames.com - Creator of Shlongg!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users