Getting a Ray's direction from Camera matrix

Started by
1 comment, last by Zern 21 years, 4 months ago
How would I go about getting the ray's direction based on the Camera matrix? I am trying to cast a ray from the position of the Camera, ( Camera.GetPosition() ) and casting it into the direction the Camera is looking at. My question is how do I extract the *angle* which the Camera is looking at? I thought about just taking the look at vector3 from my Camera class but then I run into the problem where I never change the look at directly, it is always <0,0,1>. ( trying to do basic collision detection with a triangle and seeing if the ray will hit it. ) Here is code of my update() function in my camera class..
    


void CXCamera::Update( void )
{

	
	
     FLOAT fAngularSpeed = 2.0f*fElapsedTime;

    

     // Update the position vector

     D3DXVECTOR3 vT = m_vVelocity;
     D3DXVec3TransformNormal( &vT, &vT, &m_matOrientation );
     m_vPosition += vT;
	



    // Update the yaw-pitch-rotation vector

    m_fYaw   += fAngularSpeed * m_fYawVelocity;
    m_fPitch += fAngularSpeed * m_fPitchVelocity;

	

	
    // Set the view matrix

    D3DXQUATERNION qR;

    D3DXQuaternionRotationYawPitchRoll( &qR, m_fYaw, m_fPitch, 0.0f );

    D3DXMatrixAffineTransformation( &m_matOrientation, 1.25f,   NULL, &qR, &m_vPosition );


     D3DXMatrixInverse( &m_matView, NULL, &m_matOrientation );

     g_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView );


     return;

}     
Edit: blah tags~ [edited by - Zern on December 7, 2002 9:50:35 AM]
Advertisement
Today''s post is sponsored by the term "Orthonormal basis"

The matrix itself contains ("basis") the camera direction vector, even better than that, unless it''s doing any warping etc it''s very likely to normalised ("orthonormal") . Simply:

vCameraLookDirection.x = mView._13;
vCameraLookDirection.y = mView._23;
vCameraLookDirection.z = mView._33;


If you need the angles from that from each major axis, then you could use a bit of trigonometry on 3 the right angled triangles formed between the view vector components and each principle axis (1,0,0 and 0,1,0 and 0,0,1). Keywords "Direction cosines"

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Thank you, Simon.

This topic is closed to new replies.

Advertisement