2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 10:34:26 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-06-16 09:54:34 +08:00
|
|
|
|
2011-03-19 10:34:26 +08:00
|
|
|
#include "CCActionCamera.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "base_nodes/CCNode.h"
|
2011-03-19 10:34:26 +08:00
|
|
|
#include "CCCamera.h"
|
|
|
|
#include "CCStdC.h"
|
2013-06-16 09:54:34 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
//
|
|
|
|
// CameraAction
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
void ActionCamera::startWithTarget(Node *pTarget)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval::startWithTarget(pTarget);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Camera *camera = pTarget->getCamera();
|
2013-06-15 14:03:30 +08:00
|
|
|
camera->getCenterXYZ(&_centerXOrig, &_centerYOrig, &_centerZOrig);
|
|
|
|
camera->getEyeXYZ(&_eyeXOrig, &_eyeYOrig, &_eyeZOrig);
|
|
|
|
camera->getUpXYZ(&_upXOrig, &_upYOrig, &_upZOrig);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionCamera* ActionCamera::clone() const
|
2013-06-14 08:25:14 +08:00
|
|
|
{
|
2013-06-19 06:06:53 +08:00
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new ActionCamera();
|
2013-06-14 08:25:14 +08:00
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionCamera * ActionCamera::reverse() const
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-10 14:17:42 +08:00
|
|
|
// FIXME: This conversion isn't safe.
|
2013-06-20 14:13:12 +08:00
|
|
|
return (ActionCamera*)ReverseTime::create(const_cast<ActionCamera*>(this));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// OrbitCamera
|
2012-04-18 18:43:45 +08:00
|
|
|
//
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
OrbitCamera * OrbitCamera::create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
OrbitCamera * pRet = new OrbitCamera();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
OrbitCamera* OrbitCamera::clone() const
|
2013-06-14 08:25:14 +08:00
|
|
|
{
|
2013-06-19 06:06:53 +08:00
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new OrbitCamera();
|
2013-06-15 14:03:30 +08:00
|
|
|
a->initWithDuration(_duration, _radius, _deltaRadius, _angleZ, _deltaAngleZ, _angleX, _deltaAngleX);
|
2013-06-14 08:25:14 +08:00
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool OrbitCamera::initWithDuration(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if ( ActionInterval::initWithDuration(t) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_radius = radius;
|
|
|
|
_deltaRadius = deltaRadius;
|
|
|
|
_angleZ = angleZ;
|
|
|
|
_deltaAngleZ = deltaAngleZ;
|
|
|
|
_angleX = angleX;
|
|
|
|
_deltaAngleX = deltaAngleX;
|
|
|
|
|
|
|
|
_radDeltaZ = (float)CC_DEGREES_TO_RADIANS(deltaAngleZ);
|
|
|
|
_radDeltaX = (float)CC_DEGREES_TO_RADIANS(deltaAngleX);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void OrbitCamera::startWithTarget(Node *pTarget)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval::startWithTarget(pTarget);
|
2012-04-19 14:35:52 +08:00
|
|
|
float r, zenith, azimuth;
|
|
|
|
this->sphericalRadius(&r, &zenith, &azimuth);
|
2013-06-15 14:03:30 +08:00
|
|
|
if( isnan(_radius) )
|
|
|
|
_radius = r;
|
|
|
|
if( isnan(_angleZ) )
|
|
|
|
_angleZ = (float)CC_RADIANS_TO_DEGREES(zenith);
|
|
|
|
if( isnan(_angleX) )
|
|
|
|
_angleX = (float)CC_RADIANS_TO_DEGREES(azimuth);
|
|
|
|
|
|
|
|
_radZ = (float)CC_DEGREES_TO_RADIANS(_angleZ);
|
|
|
|
_radX = (float)CC_DEGREES_TO_RADIANS(_angleX);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void OrbitCamera::update(float dt)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
float r = (_radius + _deltaRadius * dt) * Camera::getZEye();
|
2013-06-15 14:03:30 +08:00
|
|
|
float za = _radZ + _radDeltaZ * dt;
|
|
|
|
float xa = _radX + _radDeltaX * dt;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
float i = sinf(za) * cosf(xa) * r + _centerXOrig;
|
|
|
|
float j = sinf(za) * sinf(xa) * r + _centerYOrig;
|
|
|
|
float k = cosf(za) * r + _centerZOrig;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_target->getCamera()->setEyeXYZ(i,j,k);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void OrbitCamera::sphericalRadius(float *newRadius, float *zenith, float *azimuth)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
float ex, ey, ez, cx, cy, cz, x, y, z;
|
|
|
|
float r; // radius
|
|
|
|
float s;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Camera* pCamera = _target->getCamera();
|
2012-04-19 14:35:52 +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-08-01 15:30:12 +08:00
|
|
|
*azimuth= (float)M_PI - asinf(y/s);
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
|
|
|
*azimuth = asinf(y/s);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
*newRadius = r / Camera::getZEye();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|