axmol/core/2d/CCActionGrid3D.cpp

739 lines
17 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2009 On-Core
Copyright (c) 2010-2012 cocos2d-x.org
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-08-08 18:02:17 +08:00
https://axys1.github.io/
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.
****************************************************************************/
#include "2d/CCActionGrid3D.h"
#include "base/CCDirector.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
// implementation of Waves3D
2021-10-23 23:27:14 +08:00
Waves3D* Waves3D::create(float duration, const Vec2& gridSize, unsigned int waves, float amplitude)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Waves3D* action = new Waves3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, waves, amplitude))
2019-11-23 20:27:39 +08:00
{
action->autorelease();
return action;
}
delete action;
return nullptr;
}
2021-10-23 23:27:14 +08:00
bool Waves3D::initWithDuration(float duration, const Vec2& gridSize, unsigned int waves, float amplitude)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
2021-12-25 10:04:45 +08:00
_waves = waves;
_amplitude = amplitude;
2019-11-23 20:27:39 +08:00
_amplitudeRate = 1.0f;
return true;
}
return false;
}
Waves3D* Waves3D::clone() const
{
// no copy constructor
return Waves3D::create(_duration, _gridSize, _waves, _amplitude);
}
void Waves3D::update(float time)
{
int i, j;
for (i = 0; i < _gridSize.width + 1; ++i)
{
for (j = 0; j < _gridSize.height + 1; ++j)
{
Vec2 pos((float)i, (float)j);
Vec3 v = getOriginalVertex(pos);
2021-12-25 10:04:45 +08:00
v.z += (sinf((float)M_PI * time * _waves * 2 + (v.y + v.x) * 0.01f) * _amplitude * _amplitudeRate);
2022-07-16 10:43:05 +08:00
// AXLOG("v.z offset is %f\n", (sinf((float)M_PI * time * _waves * 2 + (v.y+v.x) * .01f) * _amplitude *
2021-12-25 10:04:45 +08:00
// _amplitudeRate));
2019-11-23 20:27:39 +08:00
setVertex(pos, v);
}
}
}
// implementation of FlipX3D
FlipX3D* FlipX3D::create(float duration)
{
2021-12-25 10:04:45 +08:00
FlipX3D* action = new FlipX3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration))
2019-11-23 20:27:39 +08:00
{
action->autorelease();
return action;
}
delete action;
return nullptr;
}
bool FlipX3D::initWithDuration(float duration)
{
2021-10-23 23:27:14 +08:00
return Grid3DAction::initWithDuration(duration, Vec2(1, 1));
2019-11-23 20:27:39 +08:00
}
2021-10-23 23:27:14 +08:00
bool FlipX3D::initWithSize(const Vec2& gridSize, float duration)
2019-11-23 20:27:39 +08:00
{
if (gridSize.width != 1 || gridSize.height != 1)
{
// Grid size must be (1,1)
2022-07-16 10:43:05 +08:00
AXASSERT(0, "Grid size must be (1,1)");
2019-11-23 20:27:39 +08:00
return false;
}
return Grid3DAction::initWithDuration(duration, gridSize);
}
FlipX3D* FlipX3D::clone() const
{
2021-12-25 10:04:45 +08:00
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new FlipX3D();
2019-11-23 20:27:39 +08:00
a->initWithSize(_gridSize, _duration);
a->autorelease();
return a;
}
void FlipX3D::update(float time)
{
2021-12-25 10:04:45 +08:00
float angle = (float)M_PI * time; // 180 degrees
float mz = sinf(angle);
angle = angle / 2.0f; // x calculates degrees from 0 to 90
float mx = cosf(angle);
2019-11-23 20:27:39 +08:00
Vec3 v0, v1, v, diff;
v0 = getOriginalVertex(Vec2(1.0f, 1.0f));
v1 = getOriginalVertex(Vec2());
2021-12-25 10:04:45 +08:00
float x0 = v0.x;
float x1 = v1.x;
float x;
Vec2 a, b, c, d;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
if (x0 > x1)
2019-11-23 20:27:39 +08:00
{
// Normal Grid
a.setZero();
b.set(0.0f, 1.0f);
c.set(1.0f, 0.0f);
d.set(1.0f, 1.0f);
x = x0;
}
else
{
// Reversed Grid
c.setZero();
d.set(0.0f, 1.0f);
a.set(1.0f, 0.0f);
b.set(1.0f, 1.0f);
x = x1;
}
2021-12-25 10:04:45 +08:00
diff.x = (x - x * mx);
diff.z = fabsf(floorf((x * mz) / 4.0f));
2019-11-23 20:27:39 +08:00
// bottom-left
2021-12-25 10:04:45 +08:00
v = getOriginalVertex(a);
2019-11-23 20:27:39 +08:00
v.x = diff.x;
v.z += diff.z;
setVertex(a, v);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// upper-left
2021-12-25 10:04:45 +08:00
v = getOriginalVertex(b);
2019-11-23 20:27:39 +08:00
v.x = diff.x;
v.z += diff.z;
setVertex(b, v);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// bottom-right
v = getOriginalVertex(c);
v.x -= diff.x;
v.z -= diff.z;
setVertex(c, v);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// upper-right
v = getOriginalVertex(d);
v.x -= diff.x;
v.z -= diff.z;
setVertex(d, v);
}
// implementation of FlipY3D
FlipY3D* FlipY3D::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new FlipY3D();
2019-11-23 20:27:39 +08:00
a->initWithSize(_gridSize, _duration);
a->autorelease();
return a;
}
FlipY3D* FlipY3D::create(float duration)
{
2021-12-25 10:04:45 +08:00
FlipY3D* action = new FlipY3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2019-11-23 20:27:39 +08:00
}
return action;
}
void FlipY3D::update(float time)
{
2021-12-25 10:04:45 +08:00
float angle = (float)M_PI * time; // 180 degrees
float mz = sinf(angle);
angle = angle / 2.0f; // x calculates degrees from 0 to 90
float my = cosf(angle);
Vec3 v0, v1, v, diff;
2019-11-23 20:27:39 +08:00
v0 = getOriginalVertex(Vec2(1.0f, 1.0f));
v1 = getOriginalVertex(Vec2());
2021-12-25 10:04:45 +08:00
float y0 = v0.y;
float y1 = v1.y;
2019-11-23 20:27:39 +08:00
float y;
2021-12-25 10:04:45 +08:00
Vec2 a, b, c, d;
2019-11-23 20:27:39 +08:00
if (y0 > y1)
{
// Normal Grid
a.setZero();
b.set(0.0f, 1.0f);
c.set(1.0f, 0.0f);
d.set(1.0f, 1.0f);
y = y0;
}
else
{
// Reversed Grid
b.setZero();
a.set(0.0f, 1.0f);
d.set(1.0f, 0.0f);
c.set(1.0f, 1.0f);
y = y1;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
diff.y = y - y * my;
diff.z = fabsf(floorf((y * mz) / 4.0f));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// bottom-left
2021-12-25 10:04:45 +08:00
v = getOriginalVertex(a);
2019-11-23 20:27:39 +08:00
v.y = diff.y;
v.z += diff.z;
setVertex(a, v);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// upper-left
v = getOriginalVertex(b);
v.y -= diff.y;
v.z -= diff.z;
setVertex(b, v);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// bottom-right
2021-12-25 10:04:45 +08:00
v = getOriginalVertex(c);
2019-11-23 20:27:39 +08:00
v.y = diff.y;
v.z += diff.z;
setVertex(c, v);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// upper-right
v = getOriginalVertex(d);
v.y -= diff.y;
v.z -= diff.z;
setVertex(d, v);
}
// implementation of Lens3D
2021-10-23 23:27:14 +08:00
Lens3D* Lens3D::create(float duration, const Vec2& gridSize, const Vec2& position, float radius)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Lens3D* action = new Lens3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, position, radius))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2019-11-23 20:27:39 +08:00
}
return action;
}
2021-10-23 23:27:14 +08:00
bool Lens3D::initWithDuration(float duration, const Vec2& gridSize, const Vec2& position, float radius)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
_position.set(-1.0f, -1.0f);
setPosition(position);
2021-12-25 10:04:45 +08:00
_radius = radius;
2019-11-23 20:27:39 +08:00
_lensEffect = 0.7f;
2021-12-25 10:04:45 +08:00
_concave = false;
_dirty = true;
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
Lens3D* Lens3D::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new Lens3D();
2019-11-23 20:27:39 +08:00
a->initWithDuration(_duration, _gridSize, _position, _radius);
a->autorelease();
return a;
}
void Lens3D::setPosition(const Vec2& pos)
{
2021-12-25 10:04:45 +08:00
if (!pos.equals(_position))
2019-11-23 20:27:39 +08:00
{
_position = pos;
2021-12-25 10:04:45 +08:00
_dirty = true;
2019-11-23 20:27:39 +08:00
}
}
void Lens3D::update(float /*time*/)
{
if (_dirty)
{
int i, j;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (i = 0; i < _gridSize.width + 1; ++i)
{
for (j = 0; j < _gridSize.height + 1; ++j)
{
Vec2 pos((float)i, (float)j);
2021-12-25 10:04:45 +08:00
Vec3 v = getOriginalVertex(pos);
2019-11-23 20:27:39 +08:00
Vec2 vect = _position - Vec2(v.x, v.y);
2021-12-25 10:04:45 +08:00
float r = vect.getLength();
2019-11-23 20:27:39 +08:00
if (r < _radius)
{
2021-12-25 10:04:45 +08:00
r = _radius - r;
2019-11-23 20:27:39 +08:00
float pre_log = r / _radius;
2021-12-25 10:04:45 +08:00
if (pre_log == 0)
2019-11-23 20:27:39 +08:00
{
pre_log = 0.001f;
}
2021-12-25 10:04:45 +08:00
float l = logf(pre_log) * _lensEffect;
float new_r = expf(l) * _radius;
2019-11-23 20:27:39 +08:00
if (vect.getLength() > 0)
{
vect.normalize();
Vec2 new_vect = vect * new_r;
v.z += (_concave ? -1.0f : 1.0f) * new_vect.getLength() * _lensEffect;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setVertex(pos, v);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_dirty = false;
}
}
// implementation of Ripple3D
2021-12-25 10:04:45 +08:00
Ripple3D* Ripple3D::create(float duration,
const Vec2& gridSize,
const Vec2& position,
float radius,
unsigned int waves,
float amplitude)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Ripple3D* action = new Ripple3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, position, radius, waves, amplitude))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2019-11-23 20:27:39 +08:00
}
return action;
}
2021-12-25 10:04:45 +08:00
bool Ripple3D::initWithDuration(float duration,
const Vec2& gridSize,
const Vec2& position,
float radius,
unsigned int waves,
float amplitude)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
setPosition(position);
2021-12-25 10:04:45 +08:00
_radius = radius;
_waves = waves;
_amplitude = amplitude;
2019-11-23 20:27:39 +08:00
_amplitudeRate = 1.0f;
return true;
}
return false;
}
void Ripple3D::setPosition(const Vec2& position)
{
_position = position;
}
Ripple3D* Ripple3D::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new Ripple3D();
2019-11-23 20:27:39 +08:00
a->initWithDuration(_duration, _gridSize, _position, _radius, _waves, _amplitude);
a->autorelease();
return a;
}
void Ripple3D::update(float time)
{
int i, j;
2021-12-25 10:04:45 +08:00
for (i = 0; i < (_gridSize.width + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (j = 0; j < (_gridSize.height + 1); ++j)
2019-11-23 20:27:39 +08:00
{
Vec2 pos((float)i, (float)j);
2021-12-25 10:04:45 +08:00
Vec3 v = getOriginalVertex(pos);
Vec2 vect = _position - Vec2(v.x, v.y);
float r = vect.getLength();
2019-11-23 20:27:39 +08:00
if (r < _radius)
{
2021-12-25 10:04:45 +08:00
r = _radius - r;
2019-11-23 20:27:39 +08:00
float rate = powf(r / _radius, 2);
2021-12-25 10:04:45 +08:00
v.z += (sinf(time * (float)M_PI * _waves * 2 + r * 0.1f) * _amplitude * _amplitudeRate * rate);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setVertex(pos, v);
}
}
}
// implementation of Shaky3D
2021-10-23 23:27:14 +08:00
Shaky3D* Shaky3D::create(float duration, const Vec2& gridSize, int range, bool shakeZ)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Shaky3D* action = new Shaky3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, range, shakeZ))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2019-11-23 20:27:39 +08:00
}
return action;
}
2021-10-23 23:27:14 +08:00
bool Shaky3D::initWithDuration(float duration, const Vec2& gridSize, int range, bool shakeZ)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
_randrange = range;
2021-12-25 10:04:45 +08:00
_shakeZ = shakeZ;
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
Shaky3D* Shaky3D::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new Shaky3D();
2019-11-23 20:27:39 +08:00
a->initWithDuration(_duration, _gridSize, _randrange, _shakeZ);
a->autorelease();
return a;
}
void Shaky3D::update(float /*time*/)
{
int i, j;
2021-12-25 10:04:45 +08:00
for (i = 0; i < (_gridSize.width + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (j = 0; j < (_gridSize.height + 1); ++j)
2019-11-23 20:27:39 +08:00
{
Vec2 pos((float)i, (float)j);
Vec3 v = getOriginalVertex(pos);
2021-12-25 10:04:45 +08:00
v.x += (rand() % (_randrange * 2)) - _randrange;
v.y += (rand() % (_randrange * 2)) - _randrange;
2019-11-23 20:27:39 +08:00
if (_shakeZ)
{
2021-12-25 10:04:45 +08:00
v.z += (rand() % (_randrange * 2)) - _randrange;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
setVertex(pos, v);
}
}
}
// implementation of Liquid
2021-10-23 23:27:14 +08:00
Liquid* Liquid::create(float duration, const Vec2& gridSize, unsigned int waves, float amplitude)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Liquid* action = new Liquid();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, waves, amplitude))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
2019-11-23 20:27:39 +08:00
}
2021-12-08 00:11:53 +08:00
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2021-12-08 00:11:53 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return action;
}
2021-10-23 23:27:14 +08:00
bool Liquid::initWithDuration(float duration, const Vec2& gridSize, unsigned int waves, float amplitude)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
2021-12-25 10:04:45 +08:00
_waves = waves;
_amplitude = amplitude;
2019-11-23 20:27:39 +08:00
_amplitudeRate = 1.0f;
return true;
}
return false;
}
Liquid* Liquid::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new Liquid();
2019-11-23 20:27:39 +08:00
a->initWithDuration(_duration, _gridSize, _waves, _amplitude);
a->autorelease();
return a;
}
void Liquid::update(float time)
{
int i, j;
for (i = 1; i < _gridSize.width; ++i)
{
for (j = 1; j < _gridSize.height; ++j)
{
Vec2 pos((float)i, (float)j);
Vec3 v = getOriginalVertex(pos);
2021-12-25 10:04:45 +08:00
v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
v.y = (v.y + (sinf(time * (float)M_PI * _waves * 2 + v.y * .01f) * _amplitude * _amplitudeRate));
2019-11-23 20:27:39 +08:00
setVertex(pos, v);
}
}
}
// implementation of Waves
2021-12-25 10:04:45 +08:00
Waves* Waves::create(float duration,
const Vec2& gridSize,
unsigned int waves,
float amplitude,
bool horizontal,
bool vertical)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Waves* action = new Waves();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2019-11-23 20:27:39 +08:00
}
return action;
}
2021-12-25 10:04:45 +08:00
bool Waves::initWithDuration(float duration,
const Vec2& gridSize,
unsigned int waves,
float amplitude,
bool horizontal,
bool vertical)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
2021-12-25 10:04:45 +08:00
_waves = waves;
_amplitude = amplitude;
2019-11-23 20:27:39 +08:00
_amplitudeRate = 1.0f;
2021-12-25 10:04:45 +08:00
_horizontal = horizontal;
_vertical = vertical;
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
Waves* Waves::clone() const
{
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new Waves();
2019-11-23 20:27:39 +08:00
a->initWithDuration(_duration, _gridSize, _waves, _amplitude, _horizontal, _vertical);
a->autorelease();
return a;
}
void Waves::update(float time)
{
int i, j;
for (i = 0; i < _gridSize.width + 1; ++i)
{
for (j = 0; j < _gridSize.height + 1; ++j)
{
Vec2 pos((float)i, (float)j);
Vec3 v = getOriginalVertex(pos);
if (_vertical)
{
v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.y * .01f) * _amplitude * _amplitudeRate));
}
if (_horizontal)
{
v.y = (v.y + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
}
setVertex(pos, v);
}
}
}
// implementation of Twirl
2021-10-23 23:27:14 +08:00
Twirl* Twirl::create(float duration, const Vec2& gridSize, const Vec2& position, unsigned int twirls, float amplitude)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Twirl* action = new Twirl();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (action->initWithDuration(duration, gridSize, position, twirls, amplitude))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
action->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(action);
2019-11-23 20:27:39 +08:00
}
return action;
}
2021-12-25 10:04:45 +08:00
bool Twirl::initWithDuration(float duration,
const Vec2& gridSize,
const Vec2& position,
unsigned int twirls,
float amplitude)
2019-11-23 20:27:39 +08:00
{
if (Grid3DAction::initWithDuration(duration, gridSize))
{
setPosition(position);
2021-12-25 10:04:45 +08:00
_twirls = twirls;
_amplitude = amplitude;
2019-11-23 20:27:39 +08:00
_amplitudeRate = 1.0f;
return true;
}
return false;
}
void Twirl::setPosition(const Vec2& position)
{
_position = position;
}
2021-12-25 10:04:45 +08:00
Twirl* Twirl::clone() const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// no copy constructor
2021-12-08 00:11:53 +08:00
auto a = new Twirl();
2019-11-23 20:27:39 +08:00
a->initWithDuration(_duration, _gridSize, _position, _twirls, _amplitude);
a->autorelease();
return a;
}
void Twirl::update(float time)
{
int i, j;
2021-12-25 10:04:45 +08:00
Vec2 c = _position;
for (i = 0; i < (_gridSize.width + 1); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
for (j = 0; j < (_gridSize.height + 1); ++j)
2019-11-23 20:27:39 +08:00
{
Vec2 pos((float)i, (float)j);
Vec3 v = getOriginalVertex(pos);
2021-12-25 10:04:45 +08:00
Vec2 avg(i - (_gridSize.width / 2.0f), j - (_gridSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
float r = avg.getLength();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float amp = 0.1f * _amplitude * _amplitudeRate;
2021-12-25 10:04:45 +08:00
float a = r * cosf((float)M_PI / 2.0f + time * (float)M_PI * _twirls * 2) * amp;
Vec2 d(sinf(a) * (v.y - c.y) + cosf(a) * (v.x - c.x), cosf(a) * (v.y - c.y) - sinf(a) * (v.x - c.x));
2019-11-23 20:27:39 +08:00
v.x = c.x + d.x;
v.y = c.y + d.y;
setVertex(pos, v);
}
}
}
NS_AX_END