# 天行健 君子当自强而不息

## 设计一个灵活的Camera类（2）

### 12.2.2围绕任意轴旋转

Builds a matrix that rotates around an arbitrary axis.

`D3DXMATRIX * D3DXMatrixRotationAxis(  D3DXMATRIX * pOut,  CONST D3DXVECTOR3 * pV,  FLOAT Angle);`

#### Parameters

pOut
[in, out] Pointer to the D3DXMATRIX structure that     is the result of the operation.

pV
[in] Pointer to the arbitrary axis. See     D3DXVECTOR3.

Angle
[in] Angle of rotation in radians. Angles are     measured clockwise when looking along the rotation axis toward the origin.

#### Return Values

Pointer to a D3DXMATRIX structure rotated around the specified axis.

#### Remarks

The return value for this function is the same value returned in the pOut parameter. In this way, the D3DXMatrixRotationAxis function can be used as a parameter for another function.

 D3DXMATRIX R;                 D3DXVECTOR3 axis(0.707f,         0.707f, 0.0f);                 D3DXMatrixRotationAxis(&R,         &axis, D3DX_PI / 2.0f);

### 12.2.3Pitch、Yaw和Roll

void cCamera::pitch(float angle)
{
D3DXMATRIX transform_matrix;
D3DXMatrixRotationAxis(&transform_matrix, &m_right, angle);

// rotate m_up and m_look around m_right vector

D3DXVec3TransformCoord(&m_up,   &m_up,   &transform_matrix);
D3DXVec3TransformCoord(&m_look, &m_look, &transform_matrix);
}

void cCamera::yaw(float angle)
{
D3DXMATRIX transform_matrix;

// rotate around world y-axis (0, 1, 0) always for land object

if(m_camera_type == LAND_OBJECT)
D3DXMatrixRotationY(&transform_matrix, angle);

else    // rotate around own up vector for aircraft

D3DXMatrixRotationAxis(&transform_matrix, &m_up, angle);

// rotate m_right and m_look around m_up or y-axis

D3DXVec3TransformCoord(&m_right, &m_right, &transform_matrix);
D3DXVec3TransformCoord(&m_look,  &m_look,  &transform_matrix);
}

void cCamera::roll(float angle)
{

// only roll for aircraft type

if(m_camera_type == AIR_CRAFT)
{
D3DXMATRIX transform_matrix;
D3DXMatrixRotationAxis(&transform_matrix, &m_look, angle);

// rotate m_up and m_right around m_look vector

D3DXVec3TransformCoord(&m_up,     &m_up,        &transform_matrix);
D3DXVec3TransformCoord(&m_right, &m_right,  &transform_matrix);
}
}

### 12.2.4Walking、Strafing和Flying

void cCamera::walk(float units)
{

// move only on xz plane for land object

if(m_camera_type == LAND_OBJECT)
m_pos += D3DXVECTOR3(m_look.x, 0.0f, m_look.z) * units;

else    // AIR_CRAFT

m_pos += m_look * units;
}

void cCamera::strafe(float units)
{

// move only on xz plane for land object

if(m_camera_type == LAND_OBJECT)
m_pos += D3DXVECTOR3(m_right.x, 0.0f, m_right.z) * units;

else    // AIR_CRAFT

m_pos += m_right * units;
}

void cCamera::fly(float units)
{

// move only on y_axis for land object

if(m_camera_type == LAND_OBJECT)
m_pos.y += units;

else
m_pos += m_up * units;
}

posted on 2008-03-30 15:07 lovedday 阅读(1944) 评论(0)  编辑 收藏 引用

 只有注册用户登录后才能发表评论。

• 随笔 - 1360
• 文章 - 75
• 评论 - 520
• 引用 - 0

•