axmol/core/2d/ActionCatmullRom.cpp

513 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/*
* Copyright (c) 2008 Radu Gruian
* Copyright (c) 2011 Vit Valentin
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2013-2016 Chukong Technologies Inc.
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2019-11-23 20:27:39 +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.
*
*
2021-12-25 10:04:45 +08:00
* Original code by Radu Gruian:
* http://www.codeproject.com/Articles/30838/Overhauser-Catmull-Rom-Splines-for-Camera-Animatio.So
2019-11-23 20:27:39 +08:00
*
* Adapted to cocos2d-x by Vit Valentin
*
* Adapted from cocos2d-x to cocos2d-iphone by Ricardo Quesada
*/
#include "base/Macros.h"
#include "2d/ActionCatmullRom.h"
#include "2d/Node.h"
2019-11-23 20:27:39 +08:00
#include <iterator>
using namespace std;
namespace ax
{
2019-11-23 20:27:39 +08:00
/*
* Implementation of PointArray
*/
PointArray* PointArray::create(ssize_t capacity)
{
2021-12-08 00:11:53 +08:00
PointArray* pointArray = new PointArray();
if (pointArray->initWithCapacity(capacity))
2019-11-23 20:27:39 +08:00
{
pointArray->autorelease();
return pointArray;
}
delete pointArray;
return nullptr;
}
bool PointArray::initWithCapacity(ssize_t capacity)
{
_controlPoints.reserve(capacity);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
PointArray* PointArray::clone() const
{
vector<Vec2> newArray = _controlPoints;
2021-12-25 10:04:45 +08:00
PointArray* points = new PointArray();
2019-11-23 20:27:39 +08:00
points->initWithCapacity(10);
points->setControlPoints(std::move(newArray));
points->autorelease();
return points;
}
PointArray::~PointArray()
{
AXLOGV("deallocing PointArray: {}", fmt::ptr(this));
2019-11-23 20:27:39 +08:00
}
PointArray::PointArray() {}
const std::vector<Vec2>& PointArray::getControlPoints() const
{
return _controlPoints;
}
void PointArray::setControlPoints(vector<Vec2> controlPoints)
{
_controlPoints = std::move(controlPoints);
}
void PointArray::addControlPoint(const Vec2& controlPoint)
2021-12-25 10:04:45 +08:00
{
_controlPoints.emplace_back(controlPoint);
2019-11-23 20:27:39 +08:00
}
void PointArray::insertControlPoint(const Vec2& controlPoint, ssize_t index)
{
_controlPoints.insert(std::next(_controlPoints.begin(), index), controlPoint);
}
const Vec2& PointArray::getControlPointAtIndex(ssize_t index) const
{
2021-12-25 10:04:45 +08:00
index = MIN(static_cast<ssize_t>(_controlPoints.size()) - 1, MAX(index, 0));
2019-11-23 20:27:39 +08:00
return _controlPoints.at(index);
}
void PointArray::replaceControlPoint(const Vec2& controlPoint, ssize_t index)
{
_controlPoints.at(index) = controlPoint;
}
void PointArray::removeControlPointAtIndex(ssize_t index)
{
vector<Vec2>::iterator iter = std::next(_controlPoints.begin(), index);
_controlPoints.erase(iter);
}
ssize_t PointArray::count() const
{
return _controlPoints.size();
}
PointArray* PointArray::reverse() const
{
vector<Vec2> newArray;
newArray.reserve(_controlPoints.size());
for (auto iter = _controlPoints.rbegin(), iterRend = _controlPoints.rend(); iter != iterRend; ++iter)
{
newArray.emplace_back(*iter);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
PointArray* config = PointArray::create(0);
2019-11-23 20:27:39 +08:00
config->setControlPoints(std::move(newArray));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return config;
}
void PointArray::reverseInline()
{
const size_t l = _controlPoints.size();
2021-12-25 10:04:45 +08:00
Vec2* p1 = nullptr;
Vec2* p2 = nullptr;
2019-11-23 20:27:39 +08:00
float x, y;
2021-12-25 10:04:45 +08:00
for (size_t i = 0; i < l / 2; ++i)
2019-11-23 20:27:39 +08:00
{
p1 = &_controlPoints.at(i);
2021-12-25 10:04:45 +08:00
p2 = &_controlPoints.at(l - i - 1);
2019-11-23 20:27:39 +08:00
x = p1->x;
y = p1->y;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
p1->x = p2->x;
p1->y = p2->y;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
p2->x = x;
p2->y = y;
}
}
// CatmullRom Spline formula:
2021-12-25 10:04:45 +08:00
Vec2 ccCardinalSplineAt(const Vec2& p0, const Vec2& p1, const Vec2& p2, const Vec2& p3, float tension, float t)
2019-11-23 20:27:39 +08:00
{
float t2 = t * t;
float t3 = t2 * t;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/*
2021-12-25 10:04:45 +08:00
* Formula: s(-ttt + 2tt - t)P1 + s(-ttt + tt)P2 + (2ttt - 3tt + 1)P2 + s(ttt - 2tt + t)P3 + (-2ttt + 3tt)P3 + s(ttt
* - tt)P4
2019-11-23 20:27:39 +08:00
*/
float s = (1 - tension) / 2;
2021-12-25 10:04:45 +08:00
float b1 = s * ((-t3 + (2 * t2)) - t); // s(-t3 + 2 t2 - t)P1
float b2 = s * (-t3 + t2) + (2 * t3 - 3 * t2 + 1); // s(-t3 + t2)P2 + (2 t3 - 3 t2 + 1)P2
float b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); // s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3
float b4 = s * (t3 - t2); // s(t3 - t2)P4
float x = (p0.x * b1 + p1.x * b2 + p2.x * b3 + p3.x * b4);
float y = (p0.y * b1 + p1.y * b2 + p2.y * b3 + p3.y * b4);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
return Vec2(x, y);
2019-11-23 20:27:39 +08:00
}
/* Implementation of CardinalSplineTo
*/
2021-12-25 10:04:45 +08:00
CardinalSplineTo* CardinalSplineTo::create(float duration, PointArray* points, float tension)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CardinalSplineTo* ret = new CardinalSplineTo();
2021-12-08 00:11:53 +08:00
if (ret->initWithDuration(duration, points, tension))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
2021-12-25 10:04:45 +08:00
else
2021-12-08 00:11:53 +08:00
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
bool CardinalSplineTo::initWithDuration(float duration, PointArray* points, float tension)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(points->count() > 0, "Invalid configuration. It must at least have one control point");
2019-11-23 20:27:39 +08:00
if (ActionInterval::initWithDuration(duration))
{
this->setPoints(points);
this->_tension = tension;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
CardinalSplineTo::~CardinalSplineTo()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_points);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
CardinalSplineTo::CardinalSplineTo() : _points(nullptr), _deltaT(0.f), _tension(0.f) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void CardinalSplineTo::startWithTarget(Node* target)
2019-11-23 20:27:39 +08:00
{
ActionInterval::startWithTarget(target);
2021-12-25 10:04:45 +08:00
// _deltaT = (float) 1 / _points->count();
2019-11-23 20:27:39 +08:00
// Issue #1441
2021-12-25 10:04:45 +08:00
_deltaT = (float)1 / (_points->count() - 1);
2019-11-23 20:27:39 +08:00
_previousPosition = target->getPosition();
_accumulatedDiff.setZero();
}
CardinalSplineTo* CardinalSplineTo::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new CardinalSplineTo();
2019-11-23 20:27:39 +08:00
a->initWithDuration(this->_duration, this->_points->clone(), this->_tension);
a->autorelease();
return a;
}
void CardinalSplineTo::update(float time)
{
ssize_t p;
float lt;
// eg.
// p..p..p..p..p..p..p
// 1..2..3..4..5..6..7
// want p to be 1, 2, 3, 4, 5, 6
if (time == 1)
{
2021-12-25 10:04:45 +08:00
p = _points->count() - 1;
2019-11-23 20:27:39 +08:00
lt = 1;
}
2021-12-25 10:04:45 +08:00
else
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
p = (ssize_t)(time / _deltaT);
2019-11-23 20:27:39 +08:00
lt = (time - _deltaT * p) / _deltaT;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Interpolate
2021-12-25 10:04:45 +08:00
Vec2 pp0 = _points->getControlPointAtIndex(p - 1);
Vec2 pp1 = _points->getControlPointAtIndex(p + 0);
Vec2 pp2 = _points->getControlPointAtIndex(p + 1);
Vec2 pp3 = _points->getControlPointAtIndex(p + 2);
2019-11-23 20:27:39 +08:00
Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, _tension, lt);
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_STACKABLE_ACTIONS
2019-11-23 20:27:39 +08:00
// Support for stacked actions
2021-12-25 10:04:45 +08:00
Node* node = _target;
Vec2 diff = node->getPosition() - _previousPosition;
if (diff.x != 0 || diff.y != 0)
2019-11-23 20:27:39 +08:00
{
_accumulatedDiff = _accumulatedDiff + diff;
2021-12-25 10:04:45 +08:00
newPos = newPos + _accumulatedDiff;
2019-11-23 20:27:39 +08:00
}
#endif
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
this->updatePosition(newPos);
}
2021-12-25 10:04:45 +08:00
void CardinalSplineTo::updatePosition(const Vec2& newPos)
2019-11-23 20:27:39 +08:00
{
_target->setPosition(newPos);
_previousPosition = newPos;
}
CardinalSplineTo* CardinalSplineTo::reverse() const
{
2021-12-25 10:04:45 +08:00
PointArray* pReverse = _points->reverse();
2019-11-23 20:27:39 +08:00
return CardinalSplineTo::create(_duration, pReverse, _tension);
}
/* CardinalSplineBy
*/
2021-12-25 10:04:45 +08:00
CardinalSplineBy* CardinalSplineBy::create(float duration, PointArray* points, float tension)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CardinalSplineBy* ret = new CardinalSplineBy();
2021-12-08 00:11:53 +08:00
if (ret->initWithDuration(duration, points, tension))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
2021-12-25 10:04:45 +08:00
else
2021-12-08 00:11:53 +08:00
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
CardinalSplineBy::CardinalSplineBy() : _startPosition(0, 0) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void CardinalSplineBy::updatePosition(const Vec2& newPos)
2019-11-23 20:27:39 +08:00
{
Vec2 p = newPos + _startPosition;
_target->setPosition(p);
_previousPosition = p;
}
CardinalSplineBy* CardinalSplineBy::reverse() const
{
2021-12-25 10:04:45 +08:00
PointArray* copyConfig = _points->clone();
2019-11-23 20:27:39 +08:00
//
// convert "absolutes" to "diffs"
//
Vec2 p = copyConfig->getControlPointAtIndex(0);
for (ssize_t i = 1; i < copyConfig->count(); ++i)
{
Vec2 current = copyConfig->getControlPointAtIndex(i);
2021-12-25 10:04:45 +08:00
Vec2 diff = current - p;
2019-11-23 20:27:39 +08:00
copyConfig->replaceControlPoint(diff, i);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
p = current;
}
// convert to "diffs" to "reverse absolute"
2021-12-25 10:04:45 +08:00
PointArray* pReverse = copyConfig->reverse();
2019-11-23 20:27:39 +08:00
// 1st element (which should be 0,0) should be here too
2021-12-25 10:04:45 +08:00
p = pReverse->getControlPointAtIndex(pReverse->count() - 1);
pReverse->removeControlPointAtIndex(pReverse->count() - 1);
2019-11-23 20:27:39 +08:00
p = -p;
pReverse->insertControlPoint(p, 0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (ssize_t i = 1; i < pReverse->count(); ++i)
{
Vec2 current = pReverse->getControlPointAtIndex(i);
2021-12-25 10:04:45 +08:00
current = -current;
Vec2 abs = current + p;
2019-11-23 20:27:39 +08:00
pReverse->replaceControlPoint(abs, i);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
p = abs;
}
return CardinalSplineBy::create(_duration, pReverse, _tension);
}
2021-12-25 10:04:45 +08:00
void CardinalSplineBy::startWithTarget(Node* target)
{
2019-11-23 20:27:39 +08:00
CardinalSplineTo::startWithTarget(target);
_startPosition = target->getPosition();
}
CardinalSplineBy* CardinalSplineBy::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new CardinalSplineBy();
2019-11-23 20:27:39 +08:00
a->initWithDuration(this->_duration, this->_points->clone(), this->_tension);
a->autorelease();
return a;
}
/* CatmullRomTo
*/
2021-12-25 10:04:45 +08:00
CatmullRomTo* CatmullRomTo::create(float dt, PointArray* points)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CatmullRomTo* ret = new CatmullRomTo();
2021-12-08 00:11:53 +08:00
if (ret->initWithDuration(dt, points))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
2021-12-25 10:04:45 +08:00
else
2021-12-08 00:11:53 +08:00
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
bool CatmullRomTo::initWithDuration(float dt, PointArray* points)
2019-11-23 20:27:39 +08:00
{
if (CardinalSplineTo::initWithDuration(dt, points, 0.5f))
{
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
CatmullRomTo* CatmullRomTo::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new CatmullRomTo();
2019-11-23 20:27:39 +08:00
a->initWithDuration(this->_duration, this->_points->clone());
a->autorelease();
return a;
}
CatmullRomTo* CatmullRomTo::reverse() const
{
2021-12-25 10:04:45 +08:00
PointArray* reverse = _points->reverse();
2019-11-23 20:27:39 +08:00
return CatmullRomTo::create(_duration, reverse);
}
/* CatmullRomBy
*/
2021-12-25 10:04:45 +08:00
CatmullRomBy* CatmullRomBy::create(float dt, PointArray* points)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CatmullRomBy* ret = new CatmullRomBy();
2021-12-08 00:11:53 +08:00
if (ret->initWithDuration(dt, points))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
2021-12-25 10:04:45 +08:00
else
2021-12-08 00:11:53 +08:00
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
bool CatmullRomBy::initWithDuration(float dt, PointArray* points)
2019-11-23 20:27:39 +08:00
{
if (CardinalSplineTo::initWithDuration(dt, points, 0.5f))
{
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
CatmullRomBy* CatmullRomBy::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new CatmullRomBy();
2019-11-23 20:27:39 +08:00
a->initWithDuration(this->_duration, this->_points->clone());
a->autorelease();
return a;
}
CatmullRomBy* CatmullRomBy::reverse() const
{
2021-12-25 10:04:45 +08:00
PointArray* copyConfig = _points->clone();
2019-11-23 20:27:39 +08:00
//
// convert "absolutes" to "diffs"
//
Vec2 p = copyConfig->getControlPointAtIndex(0);
for (ssize_t i = 1; i < copyConfig->count(); ++i)
{
Vec2 current = copyConfig->getControlPointAtIndex(i);
2021-12-25 10:04:45 +08:00
Vec2 diff = current - p;
2019-11-23 20:27:39 +08:00
copyConfig->replaceControlPoint(diff, i);
p = current;
}
// convert to "diffs" to "reverse absolute"
2021-12-25 10:04:45 +08:00
PointArray* reverse = copyConfig->reverse();
2019-11-23 20:27:39 +08:00
// 1st element (which should be 0,0) should be here too
2021-12-25 10:04:45 +08:00
p = reverse->getControlPointAtIndex(reverse->count() - 1);
reverse->removeControlPointAtIndex(reverse->count() - 1);
2019-11-23 20:27:39 +08:00
p = -p;
reverse->insertControlPoint(p, 0);
for (ssize_t i = 1; i < reverse->count(); ++i)
{
Vec2 current = reverse->getControlPointAtIndex(i);
2021-12-25 10:04:45 +08:00
current = -current;
Vec2 abs = current + p;
2019-11-23 20:27:39 +08:00
reverse->replaceControlPoint(abs, i);
p = abs;
}
return CatmullRomBy::create(_duration, reverse);
}
}