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.
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2015-2017 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 "CCPUForceFieldAffector.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 PUForceField::ForceFieldType PUForceFieldAffector::DEFAULT_FORCEFIELD_TYPE = PUForceField::FF_REALTIME_CALC;
|
|
|
|
const float PUForceFieldAffector::DEFAULT_DELTA = 1.0f;
|
|
|
|
const float PUForceFieldAffector::DEFAULT_FORCE = 400.0f;
|
|
|
|
const unsigned short PUForceFieldAffector::DEFAULT_OCTAVES = 2;
|
|
|
|
const double PUForceFieldAffector::DEFAULT_FREQUENCY = 1.0f;
|
|
|
|
const double PUForceFieldAffector::DEFAULT_AMPLITUDE = 1.0f;
|
|
|
|
const double PUForceFieldAffector::DEFAULT_PERSISTENCE = 3.0f;
|
|
|
|
const unsigned int PUForceFieldAffector::DEFAULT_FORCEFIELDSIZE = 64;
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
const Vec3 PUForceFieldAffector::DEFAULT_WORLDSIZE(500.0f, 500.0f, 500.0f);
|
|
|
|
const Vec3 PUForceFieldAffector::DEFAULT_MOVEMENT(500.0f, 0.0f, 0.0f);
|
2015-03-05 13:24:06 +08:00
|
|
|
const float PUForceFieldAffector::DEFAULT_MOVEMENT_FREQUENCY = 5.0f;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
PUForceFieldAffector::PUForceFieldAffector() :
|
2015-02-11 18:14:22 +08:00
|
|
|
_forceFieldType(PUForceField::FF_REALTIME_CALC),
|
|
|
|
_delta(DEFAULT_DELTA),
|
|
|
|
_scaleForce(DEFAULT_FORCE),
|
|
|
|
_octaves(DEFAULT_OCTAVES),
|
|
|
|
_frequency(DEFAULT_FREQUENCY),
|
|
|
|
_amplitude(DEFAULT_AMPLITUDE),
|
|
|
|
_persistence(DEFAULT_PERSISTENCE),
|
|
|
|
_forceFieldSize(DEFAULT_FORCEFIELDSIZE),
|
|
|
|
_worldSize(DEFAULT_WORLDSIZE),
|
|
|
|
_prepared(true),
|
|
|
|
_ignoreNegativeX(false),
|
|
|
|
_ignoreNegativeY(false),
|
|
|
|
_ignoreNegativeZ(false),
|
|
|
|
_movement(DEFAULT_MOVEMENT),
|
|
|
|
_movementSet(false),
|
|
|
|
_movementFrequency(DEFAULT_MOVEMENT_FREQUENCY),
|
|
|
|
_movementFrequencyCount(0.0f),
|
|
|
|
_suppressGeneration(false)
|
|
|
|
{
|
|
|
|
};
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
PUForceFieldAffector::~PUForceFieldAffector()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
};
|
|
|
|
//-----------------------------------------------------------------------
|
2016-03-04 12:48:28 +08:00
|
|
|
PUForceField::ForceFieldType PUForceFieldAffector::getForceFieldType() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _forceFieldType;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setForceFieldType(const PUForceField::ForceFieldType forceFieldType)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_forceFieldType = forceFieldType;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setForceFieldType(forceFieldType);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PUForceFieldAffector::getDelta(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _delta;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setDelta(float delta)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_delta = delta;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PUForceFieldAffector::getScaleForce(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _scaleForce;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setScaleForce(float scaleForce)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_scaleForce = scaleForce;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
unsigned short PUForceFieldAffector::getOctaves(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _octaves;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setOctaves(unsigned short octaves)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_octaves = octaves;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setOctaves(octaves);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
double PUForceFieldAffector::getFrequency(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _frequency;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setFrequency(double frequency)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_frequency = frequency;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setFrequency(frequency);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
double PUForceFieldAffector::getAmplitude(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _amplitude;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setAmplitude(double amplitude)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_amplitude = amplitude;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setAmplitude(amplitude);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
double PUForceFieldAffector::getPersistence(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _persistence;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setPersistence(double persistence)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_persistence = persistence;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setPersistence(persistence);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
unsigned int PUForceFieldAffector::getForceFieldSize(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _forceFieldSize;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setForceFieldSize(unsigned int forceFieldSize)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_forceFieldSize = forceFieldSize;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setForceFieldSize(forceFieldSize);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
Vec3 PUForceFieldAffector::getWorldSize(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _worldSize;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setWorldSize(const Vec3& worldSize)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_worldSize = worldSize;
|
|
|
|
if (_suppressGeneration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_forceField.setWorldSize(worldSize);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
bool PUForceFieldAffector::getIgnoreNegativeX(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _ignoreNegativeX;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setIgnoreNegativeX(bool ignoreNegativeX)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_ignoreNegativeX = ignoreNegativeX;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
bool PUForceFieldAffector::getIgnoreNegativeY(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _ignoreNegativeY;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setIgnoreNegativeY(bool ignoreNegativeY)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_ignoreNegativeY = ignoreNegativeY;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
bool PUForceFieldAffector::getIgnoreNegativeZ(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _ignoreNegativeZ;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setIgnoreNegativeZ(bool ignoreNegativeZ)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_ignoreNegativeZ = ignoreNegativeZ;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PUForceFieldAffector::getMovementFrequency(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _movementFrequency;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setMovementFrequency(float movementFrequency)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_movementFrequency = movementFrequency;
|
|
|
|
_movementSet = (movementFrequency > 0.0f);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
const Vec3& PUForceFieldAffector::getMovement(void) const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _movement;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::setMovement(const Vec3& movement)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_movement = movement;
|
|
|
|
_movementSet = (movement != Vec3::ZERO);
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::suppressGeneration(bool suppress)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_suppressGeneration = suppress;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
//for (auto iter : _particleSystem->getParticles())
|
|
|
|
{
|
|
|
|
//PUParticle3D *particle = iter;
|
|
|
|
_forceField.determineForce(particle->position, _force, _delta);
|
|
|
|
// If negative values are ignored, set the force to 0.
|
|
|
|
if (_ignoreNegativeX)
|
|
|
|
{
|
|
|
|
_force.x = 0.0f;
|
|
|
|
}
|
|
|
|
if (_ignoreNegativeY)
|
|
|
|
{
|
|
|
|
_force.y = 0.0f;
|
|
|
|
}
|
|
|
|
if (_ignoreNegativeZ)
|
|
|
|
{
|
|
|
|
_force.z = 0.0f;
|
|
|
|
}
|
|
|
|
particle->direction += deltaTime * _scaleForce * _force;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::notifyStart()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_movementFrequencyCount = 0.0f;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::preUpdateAffector( float deltaTime )
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_movementSet)
|
|
|
|
{
|
|
|
|
if (deltaTime > _movementFrequency)
|
|
|
|
{
|
|
|
|
// Ignore too large times, because it just blows things up
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_movementFrequencyCount += deltaTime;
|
|
|
|
if (_movementFrequencyCount > _movementFrequency)
|
|
|
|
{
|
|
|
|
_movementFrequencyCount -= _movementFrequency;
|
|
|
|
}
|
|
|
|
|
|
|
|
_displacement = sin(2.0f * M_PI * _movementFrequencyCount/_movementFrequency) * _movement;
|
|
|
|
_forceField.setForceFieldPositionBase(_basePosition + _displacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::prepare()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
//if (particleTechnique->getParentSystem())
|
|
|
|
{
|
|
|
|
// Forcefield position is same position as particle system position
|
|
|
|
_forceField.initialise(_forceFieldType,
|
|
|
|
// _particleSystem->getDerivedPosition()
|
|
|
|
getDerivedPosition(),
|
|
|
|
_forceFieldSize,
|
|
|
|
_octaves,
|
|
|
|
_frequency,
|
|
|
|
_amplitude,
|
|
|
|
_persistence,
|
|
|
|
_worldSize);
|
|
|
|
|
|
|
|
_basePosition = _forceField.getForceFieldPositionBase();
|
|
|
|
_prepared = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUForceFieldAffector* PUForceFieldAffector::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
auto pffa = new (std::nothrow) PUForceFieldAffector();
|
2015-02-11 18:14:22 +08:00
|
|
|
pffa->autorelease();
|
|
|
|
return pffa;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUForceFieldAffector::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
|
|
|
PUForceFieldAffector* forceFieldAffector = static_cast<PUForceFieldAffector*>(affector);
|
2015-03-02 13:07:32 +08:00
|
|
|
forceFieldAffector->_forceFieldType = _forceFieldType;
|
|
|
|
forceFieldAffector->_delta = _delta;
|
|
|
|
forceFieldAffector->_scaleForce = _scaleForce;
|
|
|
|
forceFieldAffector->_octaves = _octaves;
|
|
|
|
forceFieldAffector->_frequency = _frequency;
|
|
|
|
forceFieldAffector->_amplitude = _amplitude;
|
|
|
|
forceFieldAffector->_persistence = _persistence;
|
|
|
|
forceFieldAffector->_forceFieldSize = _forceFieldSize;
|
|
|
|
forceFieldAffector->_worldSize = _worldSize;
|
|
|
|
forceFieldAffector->_ignoreNegativeX = _ignoreNegativeX;
|
|
|
|
forceFieldAffector->_ignoreNegativeY = _ignoreNegativeY;
|
|
|
|
forceFieldAffector->_ignoreNegativeZ = _ignoreNegativeZ;
|
|
|
|
forceFieldAffector->_movementSet = _movementSet;
|
|
|
|
forceFieldAffector->_movementFrequency = _movementFrequency;
|
|
|
|
forceFieldAffector->_movement = _movement;
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:13:47 +08:00
|
|
|
NS_CC_END
|