axmol/cocos2dx/actions/CCActionCamera.cpp

163 lines
5.0 KiB
C++
Raw Normal View History

2011-03-19 10:34:26 +08:00
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
2011-03-19 10:34:26 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCActionCamera.h"
#include "CCNode.h"
#include "CCCamera.h"
#include "CCStdC.h"
#include "CCZone.h"
2011-03-19 10:34:26 +08:00
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
//
// CameraAction
//
void CCActionCamera::startWithTarget(CCNode *pTarget)
{
CCActionInterval::startWithTarget(pTarget);
CCCamera *camera = pTarget->getCamera();
camera->getCenterXYZ(&m_fCenterXOrig, &m_fCenterYOrig, &m_fCenterZOrig);
camera->getEyeXYZ(&m_fEyeXOrig, &m_fEyeYOrig, &m_fEyeZOrig);
camera->getUpXYZ(&m_fUpXOrig, &m_fUpYOrig, &m_fUpZOrig);
2012-04-18 18:43:45 +08:00
}
CCActionInterval * CCActionCamera::reverse()
{
return CCReverseTime::actionWithAction(this);
2012-04-18 18:43:45 +08:00
}
//
// CCOrbitCamera
//
CCOrbitCamera * CCOrbitCamera::actionWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
{
CCOrbitCamera * pRet = new CCOrbitCamera();
if(pRet->initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2012-04-18 18:43:45 +08:00
}
CCObject * CCOrbitCamera::copyWithZone(CCZone *pZone)
{
CCZone* pNewZone = NULL;
CCOrbitCamera* pRet = NULL;
if(pZone && pZone->m_pCopyObject) //in case of being called at sub class
pRet = (CCOrbitCamera*)(pZone->m_pCopyObject);
else
{
pRet = new CCOrbitCamera();
pZone = pNewZone = new CCZone(pRet);
}
CCActionInterval::copyWithZone(pZone);
pRet->initWithDuration(m_fDuration, m_fRadius, m_fDeltaRadius, m_fAngleZ, m_fDeltaAngleZ, m_fAngleX, m_fDeltaAngleX);
CC_SAFE_DELETE(pNewZone);
return pRet;
2012-04-18 18:43:45 +08:00
}
2010-08-09 10:58:48 +08:00
2012-04-18 18:43:45 +08:00
bool CCOrbitCamera::initWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
{
if ( CCActionInterval::initWithDuration(t) )
{
m_fRadius = radius;
m_fDeltaRadius = deltaRadius;
m_fAngleZ = angleZ;
m_fDeltaAngleZ = deltaAngleZ;
m_fAngleX = angleX;
m_fDeltaAngleX = deltaAngleX;
m_fRadDeltaZ = (CCFloat)CC_DEGREES_TO_RADIANS(deltaAngleZ);
m_fRadDeltaX = (CCFloat)CC_DEGREES_TO_RADIANS(deltaAngleX);
return true;
}
return false;
2012-04-18 18:43:45 +08:00
}
void CCOrbitCamera::startWithTarget(CCNode *pTarget)
{
CCActionInterval::startWithTarget(pTarget);
float r, zenith, azimuth;
this->sphericalRadius(&r, &zenith, &azimuth);
if( isnan(m_fRadius) )
m_fRadius = r;
if( isnan(m_fAngleZ) )
m_fAngleZ = (CCFloat)CC_RADIANS_TO_DEGREES(zenith);
if( isnan(m_fAngleX) )
m_fAngleX = (CCFloat)CC_RADIANS_TO_DEGREES(azimuth);
m_fRadZ = (CCFloat)CC_DEGREES_TO_RADIANS(m_fAngleZ);
m_fRadX = (CCFloat)CC_DEGREES_TO_RADIANS(m_fAngleX);
2012-04-18 18:43:45 +08:00
}
void CCOrbitCamera::update(ccTime dt)
{
float r = (m_fRadius + m_fDeltaRadius * dt) * CCCamera::getZEye();
float za = m_fRadZ + m_fRadDeltaZ * dt;
float xa = m_fRadX + m_fRadDeltaX * dt;
2012-04-18 18:43:45 +08:00
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;
2012-04-18 18:43:45 +08:00
m_pTarget->getCamera()->setEyeXYZ(i,j,k);
2012-04-18 18:43:45 +08:00
}
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= (CCFloat)M_PI - asinf(y/s);
else
*azimuth = asinf(y/s);
*newRadius = r / CCCamera::getZEye();
2012-04-18 18:43:45 +08:00
}
NS_CC_END