.h
#ifndef __CCCAMERA_ACTION_H__
#define __CCCAMERA_ACTION_H__
#include "CCActionInterval.h"
NS_CC_BEGIN
class CCCamera;
@brief Base class for CCCamera actions
@ingroup Actions
*/
class CC_DLL CCActionCamera : public CCActionInterval //<NSCopying> //摄像机动作基类
{
public:
CCActionCamera()
:m_fCenterXOrig(0)
,m_fCenterYOrig(0)
,m_fCenterZOrig(0)
,m_fEyeXOrig(0)
,m_fEyeYOrig(0)
,m_fEyeZOrig(0)
,m_fUpXOrig(0)
,m_fUpYOrig(0)
,m_fUpZOrig(0)
{}
virtual ~CCActionCamera(){}
// super methods
virtual void startWithTarget(CCNode *pTarget);
virtual CCActionInterval * reverse();
protected:
float m_fCenterXOrig;
float m_fCenterYOrig;
float m_fCenterZOrig;
float m_fEyeXOrig;
float m_fEyeYOrig;
float m_fEyeZOrig;
float m_fUpXOrig;
float m_fUpYOrig;
float m_fUpZOrig;
};
/**
@brief CCOrbitCamera action
Orbits(轨道) the camera around the center of the screen using spherical(球面) coordinates
*/
class CC_DLL CCOrbitCamera : public CCActionCamera //<NSCopying>
{
public:
CCOrbitCamera()
: m_fRadius(0.0)
, m_fDeltaRadius(0.0)
, m_fAngleZ(0.0)
, m_fDeltaAngleZ(0.0)
, m_fAngleX(0.0)
, m_fDeltaAngleX(0.0)
, m_fRadZ(0.0)
, m_fRadDeltaZ(0.0)
, m_fRadX(0.0)
, m_fRadDeltaX(0.0)
{}
~CCOrbitCamera(){}
/** creates a CCOrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX */
static CCOrbitCamera* create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX);
/** initializes a CCOrbitCamera action with radius, delta-radius, z, deltaZ, x, deltaX */
bool initWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX);
/** positions the camera according to(根据) spherical coordinates */
void sphericalRadius(float *r, float *zenith, float *azimuth) ; //通过球坐标得到摄像机在坐标系中的位置
// super methods
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void startWithTarget(CCNode *pTarget);
virtual void update(float time);
protected:
float m_fRadius;
float m_fDeltaRadius;
float m_fAngleZ;
float m_fDeltaAngleZ;
float m_fAngleX;
float m_fDeltaAngleX;
float m_fRadZ;
float m_fRadDeltaZ;
float m_fRadX;
float m_fRadDeltaX;
};
// end of actions group
/// @}
NS_CC_END
#endif //__CCCAMERA_ACTION_H__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.cpp
void CCOrbitCamera::update(float dt)
{
float r = (m_fRadius + m_fDeltaRadius * dt) * CCCamera::getZEye();
float za = m_fRadZ + m_fRadDeltaZ * dt;
float xa = m_fRadX + m_fRadDeltaX * dt;
float i = sinf(za) * cosf(xa) * r + m_fCenterXOrig;
float j = sinf(za) * sinf(xa) * r + m_fCenterYOrig;
float k = cosf(za) * r + m_fCenterZOrig;
m_pTarget->getCamera()->setEyeXYZ(i,j,k);
}
void CCOrbitCamera::sphericalRadius(float *newRadius, float *zenith, float *azimuth)
{
float ex, ey, ez, cx, cy, cz, x, y, z;
float r; // radius
float s;
CCCamera* pCamera = m_pTarget->getCamera();
pCamera->getEyeXYZ(&ex, &ey, &ez);
pCamera->getCenterXYZ(&cx, &cy, &cz);
x = ex-cx;
y = ey-cy;
z = ez-cz;
r = sqrtf( powf(x,2) + powf(y,2) + powf(z,2));
s = sqrtf( powf(x,2) + powf(y,2));
if( s == 0.0f )
s = FLT_EPSILON;
if(r==0.0f)
r = FLT_EPSILON;
*zenith = acosf( z/r);
if( x < 0 )
*azimuth= (float)M_PI - asinf(y/s);
else
*azimuth = asinf(y/s);
*newRadius = r / CCCamera::getZEye();
}