particle affectors

This commit is contained in:
Henrique Santiago 2011-12-15 23:59:29 -02:00
parent 435161df62
commit ad38dad227
11 changed files with 176 additions and 100 deletions

View File

@ -1,92 +1,22 @@
ParticleSystem ParticleSystem
// continuous fire, yellow GoToPointAffector
destination: 150 150
Emitter Emitter
position: 100 100 position: 100 100
duration: 8 burstRate: 2
delay: 0.1 burstCount: 1
burstRate: 0.03
burstCount: 5
particle-min-duration: 0.6 particle-duration: 1
particle-max-duration: 0.8
particle-position-radius: 0 particle-position-radius: 0
particle-velocity: 96 particle-velocity: 32
particle-min-velocity-angle: 310 particle-velocity-angle: 0
particle-max-velocity-angle: 340
particle-acceleration: 0 particle-acceleration: 0
particle-size: 24 24 particle-size: 24 24
particle-color: #ffff0044 particle-color: #ffff0044
particle-texture: circle2.png particle-texture: circle2.png
// fire ball, up side, orange
Emitter
position: 100 100
duration: 0.5
delay: 0.1
burstRate: 0.01
burstCount: 6
particle-min-duration: 0.1
particle-max-duration: 0.3
particle-position-radius: 0
particle-velocity: 128
particle-min-velocity-angle: 360
particle-max-velocity-angle: 380
particle-acceleration: 512
particle-acceleration-angle: 270
particle-size: 24 24
particle-color: #ffa50044
particle-texture: circle2.png
// fire ball, down side, orange
Emitter
position: 100 100
duration: 0.5
delay: 0.1
burstRate: 0.01
burstCount: 6
particle-min-duration: 0.1
particle-max-duration: 0.3
particle-position-radius: 0
particle-velocity: 128
particle-min-velocity-angle: 270
particle-max-velocity-angle: 290
particle-acceleration: 512
particle-acceleration-angle: 45
particle-size: 24 24
particle-color: #ffa50044
particle-texture: circle2.png
// smoke
Emitter
position: 100 100
duration: 9
burstRate: 0.03
burstCount: 1
particle-min-duration: 0.6
particle-max-duration: 1
particle-position-radius: 0
particle-velocity: 64
particle-min-velocity-angle: 290
particle-max-velocity-angle: 360
particle-acceleration: 0
particle-size: 24 24
particle-color: #cccccc22
particle-texture: circle2.png

View File

@ -146,6 +146,7 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/graphics/particlemanager.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particlemanager.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/particlesystem.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particlesystem.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/particleemitter.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particleemitter.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/particleaffector.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/particle.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/particle.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/shader.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/shader.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/shaderprogram.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/shaderprogram.cpp

View File

@ -37,6 +37,7 @@ class ShaderProgram;
class PainterShaderProgram; class PainterShaderProgram;
class Particle; class Particle;
class ParticleEmitter; class ParticleEmitter;
class ParticleAffector;
class ParticleSystem; class ParticleSystem;
typedef std::weak_ptr<Texture> TextureWeakPtr; typedef std::weak_ptr<Texture> TextureWeakPtr;
@ -53,6 +54,7 @@ typedef std::shared_ptr<ShaderProgram> ShaderProgramPtr;
typedef std::shared_ptr<PainterShaderProgram> PainterShaderProgramPtr; typedef std::shared_ptr<PainterShaderProgram> PainterShaderProgramPtr;
typedef std::shared_ptr<Particle> ParticlePtr; typedef std::shared_ptr<Particle> ParticlePtr;
typedef std::shared_ptr<ParticleEmitter> ParticleEmitterPtr; typedef std::shared_ptr<ParticleEmitter> ParticleEmitterPtr;
typedef std::shared_ptr<ParticleAffector> ParticleAffectorPtr;
typedef std::shared_ptr<ParticleSystem> ParticleSystemPtr; typedef std::shared_ptr<ParticleSystem> ParticleSystemPtr;
typedef std::vector<ShaderPtr> ShaderList; typedef std::vector<ShaderPtr> ShaderList;

View File

@ -27,7 +27,7 @@
Particle::Particle(const Point& pos, const Size& size, const PointF& velocity, const PointF& acceleration, float duration, const Color& color, TexturePtr texture) Particle::Particle(const Point& pos, const Size& size, const PointF& velocity, const PointF& acceleration, float duration, const Color& color, TexturePtr texture)
{ {
m_rect = Rect(pos, size); m_rect = Rect(pos, size);
m_pos = PointF(pos.x, pos.y); m_position = PointF(pos.x, pos.y);
m_size = size; m_size = size;
m_velocity = velocity; m_velocity = velocity;
m_acceleration = acceleration; m_acceleration = acceleration;
@ -66,10 +66,10 @@ void Particle::update()
// update position // update position
PointF delta = m_velocity * elapsedTime; PointF delta = m_velocity * elapsedTime;
delta.y *= -1; // painter orientate Y axis in the inverse direction delta.y *= -1; // painter orientate Y axis in the inverse direction
m_pos += delta; m_position += delta;
// update acceleration // update acceleration
m_velocity += m_acceleration * elapsedTime; m_velocity += m_acceleration * elapsedTime;
m_rect.moveTo((int)m_pos.x, (int)m_pos.y); m_rect.moveTo((int)m_position.x, (int)m_position.y);
} }

View File

@ -35,10 +35,16 @@ public:
bool hasFinished() { return m_finished; } bool hasFinished() { return m_finished; }
PointF getPosition() { return m_position; }
PointF getVelocity() { return m_velocity; }
void setPos(const PointF& position) { m_position = position; }
void setVelocity(const PointF& velocity) { m_velocity = velocity; }
private: private:
Color m_color; Color m_color;
TexturePtr m_texture; TexturePtr m_texture;
PointF m_pos; PointF m_position;
PointF m_velocity; PointF m_velocity;
PointF m_acceleration; PointF m_acceleration;
Size m_size; Size m_size;

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* 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 "particle.h"
#include "particleaffector.h"
#include <framework/core/clock.h>
void Gravity270Affector::update(const ParticlePtr& particle, double elapsedTime)
{
// earth gravity is 9.8 m/s². -> in tibia, 32 pixels are equal to 1 meter -> 32 pixels/m -> 9.8 * 32 is gravity constant
const float gravity = 9.8 * 32;
PointF velocity = particle->getVelocity();
velocity += PointF(0, -gravity * elapsedTime);
particle->setVelocity(velocity);
}
void Gravity315Affector::update(const ParticlePtr& particle, double elapsedTime)
{
// earth gravity is 9.8 m/s². -> in tibia, 32 pixels are equal to 1 meter -> 32 pixels/m -> 9.8 * 32 is gravity constant
const float gravity = 9.8 * 32;
PointF velocity = particle->getVelocity();
velocity += PointF(gravity * elapsedTime * cos(7 * Fw::pi / 4), gravity * elapsedTime * sin(7 * Fw::pi / 4));
particle->setVelocity(velocity);
}
bool GoToPointAffector::load(const OTMLNodePtr& node)
{
for(const OTMLNodePtr& childNode : node->children()) {
if(childNode->tag() == "destination")
m_destination = childNode->value<Point>();
else if(childNode->tag() == "rotate-speed-percent")
m_rotateSpeedPercent = childNode->value<float>();
}
return true;
}
void GoToPointAffector::update(const ParticlePtr& particle, double elapsedTime)
{
// must change velocity angle, keeping modulus.
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* 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.
*/
#ifndef PARTICLEAFFECTOR_H
#define PARTICLEAFFECTOR_H
#include "declarations.h"
#include <framework/otml/otml.h>
class ParticleAffector {
public:
virtual bool load(const OTMLNodePtr&) { return true; }
virtual void update(const ParticlePtr&, double) {}
};
class Gravity270Affector : public ParticleAffector {
public:
void update(const ParticlePtr& particle, double elapsedTime);
};
class Gravity315Affector : public ParticleAffector {
public:
void update(const ParticlePtr& particle, double elapsedTime);
};
class GoToPointAffector : public ParticleAffector {
public:
bool load(const OTMLNodePtr& node);
void update(const ParticlePtr& particle, double elapsedTime);
private:
Point m_destination;
float m_rotateSpeedPercent;
};
#endif

View File

@ -35,7 +35,7 @@ bool ParticleManager::load(const std::string& filename)
if(node->tag() == "ParticleSystem") { if(node->tag() == "ParticleSystem") {
ParticleSystemPtr particleSystem = ParticleSystemPtr(new ParticleSystem); ParticleSystemPtr particleSystem = ParticleSystemPtr(new ParticleSystem);
particleSystem->load(node); particleSystem->load(node);
m_particleSystems.push_back(particleSystem); m_systems.push_back(particleSystem);
} }
} }
return true; return true;
@ -47,17 +47,17 @@ bool ParticleManager::load(const std::string& filename)
void ParticleManager::render() void ParticleManager::render()
{ {
for(auto it = m_particleSystems.begin(), end = m_particleSystems.end(); it != end; ++it) for(auto it = m_systems.begin(), end = m_systems.end(); it != end; ++it)
(*it)->render(); (*it)->render();
} }
void ParticleManager::update() void ParticleManager::update()
{ {
for(auto it = m_particleSystems.begin(), end = m_particleSystems.end(); it != end;) { for(auto it = m_systems.begin(), end = m_systems.end(); it != end;) {
const ParticleSystemPtr& particleSystem = *it; const ParticleSystemPtr& particleSystem = *it;
if(particleSystem->hasFinished()) { if(particleSystem->hasFinished()) {
it = m_particleSystems.erase(it); it = m_systems.erase(it);
continue; continue;
} }

View File

@ -25,6 +25,7 @@
#include "declarations.h" #include "declarations.h"
#include "particlesystem.h" #include "particlesystem.h"
#include "particleaffector.h"
class ParticleManager { class ParticleManager {
public: public:
@ -34,7 +35,7 @@ public:
void update(); void update();
private: private:
std::list<ParticleSystemPtr> m_particleSystems; std::list<ParticleSystemPtr> m_systems;
}; };
extern ParticleManager g_particleManager; extern ParticleManager g_particleManager;

View File

@ -22,10 +22,12 @@
#include "particle.h" #include "particle.h"
#include "particlesystem.h" #include "particlesystem.h"
#include <framework/core/clock.h>
ParticleSystem::ParticleSystem() ParticleSystem::ParticleSystem()
{ {
m_finished = false; m_finished = false;
m_lastUpdateTime = g_clock.time();
} }
bool ParticleSystem::load(const OTMLNodePtr& node) bool ParticleSystem::load(const OTMLNodePtr& node)
@ -36,6 +38,21 @@ bool ParticleSystem::load(const OTMLNodePtr& node)
emitter->load(childNode); emitter->load(childNode);
m_emitters.push_back(emitter); m_emitters.push_back(emitter);
} }
else if(childNode->tag().find("Affector") != std::string::npos) {
ParticleAffectorPtr affector;
if(childNode->tag() == "Gravity270Affector")
affector = ParticleAffectorPtr(new Gravity270Affector);
else if(childNode->tag() == "Gravity315Affector")
affector = ParticleAffectorPtr(new Gravity315Affector);
else if(childNode->tag() == "GoToPointAffector")
affector = ParticleAffectorPtr(new GoToPointAffector);
if(affector) {
affector->load(childNode);
m_affectors.push_back(affector);
}
}
} }
return true; return true;
} }
@ -53,6 +70,9 @@ void ParticleSystem::render()
void ParticleSystem::update() void ParticleSystem::update()
{ {
float elapsedTime = g_clock.timeElapsed(m_lastUpdateTime);
m_lastUpdateTime = g_clock.time();
// check if finished // check if finished
if(m_particles.empty() && m_emitters.empty()) { if(m_particles.empty() && m_emitters.empty()) {
m_finished = true; m_finished = true;
@ -77,6 +97,11 @@ void ParticleSystem::update()
it = m_particles.erase(it); it = m_particles.erase(it);
continue; continue;
} }
// pass particles through affectors
for(const ParticleAffectorPtr& particleAffector : m_affectors)
particleAffector->update(particle, elapsedTime);
particle->update(); particle->update();
++it; ++it;
} }

View File

@ -25,18 +25,7 @@
#include "declarations.h" #include "declarations.h"
#include "particleemitter.h" #include "particleemitter.h"
#include "particleaffector.h"
class Affector {
public:
virtual void update() {}
};
class Gravity270Affector : public Affector {
public:
void update() {
// earth gravity is 9.8 m/s². -> in tibia, 32 pixels are equal to 1 meter -> 32 pixels/m -> 9.8 * 32 is gravity constant
}
};
class ParticleSystem : public std::enable_shared_from_this<ParticleSystem> { class ParticleSystem : public std::enable_shared_from_this<ParticleSystem> {
public: public:
@ -54,9 +43,10 @@ public:
private: private:
bool m_finished; bool m_finished;
double m_lastUpdateTime;
std::list<ParticlePtr> m_particles; std::list<ParticlePtr> m_particles;
std::list<ParticleEmitterPtr> m_emitters; std::list<ParticleEmitterPtr> m_emitters;
std::list<Affector> m_affectors; std::list<ParticleAffectorPtr> m_affectors;
}; };
#endif #endif