2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-01 15:08:23 +08:00
|
|
|
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"
|
|
|
|
|
|
|
|
namespace cocos2d{
|
2010-08-09 10:58:48 +08:00
|
|
|
//
|
|
|
|
// CameraAction
|
|
|
|
//
|
2010-12-22 15:43:54 +08:00
|
|
|
void CCActionCamera::startWithTarget(CCNode *pTarget)
|
2010-08-09 10:58:48 +08:00
|
|
|
{
|
2010-12-22 15:43:54 +08:00
|
|
|
CCActionInterval::startWithTarget(pTarget);
|
2010-08-09 10:58:48 +08:00
|
|
|
|
2010-08-30 15:06:08 +08:00
|
|
|
CCCamera *camera = pTarget->getCamera();
|
2010-08-12 17:01:51 +08:00
|
|
|
camera->getCenterXYZ(&m_fCenterXOrig, &m_fCenterYOrig, &m_fCenterZOrig);
|
|
|
|
camera->getEyeXYZ(&m_fEyeXOrig, &m_fEyeYOrig, &m_fEyeZOrig);
|
|
|
|
camera->getUpXYZ(&m_fUpXOrig, &m_fUpYOrig, &m_fUpZOrig);
|
2010-08-09 10:58:48 +08:00
|
|
|
}
|
|
|
|
|
2010-12-22 15:43:54 +08:00
|
|
|
CCActionInterval * CCActionCamera::reverse()
|
2010-08-09 10:58:48 +08:00
|
|
|
{
|
|
|
|
return CCReverseTime::actionWithAction(this);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// CCOrbitCamera
|
|
|
|
//
|
|
|
|
CCOrbitCamera * CCOrbitCamera::actionWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
|
|
|
|
{
|
|
|
|
CCOrbitCamera * pRet = new CCOrbitCamera();
|
2010-09-04 12:02:52 +08:00
|
|
|
if(pRet->initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-08-09 10:58:48 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCObject * CCOrbitCamera::copyWithZone(CCZone *pZone)
|
2010-08-09 10:58:48 +08:00
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCZone* pNewZone = NULL;
|
2010-08-09 10:58:48 +08:00
|
|
|
CCOrbitCamera* pRet = NULL;
|
|
|
|
if(pZone && pZone->m_pCopyObject) //in case of being called at sub class
|
2010-08-30 15:06:08 +08:00
|
|
|
pRet = (CCOrbitCamera*)(pZone->m_pCopyObject);
|
2010-08-09 10:58:48 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pRet = new CCOrbitCamera();
|
2011-03-07 17:11:57 +08:00
|
|
|
pZone = pNewZone = new CCZone(pRet);
|
2010-08-09 10:58:48 +08:00
|
|
|
}
|
|
|
|
|
2010-12-22 15:43:54 +08:00
|
|
|
CCActionInterval::copyWithZone(pZone);
|
2010-08-09 10:58:48 +08:00
|
|
|
|
|
|
|
pRet->initWithDuration(m_fDuration, m_fRadius, m_fDeltaRadius, m_fAngleZ, m_fDeltaAngleZ, m_fAngleX, m_fDeltaAngleX);
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pNewZone);
|
2010-08-09 10:58:48 +08:00
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCOrbitCamera::initWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
|
2010-08-09 10:58:48 +08:00
|
|
|
{
|
2011-03-19 10:34:26 +08:00
|
|
|
if ( CCActionInterval::initWithDuration(t) )
|
|
|
|
{
|
2010-08-09 10:58:48 +08:00
|
|
|
m_fRadius = radius;
|
|
|
|
m_fDeltaRadius = deltaRadius;
|
|
|
|
m_fAngleZ = angleZ;
|
|
|
|
m_fDeltaAngleZ = deltaAngleZ;
|
|
|
|
m_fAngleX = angleX;
|
|
|
|
m_fDeltaAngleX = deltaAngleX;
|
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
m_fRadDeltaZ = (CCFloat)CC_DEGREES_TO_RADIANS(deltaAngleZ);
|
|
|
|
m_fRadDeltaX = (CCFloat)CC_DEGREES_TO_RADIANS(deltaAngleX);
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-09 10:58:48 +08:00
|
|
|
}
|
2010-09-04 12:02:52 +08:00
|
|
|
return false;
|
2010-08-09 10:58:48 +08:00
|
|
|
}
|
|
|
|
|
2010-08-28 12:02:10 +08:00
|
|
|
void CCOrbitCamera::startWithTarget(CCNode *pTarget)
|
2010-08-09 10:58:48 +08:00
|
|
|
{
|
2010-12-22 15:43:54 +08:00
|
|
|
CCActionInterval::startWithTarget(pTarget);
|
2010-08-09 10:58:48 +08:00
|
|
|
float r, zenith, azimuth;
|
|
|
|
this->sphericalRadius(&r, &zenith, &azimuth);
|
2011-01-15 18:05:35 +08:00
|
|
|
if( isnan(m_fRadius) )
|
2010-08-09 10:58:48 +08:00
|
|
|
m_fRadius = r;
|
2011-01-15 18:05:35 +08:00
|
|
|
if( isnan(m_fAngleZ) )
|
2012-04-13 14:09:02 +08:00
|
|
|
m_fAngleZ = (CCFloat)CC_RADIANS_TO_DEGREES(zenith);
|
2011-01-15 18:05:35 +08:00
|
|
|
if( isnan(m_fAngleX) )
|
2012-04-13 14:09:02 +08:00
|
|
|
m_fAngleX = (CCFloat)CC_RADIANS_TO_DEGREES(azimuth);
|
2010-08-09 10:58:48 +08:00
|
|
|
|
2012-04-13 14:09:02 +08:00
|
|
|
m_fRadZ = (CCFloat)CC_DEGREES_TO_RADIANS(m_fAngleZ);
|
|
|
|
m_fRadX = (CCFloat)CC_DEGREES_TO_RADIANS(m_fAngleX);
|
2010-08-09 10:58:48 +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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-08-30 15:06:08 +08:00
|
|
|
m_pTarget->getCamera()->setEyeXYZ(i,j,k);
|
2010-08-09 10:58:48 +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;
|
|
|
|
|
2010-08-30 15:06:08 +08:00
|
|
|
CCCamera* pCamera = m_pTarget->getCamera();
|
2010-08-09 10:58:48 +08:00
|
|
|
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 )
|
2012-04-13 14:09:02 +08:00
|
|
|
*azimuth= (CCFloat)M_PI - asinf(y/s);
|
2010-08-09 10:58:48 +08:00
|
|
|
else
|
|
|
|
*azimuth = asinf(y/s);
|
|
|
|
|
|
|
|
*newRadius = r / CCCamera::getZEye();
|
|
|
|
}
|
|
|
|
} // namespace cocos2d
|