球坐标和笛卡尔坐标的转换 我个人觉得coordinate
终极管理员 知识笔记 48阅读
的球坐标和坐标的变换在控制相机的时候最有用,比如CS里的操作模式,鼠标负责方向的变化,准确来说就是球坐标的变化。而相机位置的变化就是坐标的变化,所以两者之间的转换是必不可少的。下面是基本的数学公式,很简单。因此,我们可以通过用我的代码转换from spherical to Cartesian coordinates 来转换from Cartesian to spherical coordinates 。不要笑,没有评论,比较简单,大家应该能看懂3360//_ horizon。_vertical是球坐标void ao _ camera :3360 mobile(float _ horizon,float _ vertical) {float r,s,thi,theta,x,y,z;x=m _ forwardVector.xy=m _ forwardVector.yz=m _ forwardVector.zr=d 3 dx vec 3 length(m _ forward vector);s=D3DXVec3Length(D3DXVECTOR3(x,0.0f,z));thi=acos(y/r);
="Helvetica, Arial, Arial Narrow">//限制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