axmol/extensions/Particle3D/PU/CCPULineAffector.cpp

180 lines
6.3 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 Chukong Technologies Inc.
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 "CCPULineAffector.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
2015-02-11 18:14:22 +08:00
NS_CC_BEGIN
// Constants
2015-03-05 13:24:06 +08:00
const float PULineAffector::DEFAULT_MAX_DEVIATION = 1.0f;
const float PULineAffector::DEFAULT_TIME_STEP = 0.1f;
const Vec3 PULineAffector::DEFAULT_END(0, 0, 0);
const float PULineAffector::DEFAULT_DRIFT = 0.0f;
2015-02-11 18:14:22 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
PULineAffector::PULineAffector(void) :
PUAffector(),
2015-02-11 18:14:22 +08:00
_maxDeviation(DEFAULT_MAX_DEVIATION),
_scaledMaxDeviation(1.0f),
_end(DEFAULT_END),
_timeSinceLastUpdate(0.0f),
_timeStep(DEFAULT_TIME_STEP),
_drift(DEFAULT_DRIFT),
_oneMinusDrift(1.0f),
_update(true),
_first(true)
2015-02-11 18:14:22 +08:00
{
}
2015-03-05 13:24:06 +08:00
PULineAffector::~PULineAffector( void )
2015-02-11 18:14:22 +08:00
{
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
float PULineAffector::getMaxDeviation(void) const
2015-02-11 18:14:22 +08:00
{
return _maxDeviation;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::setMaxDeviation(float maxDeviation)
2015-02-11 18:14:22 +08:00
{
_maxDeviation = maxDeviation;
_scaledMaxDeviation = _maxDeviation * _affectorScale.length();
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
const Vec3& PULineAffector::getEnd(void) const
2015-02-11 18:14:22 +08:00
{
return _end;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::setEnd(const Vec3& end)
2015-02-11 18:14:22 +08:00
{
_end = end;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
float PULineAffector::getTimeStep(void) const
2015-02-11 18:14:22 +08:00
{
return _timeStep;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::setTimeStep(float timeStep)
2015-02-11 18:14:22 +08:00
{
_timeStep = timeStep;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
float PULineAffector::getDrift(void) const
2015-02-11 18:14:22 +08:00
{
return _drift;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::setDrift(float drift)
2015-02-11 18:14:22 +08:00
{
_drift = drift;
_oneMinusDrift = 1.0f - drift;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::notifyRescaled(const Vec3& scale)
2015-02-11 18:14:22 +08:00
{
_scaledMaxDeviation = _maxDeviation * scale.length();
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::preUpdateAffector(float deltaTime)
2015-02-11 18:14:22 +08:00
{
if (/*technique->getNumberOfEmittedParticles()*/static_cast<PUParticleSystem3D *>(_particleSystem)->getAliveParticleCount() > 0)
2015-02-11 18:14:22 +08:00
{
_timeSinceLastUpdate += deltaTime;
while (_timeSinceLastUpdate > _timeStep)
{
_timeSinceLastUpdate -= _timeStep;
_update = true;
}
}
(static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(_end); // Always update
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
2015-02-11 18:14:22 +08:00
{
//_first = true;
//for (auto iter : _particleSystem->getParticles())
{
//PUParticle3D *particle = iter;
(static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(particle->originalPosition); // Always update
if (_update && CCRANDOM_0_1() > 0.5 && !_first)
{
// Generate a random vector perpendicular on the line
Vec3 perpendicular;
Vec3::cross(_end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
perpendicular.normalize();
// Determine a random point near the line.
Vec3 targetPosition = particle->originalPosition + _scaledMaxDeviation * CCRANDOM_0_1() * perpendicular;
/** Set the new position.
@remarks
This affector already takes rotational offset of the particle system into account. This means that there is no need
to set the particle system to keep_local to 'true'. The reason is that this is a specialized affector that calculates
a new particle position instead of a direction.
*/
particle->position = _drift * targetPosition + _oneMinusDrift * particle->position;
(static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(particle->position);
}
_first = false;
}
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PULineAffector::postUpdateAffector(float deltaTime)
2015-02-11 18:14:22 +08:00
{
_update = false;
}
2015-03-05 13:24:06 +08:00
void PULineAffector::firstParticleUpdate( PUParticle3D *particle, float deltaTime )
2015-02-11 18:14:22 +08:00
{
_first = true;
}
2015-03-05 13:24:06 +08:00
PULineAffector* PULineAffector::create()
2015-02-11 18:14:22 +08:00
{
2015-03-05 13:24:06 +08:00
auto pla = new (std::nothrow) PULineAffector();
2015-02-11 18:14:22 +08:00
pla->autorelease();
return pla;
}
2015-03-05 13:24:06 +08:00
void PULineAffector::copyAttributesTo( PUAffector* affector )
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
PUAffector::copyAttributesTo(affector);
2015-03-02 13:07:32 +08:00
2015-03-05 13:24:06 +08:00
PULineAffector* lineAffector = static_cast<PULineAffector*>(affector);
2015-03-02 13:07:32 +08:00
lineAffector->setMaxDeviation(_maxDeviation);
lineAffector->_end = _end;
lineAffector->_timeStep = _timeStep;
lineAffector->_drift = _drift;
lineAffector->_oneMinusDrift = _oneMinusDrift;
}
2015-03-03 15:13:47 +08:00
NS_CC_END