2015-02-11 18:14:22 +08:00
|
|
|
/****************************************************************************
|
2015-03-02 16:05:26 +08:00
|
|
|
Copyright (C) 2013 Henry van Merode. All rights reserved.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2015-02-11 18:14:22 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
#include "CCPUCircleEmitter.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUUtil.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
#include "base/ccRandom.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// Constants
|
2015-03-05 13:24:06 +08:00
|
|
|
const float PUCircleEmitter::DEFAULT_RADIUS = 100.0f;
|
|
|
|
const float PUCircleEmitter::DEFAULT_STEP = 0.1f;
|
|
|
|
const float PUCircleEmitter::DEFAULT_ANGLE = 0.0f;
|
|
|
|
const bool PUCircleEmitter::DEFAULT_RANDOM = true;
|
|
|
|
const Vec3 PUCircleEmitter::DEFAULT_NORMAL(0, 0, 0);
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
PUCircleEmitter::PUCircleEmitter(void) :
|
|
|
|
PUEmitter(),
|
2015-02-11 18:14:22 +08:00
|
|
|
_radius(DEFAULT_RADIUS),
|
|
|
|
_circleAngle(DEFAULT_ANGLE),
|
|
|
|
_originalCircleAngle(DEFAULT_ANGLE),
|
2015-03-13 06:47:58 +08:00
|
|
|
_step(DEFAULT_STEP),
|
|
|
|
_x(0.0f),
|
|
|
|
_z(0.0f),
|
2015-02-11 18:14:22 +08:00
|
|
|
_random(DEFAULT_RANDOM),
|
|
|
|
_orientation(),
|
2015-03-13 06:47:58 +08:00
|
|
|
_normal(DEFAULT_NORMAL)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2016-03-04 12:48:28 +08:00
|
|
|
float PUCircleEmitter::getRadius() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _radius;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::setRadius(const float radius)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_radius = radius;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2016-03-04 12:48:28 +08:00
|
|
|
float PUCircleEmitter::getCircleAngle() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _originalCircleAngle;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::setCircleAngle(const float circleAngle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_originalCircleAngle = circleAngle;
|
|
|
|
_circleAngle = circleAngle;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2016-03-04 12:48:28 +08:00
|
|
|
float PUCircleEmitter::getStep() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _step;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::setStep(const float step)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_step = step;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2016-03-04 12:48:28 +08:00
|
|
|
bool PUCircleEmitter::isRandom() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _random;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::setRandom(const bool random)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_random = random;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
const Quaternion& PUCircleEmitter::getOrientation(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _orientation;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
const Vec3& PUCircleEmitter::getNormal(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _normal;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::setNormal(const Vec3& normal)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
//_orientation = Vec3::UNIT_Y.getRotationTo(normal, Vec3::UNIT_X);
|
|
|
|
_orientation = getRotationTo(Vec3::UNIT_Y, normal, Vec3::UNIT_X);
|
|
|
|
_normal = normal;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::notifyStart (void)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Reset the attributes to allow a restart.
|
|
|
|
_circleAngle = _originalCircleAngle;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::initParticlePosition(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
float angle = 0;
|
|
|
|
if (_random)
|
|
|
|
{
|
|
|
|
// Choose a random position on the circle.
|
|
|
|
angle = cocos2d::random(0.0, M_PI * 2.0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Follow the contour of the circle.
|
|
|
|
_circleAngle += _step;
|
|
|
|
_circleAngle = _circleAngle > M_PI * 2.0f ? _circleAngle - (M_PI * 2.0) : _circleAngle;
|
|
|
|
angle = _circleAngle;
|
|
|
|
}
|
|
|
|
|
|
|
|
_x = cosf(angle);
|
|
|
|
_z = sinf(angle);
|
|
|
|
//ParticleSystem* sys = mParentTechnique->getParentSystem();
|
|
|
|
//if (sys)
|
|
|
|
{
|
|
|
|
// Take both orientation of the node and its own orientation, based on the normal, into account
|
|
|
|
Mat4 rotMat;
|
|
|
|
Mat4::createRotation(static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedOrientation() * _orientation, &rotMat);
|
|
|
|
particle->position = getDerivedPosition() +
|
|
|
|
/*sys->getDerivedOrientation() * */rotMat * (Vec3(_x * _radius * _emitterScale.x, 0, _z * _radius * _emitterScale.z));
|
|
|
|
}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// particle->position = getDerivedPosition() + _emitterScale * ( mOrientation * Vec3(mX * mRadius, 0, mZ * mRadius) );
|
|
|
|
//}
|
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::initParticleDirection(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_autoDirection)
|
|
|
|
{
|
|
|
|
// The value of the direction vector that has been set does not have a meaning for
|
|
|
|
// the circle emitter.
|
|
|
|
float angle = 0.0f;
|
|
|
|
generateAngle(angle);
|
|
|
|
if (angle != 0.0f)
|
|
|
|
{
|
|
|
|
//particle->direction = (mOrientation * Vec3(mX, 0, mZ) ).randomDeviant(angle, mUpVector);
|
|
|
|
Mat4 mat;
|
|
|
|
Mat4::createRotation(_orientation, &mat);
|
|
|
|
Vec3 temp = mat * Vec3(_x, 0, _z);
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
particle->direction = PUUtil::randomDeviant(temp, angle, _upVector);
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Mat4 rotMat;
|
|
|
|
Mat4::createRotation(_orientation, &rotMat);
|
|
|
|
particle->direction = rotMat * Vec3(_x, 0, _z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use the standard way
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::initParticleDirection(particle);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUCircleEmitter* PUCircleEmitter::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
auto pe = new (std::nothrow) PUCircleEmitter();
|
2015-02-11 18:14:22 +08:00
|
|
|
pe->autorelease();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
cocos2d::Quaternion PUCircleEmitter::getRotationTo( const Vec3 &src, const Vec3& dest, const Vec3& fallbackAxis /*= Vec3::ZERO*/ ) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Based on Stan Melax's article in Game Programming Gems
|
|
|
|
Quaternion q;
|
|
|
|
// Copy, since cannot modify local
|
|
|
|
Vec3 v0 = src;
|
|
|
|
Vec3 v1 = dest;
|
|
|
|
v0.normalize();
|
|
|
|
v1.normalize();
|
|
|
|
|
|
|
|
float d = v0.dot(v1);
|
|
|
|
// If dot == 1, vectors are the same
|
|
|
|
if (d >= 1.0f)
|
|
|
|
{
|
|
|
|
return Quaternion();
|
|
|
|
}
|
|
|
|
if (d < (1e-6f - 1.0f))
|
|
|
|
{
|
|
|
|
if (fallbackAxis != Vec3::ZERO)
|
|
|
|
{
|
|
|
|
// rotate 180 degrees about the fallback axis
|
|
|
|
q.set(fallbackAxis, (float)M_PI);
|
|
|
|
//q.FromAngleAxis(Radian(Math::PI), fallbackAxis);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Generate an axis
|
|
|
|
Vec3 axis/* = Vec3::UNIT_X.crossProduct(*this)*/;
|
|
|
|
Vec3::cross(Vec3::UNIT_X, src, &axis);
|
|
|
|
if (axis.lengthSquared() < (1e-06 * 1e-06)) // pick another if colinear
|
|
|
|
//axis = Vec3::UNIT_Y.crossProduct(*this);
|
|
|
|
Vec3::cross(Vec3::UNIT_Y, src, &axis);
|
|
|
|
axis.normalize();
|
|
|
|
|
|
|
|
//q.FromAngleAxis(Radian(Math::PI), axis);
|
|
|
|
q.set(axis, (float)M_PI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*float s = Math::Sqrt( (1+d)*2 );*/
|
|
|
|
float s = sqrtf( (1+d)*2 );
|
|
|
|
float invs = 1 / s;
|
|
|
|
|
|
|
|
Vec3 c /*= v0.crossProduct(v1)*/;
|
|
|
|
Vec3::cross(v0, v1, &c);
|
|
|
|
|
|
|
|
q.x = c.x * invs;
|
|
|
|
q.y = c.y * invs;
|
|
|
|
q.z = c.z * invs;
|
|
|
|
q.w = s * 0.5f;
|
|
|
|
q.normalize();
|
|
|
|
}
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUCircleEmitter::copyAttributesTo( PUEmitter* emitter )
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::copyAttributesTo(emitter);
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUCircleEmitter* circleEmitter = static_cast<PUCircleEmitter*>(emitter);
|
2015-03-02 13:07:32 +08:00
|
|
|
circleEmitter->_radius = _radius;
|
|
|
|
circleEmitter->_circleAngle = _circleAngle;
|
|
|
|
circleEmitter->_originalCircleAngle = _originalCircleAngle;
|
|
|
|
circleEmitter->_step = _step;
|
|
|
|
circleEmitter->_random = _random;
|
|
|
|
circleEmitter->_normal = _normal;
|
|
|
|
circleEmitter->_orientation = _orientation;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUCircleEmitter* PUCircleEmitter::clone()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
auto be = PUCircleEmitter::create();
|
2015-03-02 13:07:32 +08:00
|
|
|
copyAttributesTo(be);
|
|
|
|
return be;
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
NS_CC_END
|