2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 15:08:23 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2011-03-19 10:34:26 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCActionCamera.h"
|
|
|
|
#include "2d/CCNode.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCStdC.h"
|
2013-06-16 09:54:34 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2012-04-18 18:43:45 +08:00
|
|
|
//
|
|
|
|
// CameraAction
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
ActionCamera::ActionCamera() : _center(0, 0, 0), _eye(0, 0, FLT_EPSILON), _up(0, 1, 0) {}
|
|
|
|
void ActionCamera::startWithTarget(Node* target)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-18 07:56:19 +08:00
|
|
|
ActionInterval::startWithTarget(target);
|
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
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto action = new ActionCamera();
|
|
|
|
action->autorelease();
|
|
|
|
return action;
|
2013-06-14 08:25:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +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-12-21 08:33:31 +08:00
|
|
|
|
|
|
|
void ActionCamera::restore()
|
|
|
|
{
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_center.setZero();
|
|
|
|
_eye.set(0.0f, 0.0f, FLT_EPSILON);
|
|
|
|
_up.set(0.0f, 1.0f, 0.0f);
|
2014-01-14 04:52:07 +08:00
|
|
|
}
|
2013-12-21 08:33:31 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ActionCamera::setEye(const Vec3& eye)
|
2014-01-14 04:52:07 +08:00
|
|
|
{
|
|
|
|
_eye = eye;
|
|
|
|
updateTransform();
|
2013-12-21 08:33:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionCamera::setEye(float x, float y, float z)
|
|
|
|
{
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_eye.set(x, y, z);
|
2013-12-21 08:33:31 +08:00
|
|
|
updateTransform();
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ActionCamera::setCenter(const Vec3& center)
|
2013-12-21 08:33:31 +08:00
|
|
|
{
|
2014-01-14 04:52:07 +08:00
|
|
|
_center = center;
|
2013-12-21 08:33:31 +08:00
|
|
|
updateTransform();
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ActionCamera::setUp(const Vec3& up)
|
2013-12-21 08:33:31 +08:00
|
|
|
{
|
2014-01-14 04:52:07 +08:00
|
|
|
_up = up;
|
2013-12-21 08:33:31 +08:00
|
|
|
updateTransform();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActionCamera::updateTransform()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 lookupMatrix;
|
|
|
|
Mat4::createLookAt(_eye.x, _eye.y, _eye.z, _center.x, _center.y, _center.z, _up.x, _up.y, _up.z, &lookupMatrix);
|
2013-12-21 11:19:24 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 anchorPoint = _target->getAnchorPointInPoints();
|
2013-12-21 11:19:24 +08:00
|
|
|
|
2015-04-20 01:40:52 +08:00
|
|
|
bool needsTranslation = !anchorPoint.isZero();
|
2013-12-21 11:19:24 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 mv = Mat4::IDENTITY;
|
2013-12-21 11:19:24 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (needsTranslation)
|
2016-04-08 13:40:36 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 t;
|
|
|
|
Mat4::createTranslation(anchorPoint.x, anchorPoint.y, 0, &t);
|
2014-04-11 14:00:55 +08:00
|
|
|
mv = mv * t;
|
2013-12-21 11:19:24 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-11 14:00:55 +08:00
|
|
|
mv = mv * lookupMatrix;
|
2013-12-21 11:19:24 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (needsTranslation)
|
2016-04-08 13:40:36 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 t;
|
|
|
|
Mat4::createTranslation(-anchorPoint.x, -anchorPoint.y, 0, &t);
|
2014-04-11 14:00:55 +08:00
|
|
|
mv = mv * t;
|
2013-12-21 11:19:24 +08:00
|
|
|
}
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: Using the AdditionalTransform is a complete hack.
|
2015-09-09 11:37:41 +08:00
|
|
|
// This should be done by multiplying the lookup-Matrix with the Node's MV matrix
|
2013-12-21 11:37:14 +08:00
|
|
|
// And then setting the result as the new MV matrix
|
|
|
|
// But that operation needs to be done after all the 'updates'.
|
|
|
|
// So the Director should emit an 'director_after_update' event.
|
|
|
|
// And this object should listen to it
|
2014-04-11 14:00:55 +08:00
|
|
|
_target->setAdditionalTransform(&mv);
|
2013-12-21 08:33:31 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-03-28 18:00:25 +08:00
|
|
|
OrbitCamera::OrbitCamera()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _radius(0.0)
|
|
|
|
, _deltaRadius(0.0)
|
|
|
|
, _angleZ(0.0)
|
|
|
|
, _deltaAngleZ(0.0)
|
|
|
|
, _angleX(0.0)
|
|
|
|
, _deltaAngleX(0.0)
|
|
|
|
, _radZ(0.0)
|
|
|
|
, _radDeltaZ(0.0)
|
|
|
|
, _radX(0.0)
|
|
|
|
, _radDeltaX(0.0)
|
|
|
|
{}
|
|
|
|
OrbitCamera::~OrbitCamera() {}
|
|
|
|
|
|
|
|
OrbitCamera* OrbitCamera::create(float t,
|
|
|
|
float radius,
|
|
|
|
float deltaRadius,
|
|
|
|
float angleZ,
|
|
|
|
float deltaAngleZ,
|
|
|
|
float angleX,
|
|
|
|
float deltaAngleX)
|
|
|
|
{
|
|
|
|
OrbitCamera* obitCamera = new OrbitCamera();
|
|
|
|
if (obitCamera->initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-16 21:08:00 +08:00
|
|
|
obitCamera->autorelease();
|
|
|
|
return obitCamera;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-04-08 13:40:36 +08:00
|
|
|
delete obitCamera;
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
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
|
|
|
{
|
2016-04-08 13:40:36 +08:00
|
|
|
// no copy constructor
|
|
|
|
return OrbitCamera::create(_duration, _radius, _deltaRadius, _angleZ, _deltaAngleZ, _angleX, _deltaAngleX);
|
2013-06-14 08:25:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +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
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ActionInterval::initWithDuration(t))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_radius = radius;
|
2013-06-15 14:03:30 +08:00
|
|
|
_deltaRadius = deltaRadius;
|
2021-12-25 10:04:45 +08:00
|
|
|
_angleZ = angleZ;
|
2013-06-15 14:03:30 +08:00
|
|
|
_deltaAngleZ = deltaAngleZ;
|
2021-12-25 10:04:45 +08:00
|
|
|
_angleX = angleX;
|
2013-06-15 14:03:30 +08:00
|
|
|
_deltaAngleX = deltaAngleX;
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_radDeltaZ = (float)AX_DEGREES_TO_RADIANS(deltaAngleZ);
|
|
|
|
_radDeltaX = (float)AX_DEGREES_TO_RADIANS(deltaAngleX);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void OrbitCamera::startWithTarget(Node* target)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-21 11:19:24 +08:00
|
|
|
ActionCamera::startWithTarget(target);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float r, zenith, azimuth;
|
|
|
|
this->sphericalRadius(&r, &zenith, &azimuth);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (std::isnan(_radius))
|
2013-06-15 14:03:30 +08:00
|
|
|
_radius = r;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (std::isnan(_angleZ))
|
2022-07-15 19:17:01 +08:00
|
|
|
_angleZ = (float)AX_RADIANS_TO_DEGREES(zenith);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (std::isnan(_angleX))
|
2022-07-15 19:17:01 +08:00
|
|
|
_angleX = (float)AX_RADIANS_TO_DEGREES(azimuth);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_radZ = (float)AX_DEGREES_TO_RADIANS(_angleZ);
|
|
|
|
_radX = (float)AX_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
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float r = (_radius + _deltaRadius * dt) * FLT_EPSILON;
|
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
|
|
|
|
2014-01-14 04:52:07 +08:00
|
|
|
float i = sinf(za) * cosf(xa) * r + _center.x;
|
|
|
|
float j = sinf(za) * sinf(xa) * r + _center.y;
|
|
|
|
float k = cosf(za) * r + _center.z;
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
setEye(i, j, k);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void OrbitCamera::sphericalRadius(float* newRadius, float* zenith, float* azimuth)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float r; // radius
|
2012-04-19 14:35:52 +08:00
|
|
|
float s;
|
|
|
|
|
2014-01-14 04:52:07 +08:00
|
|
|
float x = _eye.x - _center.x;
|
|
|
|
float y = _eye.y - _center.y;
|
|
|
|
float z = _eye.z - _center.z;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
r = sqrtf(powf(x, 2) + powf(y, 2) + powf(z, 2));
|
|
|
|
s = sqrtf(powf(x, 2) + powf(y, 2));
|
|
|
|
if (s == 0.0f)
|
2012-04-19 14:35:52 +08:00
|
|
|
s = FLT_EPSILON;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (r == 0.0f)
|
2012-04-19 14:35:52 +08:00
|
|
|
r = FLT_EPSILON;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
*zenith = acosf(z / r);
|
|
|
|
if (x < 0)
|
|
|
|
*azimuth = (float)M_PI - asinf(y / s);
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
*azimuth = asinf(y / s);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-21 08:33:31 +08:00
|
|
|
*newRadius = r / FLT_EPSILON;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|