From 26eb3b42a633258c9f63e5bef7aad061162b2a1e Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Fri, 16 Dec 2011 00:48:10 -0200 Subject: [PATCH] some changes to particles --- modules/particle.otpa | 22 +++++++++-- src/framework/graphics/particleaffector.cpp | 43 +++++++++++++-------- src/framework/graphics/particleaffector.h | 13 +++---- src/framework/graphics/particlesystem.cpp | 10 ++--- src/framework/util/point.h | 2 +- 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/modules/particle.otpa b/modules/particle.otpa index f23e82ec..66ef7410 100644 --- a/modules/particle.otpa +++ b/modules/particle.otpa @@ -1,14 +1,14 @@ ParticleSystem - - GoToPointAffector + + AttractionAffector destination: 150 150 Emitter position: 100 100 - burstRate: 2 + burstRate: 99 burstCount: 1 - particle-duration: 1 + particle-duration: 999 particle-position-radius: 0 particle-velocity: 32 @@ -20,3 +20,17 @@ ParticleSystem particle-color: #ffff0044 particle-texture: circle2.png + Emitter + position: 150 150 + burstRate: 99 + burstCount: 1 + + particle-duration: 999 + particle-position-radius: 0 + + particle-velocity: 0 + particle-acceleration: 0 + + particle-size: 24 24 + particle-color: #ffff0044 + particle-texture: circle2.png diff --git a/src/framework/graphics/particleaffector.cpp b/src/framework/graphics/particleaffector.cpp index bb25bc58..460cb5a1 100644 --- a/src/framework/graphics/particleaffector.cpp +++ b/src/framework/graphics/particleaffector.cpp @@ -24,43 +24,52 @@ #include "particleaffector.h" #include -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; +#define DEG_TO_RAD (acos(-1)/180.0) - PointF velocity = particle->getVelocity(); - velocity += PointF(0, -gravity * elapsedTime); +bool GravityAffector::load(const OTMLNodePtr& node) +{ + m_angle = 270 * DEG_TO_RAD; + m_gravity = 9.8; - particle->setVelocity(velocity); + for(const OTMLNodePtr& childNode : node->children()) { + if(childNode->tag() == "angle") + m_angle = childNode->value() * DEG_TO_RAD; + else if(childNode->tag() == "gravity") + m_gravity = childNode->value(); + } + return true; } -void Gravity315Affector::update(const ParticlePtr& particle, double elapsedTime) +void GravityAffector::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)); - + velocity += PointF(m_gravity * elapsedTime * cos(m_angle), m_gravity * elapsedTime * sin(m_angle)); particle->setVelocity(velocity); } -bool GoToPointAffector::load(const OTMLNodePtr& node) +bool AttractionAffector::load(const OTMLNodePtr& node) { + m_acceleration = 32; + for(const OTMLNodePtr& childNode : node->children()) { if(childNode->tag() == "destination") m_destination = childNode->value(); - else if(childNode->tag() == "rotate-speed-percent") - m_rotateSpeedPercent = childNode->value(); + else if(childNode->tag() == "acceleration") + m_acceleration = childNode->value(); } return true; } -void GoToPointAffector::update(const ParticlePtr& particle, double elapsedTime) +void AttractionAffector::update(const ParticlePtr& particle, double elapsedTime) { // must change velocity angle, keeping modulus. + PointF pPosition = particle->getPosition(); + PointF d = PointF(m_destination.x - pPosition.x, pPosition.y - m_destination.y); + if(d.length() == 0) + return; + PointF pVelocity = particle->getVelocity(); + particle->setVelocity(pVelocity + (d / d.length()) * m_acceleration * elapsedTime); } diff --git a/src/framework/graphics/particleaffector.h b/src/framework/graphics/particleaffector.h index 2ab5afc1..284dc7df 100644 --- a/src/framework/graphics/particleaffector.h +++ b/src/framework/graphics/particleaffector.h @@ -32,24 +32,23 @@ public: virtual void update(const ParticlePtr&, double) {} }; -class Gravity270Affector : public ParticleAffector { +class GravityAffector : public ParticleAffector { public: + bool load(const OTMLNodePtr& node); void update(const ParticlePtr& particle, double elapsedTime); -}; -class Gravity315Affector : public ParticleAffector { -public: - void update(const ParticlePtr& particle, double elapsedTime); +private: + float m_angle, m_gravity; }; -class GoToPointAffector : public ParticleAffector { +class AttractionAffector : public ParticleAffector { public: bool load(const OTMLNodePtr& node); void update(const ParticlePtr& particle, double elapsedTime); private: Point m_destination; - float m_rotateSpeedPercent; + float m_acceleration; }; #endif diff --git a/src/framework/graphics/particlesystem.cpp b/src/framework/graphics/particlesystem.cpp index cea32138..4351b7bd 100644 --- a/src/framework/graphics/particlesystem.cpp +++ b/src/framework/graphics/particlesystem.cpp @@ -41,12 +41,10 @@ bool ParticleSystem::load(const OTMLNodePtr& node) 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(childNode->tag() == "GravityAffector") + affector = ParticleAffectorPtr(new GravityAffector); + else if(childNode->tag() == "AttractionAffector") + affector = ParticleAffectorPtr(new AttractionAffector); if(affector) { affector->load(childNode); diff --git a/src/framework/util/point.h b/src/framework/util/point.h index 0b3f87d6..a4683ccf 100644 --- a/src/framework/util/point.h +++ b/src/framework/util/point.h @@ -48,7 +48,7 @@ public: TPoint& operator-=(const TPoint& other) { x-=other.x; y-=other.y; return *this; } TPoint operator*(const TPoint& other) const { return TPoint(x * other.x, y * other.y); } TPoint& operator*=(const TPoint& other) { x*=other.x; y*=other.y; return *this; } - TPoint operator*(const T v) const { return TPoint(x * v, y * v); } + TPoint operator*(const T v) const { return TPoint(x*v, y*v); } TPoint& operator*=(const T v) { x*=v; y*=v; return *this; } TPoint operator/(const TPoint& other) const { return TPoint(x/other.x, y/other.y); } TPoint& operator/=(const TPoint& other) { x/=other.x; y/=other.y; return *this; }