球形坐标和Cartesian 坐标的转换 spherical coordinate

c5.asp?nm=chiyuwang

            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

转载于:https://www.cnblogs.com/chiyuwang/archive/2006/09/04/494676.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值