Camera Rotation

Started by
16 comments, last by dagamer34 20 years, 7 months ago
Sample code.... there is no foun in programming if you do not give it a personal shot...
Advertisement
There is no fun to begin with if you have a deadline
I gave it a shot. It didn''t work. Here is the code i came up with.

/* m_vView and m_vPosition are D3DXVECTOR3''s by the way */

/* Move to the origin so we can rotate along the axises
without problems */
D3DXMATRIX translate;
D3DXVECTOR3 vtranslate = m_vView - m_vPosition;
D3DXMatrixTranslation (&translate, vtranslate.x, vtranslate.y,
vtranslate.z);

/* Find the angle between (0, 0, 0) and our current position */
float angle = atan2 (m_vPosition.x, m_vPosition.z);

/* Rotate along the y-axis */
D3DXMATRIX matrixY;
D3DXMatrixRotationY (&matrixY, angle);

/* Rotate along the z-axis */
D3DXMATRIX matrixZ;
D3DXMatrixRotationX (&matrixZ, D3DXToRadian (fSpeed));

/* Move back */
D3DXMATRIX translate2 = -translate;
D3DXVECTOR3 vtranslate2 = -vtranslate;
D3DXMatrixTranslation (&translate2, vtranslate2.x, vtranslate2.y,
vtranslate2.z);

/* Now we build the matrix */
D3DXMatrixLookAtLH (&m_matView, &m_vPosition, &m_vView,
&m_vUpVector);

//m_matView = m_matView * translate * matrixY * matrixZ * translate2;

}
I am working on a 3rd person game myself... Here''s some sample
code of what i do to determine the camera angle:

m_pPlayer is the player object

m_pPlayer.vLoc is a vector which contains the player''s current (x,y,z) location

m_fDistanceFromPlayer is the distance between the camera and player

m_vCameraLoc is a D3DXVECTOR3 containing the camera position

// Min and max camera distance from the player
FLOAT fMinDistance = 10.0f;
FLOAT fMaxDistance = 100.0f;

// Zoom camera in
if( m_UserInput.bCameraZoomIn && m_fDistanceFromPlayer > fMinDistance )
m_fDistanceFromPlayer -= 1.0f;

// Zoom camera out
if( m_UserInput.bCameraZoomOut && m_fDistanceFromPlayer < fMaxDistance )
m_fDistanceFromPlayer += 1.0f;

// Calculate the camera angle (180 degrees or opposide of whatever the player''s y-axis rotation is)
const FLOAT fAngle = m_pPlayer.fAngle + DEGTORAD(TWOPI);

// Calculate the camera''s x-axis position
m_vCameraLoc.x = m_pPlayer.vLoc.x + m_fDistanceFromPlayer * sinf( fAngle );

// Calculate the camera''s z-axis position
m_vCameraLoc.z = m_pPlayer.vLoc.z + m_fDistanceFromPlayer * cosf( fAngle );

// The y-axis is always the same for my game
m_vCameraLoc.y = 5.0f;

// Set the eye point
D3DXVECTOR3 vEyePt = D3DXVECTOR3( m_vCameraLoc.x, m_vCameraLoc.y, m_vCameraLoc.z );

// Set the look at point (i have it looking at the player)
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( m_pPlayer.vLoc.x, m_pPlayer.vLoc.y, m_pPlayer.vLoc.z );

// Up vector (i''m still new and still dont know what this is)
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );

// Set the new view matrix
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

Mike Cunningham (First year student)
Mike Cunningham (First year student)
Umm.. you didn''t answer my question.

i want to rotate the camera UP and DOWN. kinda like super mario sunshine without the zooming in at the same time.

I know there are 2 ways to do it:

#1: The mathematically easy way

Get the strafe vector(cross of view and up) and rotate around it. Some reason it didn''t work

#2 The HARD WAY (this should work too but doesn''t)

Move the camera to the origin PLUS the look at point coordinates. So if the camera was at (10, 10, 10) and it was looking at (5, 5, 5) i would then move it to (5, 5, 5).

Then i would rotate the camera until it is aligned with the x axis by rotating it about the y-axis. Then rotate about the z-axis and the rotate it back to the angle it was. Then i move it back and voila it works.

Or at least it should have.

SOME HELP HERE!!!!!!
Wouldn''t you just have to increment or decrement the y-axis coordinate of the lookat vector if you wanted to Look up, or down?

Mike Cunningham (First year student)
Mike Cunningham (First year student)
Try this. (There might be a better way to do it, but it works for me.)

Assuming you have the camera's position, look-at, and up vectors:
- Get the "front" vector which is the vector between the position and look-at point.
- Get the length (d) of the front vector.
- Get the strafe vector using the cross product of the up and front vectors.
- Use D3DXMatrixRotationAxis(&m, &cross, angle) where m will be the rotation matrix.
- Do D3DXVector3Transform(&newup, &up, &m)
- Do D3DXVector3Transform(&newfront, &front, &m)
- Update the up and front vectors using newup and newfront.
- EDIT: Forgot to normalize the new front vector.
- Update the lookat point by doing: lookat = position + (d*front);

Hopefully, I got that right.

-HQ

[edited by - HQ-SOFT on August 16, 2003 11:59:11 PM]
how do iget it so that the character always is "behind" the camera???

This topic is closed to new replies.

Advertisement