spherical coordinate 和cartesian坐标的转换, 个人认为在控制camera的时候最为有用,比如CS中的操作方式, 鼠标负责方向的改变,其恰恰就是球形坐标的改变。而camera的位置改变就是cartesian的改变,所以这两者的转换就必不可少了。
            以下是基本的数学公式,非常简单。

Thus, we can convert from spherical to Cartesian coordinates by

Missing equation

 

We convert from Cartesian to spherical coordinates by

Missing equation

我的代码,比较烂不要见笑, 没什么注释,比较简单大家应该可以看懂:

//_horizon, _vertical是球形坐标的改变大小

void AO_Camera::moveRatate( float _horizon, float _vertical )

{

float r, s, thi, theta, x, y, z;

x = m_forwardVector.x;

y = m_forwardVector.y;

z = m_forwardVector.z;

r = D3DXVec3Length( &m_forwardVector);

s = D3DXVec3Length( &D3DXVECTOR3( x, 0.0f, z) );

thi = acos( y / r );

//限制theta的改变范围
if
( x <= 0 )

{

theta = PI - (float)asinf( z / s );

}

else

{

theta = (float)asinf( z / s );

}

 

 


thi = thi + _vertical;

if ( thi <= PRECISION )

{

thi = PRECISION;

}

else if( thi >= PI - PRECISION )

{

thi = PI - PRECISION;

}

theta = theta - _horizon;

m_forwardVector.x = r * sin( thi ) * cos( theta );

m_forwardVector.z = r * sin( thi ) * sin( theta );

m_forwardVector.y = r * cos( thi );

 

m_LookAt = m_EyePos + m_forwardVector;

 

}

作者> chiyuwang

posted on 2006-09-04 20:29  Wang Chiyu  阅读(1696)  评论(0编辑  收藏  举报