axmol/extensions/Particle3D/PU/CCPUCircleEmitter.cpp

275 lines
9.0 KiB
C++
Raw Normal View History

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.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2022-02-24 18:51:36 +08:00
https://adxeproject.github.io/
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +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:
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
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;
2021-12-25 10:04:45 +08:00
const float PUCircleEmitter::DEFAULT_STEP = 0.1f;
const float PUCircleEmitter::DEFAULT_ANGLE = 0.0f;
const bool PUCircleEmitter::DEFAULT_RANDOM = true;
2015-03-05 13:24:06 +08:00
const Vec3 PUCircleEmitter::DEFAULT_NORMAL(0, 0, 0);
2015-02-11 18:14:22 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUCircleEmitter::PUCircleEmitter()
: PUEmitter()
, _radius(DEFAULT_RADIUS)
, _circleAngle(DEFAULT_ANGLE)
, _originalCircleAngle(DEFAULT_ANGLE)
, _step(DEFAULT_STEP)
, _x(0.0f)
, _z(0.0f)
, _random(DEFAULT_RANDOM)
, _orientation()
, _normal(DEFAULT_NORMAL)
{}
2015-02-11 18:14:22 +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;
}
//-----------------------------------------------------------------------
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;
2021-12-25 10:04:45 +08:00
_circleAngle = circleAngle;
2015-02-11 18:14:22 +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;
}
//-----------------------------------------------------------------------
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;
}
//-----------------------------------------------------------------------
const Quaternion& PUCircleEmitter::getOrientation() const
2015-02-11 18:14:22 +08:00
{
return _orientation;
}
2021-12-25 10:04:45 +08:00
//-----------------------------------------------------------------------
const Vec3& PUCircleEmitter::getNormal() const
2021-12-25 10:04:45 +08:00
{
2015-02-11 18:14:22 +08:00
return _normal;
2021-12-25 10:04:45 +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);
2021-12-25 10:04:45 +08:00
_normal = normal;
2015-02-11 18:14:22 +08:00
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUCircleEmitter::notifyStart()
2015-02-11 18:14:22 +08:00
{
// Reset the attributes to allow a restart.
_circleAngle = _originalCircleAngle;
}
2021-12-25 10:04:45 +08:00
//-----------------------------------------------------------------------
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;
2021-12-25 10:04:45 +08:00
angle = _circleAngle;
2015-02-11 18:14:22 +08:00
}
_x = cosf(angle);
_z = sinf(angle);
2021-12-25 10:04:45 +08:00
// ParticleSystem* sys = mParentTechnique->getParentSystem();
// if (sys)
2015-02-11 18:14:22 +08:00
{
// Take both orientation of the node and its own orientation, based on the normal, into account
Mat4 rotMat;
2021-12-25 10:04:45 +08:00
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));
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
// else
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// particle->position = getDerivedPosition() + _emitterScale * ( mOrientation * Vec3(mX * mRadius, 0, mZ * mRadius)
//);
// }
2015-02-11 18:14:22 +08:00
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)
{
2021-12-25 10:04:45 +08:00
// particle->direction = (mOrientation * Vec3(mX, 0, mZ) ).randomDeviant(angle, mUpVector);
2015-02-11 18:14:22 +08:00
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
{
2021-12-08 00:11:53 +08:00
auto pe = new PUCircleEmitter();
2015-02-11 18:14:22 +08:00
pe->autorelease();
return pe;
}
2021-12-25 10:04:45 +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);
2021-12-25 10:04:45 +08:00
// q.FromAngleAxis(Radian(Math::PI), fallbackAxis);
2015-02-11 18:14:22 +08:00
}
else
{
// Generate an axis
2021-12-25 10:04:45 +08:00
Vec3 axis /* = Vec3::UNIT_X.crossProduct(*this)*/;
2015-02-11 18:14:22 +08:00
Vec3::cross(Vec3::UNIT_X, src, &axis);
2021-12-25 10:04:45 +08:00
if (axis.lengthSquared() < (1e-06 * 1e-06)) // pick another if colinear
// axis = Vec3::UNIT_Y.crossProduct(*this);
2015-02-11 18:14:22 +08:00
Vec3::cross(Vec3::UNIT_Y, src, &axis);
axis.normalize();
2021-12-25 10:04:45 +08:00
// q.FromAngleAxis(Radian(Math::PI), axis);
2015-02-11 18:14:22 +08:00
q.set(axis, (float)M_PI);
}
}
else
{
/*float s = Math::Sqrt( (1+d)*2 );*/
2021-12-25 10:04:45 +08:00
float s = sqrtf((1 + d) * 2);
2015-02-11 18:14:22 +08:00
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;
}
2021-12-25 10:04:45 +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
2021-12-25 10:04:45 +08:00
PUCircleEmitter* circleEmitter = static_cast<PUCircleEmitter*>(emitter);
circleEmitter->_radius = _radius;
circleEmitter->_circleAngle = _circleAngle;
2015-03-02 13:07:32 +08:00
circleEmitter->_originalCircleAngle = _originalCircleAngle;
2021-12-25 10:04:45 +08:00
circleEmitter->_step = _step;
circleEmitter->_random = _random;
circleEmitter->_normal = _normal;
circleEmitter->_orientation = _orientation;
2015-03-02 13:07:32 +08:00
}
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