球形坐标和Cartesian 坐标的转换 spherical coordinate
??????????? spherical coordinate 和cartesian坐標的轉換, 個人認為在控制camera的時候最為有用,比如CS中的操作方式, 鼠標負責方向的改變,其恰恰就是球形坐標的改變。而camera的位置改變就是cartesian的改變,所以這兩者的轉換就必不可少了。
??????????? 以下是基本的數學公式,非常簡單。
Thus, we can convert from spherical to Cartesian coordinates by
?
We convert from Cartesian to spherical coordinates by
我的代碼,比較爛不要見笑, 沒什么注釋,比較簡單大家應該可以看懂:
//_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
轉載于:https://www.cnblogs.com/chiyuwang/archive/2006/09/04/494676.html
總結
以上是生活随笔為你收集整理的球形坐标和Cartesian 坐标的转换 spherical coordinate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot学习笔记2
- 下一篇: Python中的pip怎么配置环境变量