#### Archived

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

# Using D3DXMatrixLookAtLH().

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

## Recommended Posts

I have been trying to get to grips with rotating a camera AROUND an object in a scene; it looks like the "LookAt" (eg: D3DXMatrixLookAtLH()) functions could be the way to go but I haven''t been able to locate a simple (or any!)working example. I can rotate my camera up, down, left, right and zoom in and out but I can''t work out how to rotate around a given object in a scene. If someone can point me to a c/c++ example, or post a code example I would be grateful (or show me a better function to do it). Deskman

##### Share on other sites
One (not necessarily the most beautiful) way is to put camera at same point as object, rotate it there and after rotation move it a few units (meters?) backwards.

Very Pseudo Example(tm);

while (!bored_enough_to_reset)
{
camera.position = object.position
camera.3x3rotationmatrix = composematrix(lookatangles)
camera.move (backwards)
render
lookatangles += (2*PI)
}

##### Share on other sites
Lookup \dx7sdk\samples\Multimedia\D3DIM\src\D3DFrame\d3dutil.cpp for the function called ''D3DUtil_SetViewMatrix''

(Source follows)

//-----------------------------------------------------------------------------
// Name: D3DUtil_SetViewMatrix()
// Desc: Given an eye point, a lookat point, and an up vector, this
// function builds a 4x4 view matrix.
//-----------------------------------------------------------------------------
HRESULT D3DUtil_SetViewMatrix( D3DMATRIX& mat, D3DVECTOR& vFrom,
D3DVECTOR& vAt, D3DVECTOR& vWorldUp )
{
// Get the z basis vector, which points straight ahead. This is the
// difference from the eyepoint to the lookat point.
D3DVECTOR vView = vAt - vFrom;

FLOAT fLength = Magnitude( vView );
if( fLength < 1e-6f )
return E_INVALIDARG;

// Normalize the z basis vector
vView /= fLength;

// Get the dot product, and calculate the projection of the z basis
// vector onto the up vector. The projection is the y basis vector.
FLOAT fDotProduct = DotProduct( vWorldUp, vView );

D3DVECTOR vUp = vWorldUp - fDotProduct * vView;

// If this vector has near-zero length because the input specified a
// bogus up vector, let''s try a default up vector
if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
{
vUp = D3DVECTOR( 0.0f, 1.0f, 0.0f ) - vView.y * vView;

// If we still have near-zero length, resort to a different axis.
if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
{
vUp = D3DVECTOR( 0.0f, 0.0f, 1.0f ) - vView.z * vView;

if( 1e-6f > ( fLength = Magnitude( vUp ) ) )
return E_INVALIDARG;
}
}

// Normalize the y basis vector
vUp /= fLength;

// The x basis vector is found simply with the cross product of the y
// and z basis vectors
D3DVECTOR vRight = CrossProduct( vUp, vView );

// Start building the matrix. The first three rows contains the basis
// vectors used to rotate the view to point at the lookat point
D3DUtil_SetIdentityMatrix( mat );
mat._11 = vRight.x; mat._12 = vUp.x; mat._13 = vView.x;
mat._21 = vRight.y; mat._22 = vUp.y; mat._23 = vView.y;
mat._31 = vRight.z; mat._32 = vUp.z; mat._33 = vView.z;

// Do the translation values (rotations are still about the eyepoint)
mat._41 = - DotProduct( vFrom, vRight );
mat._42 = - DotProduct( vFrom, vUp );
mat._43 = - DotProduct( vFrom, vView );

return S_OK;
}

//-----------------------------------------------------------------------------

##### Share on other sites
Hey,

Thanks for the help guys!

I'll try them out asap. If anyone has a version using D3DXMatrixLookAtLH, I'd still love to hear from you!

Deskman

Edited by - Deskman on October 4, 2000 12:50:43 AM

##### Share on other sites
apparently D3DXMatrixLookAtLH has a bug, so stick to SetViewMatrix until DX8

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002332
×