From 09b757f0182dda77d5ca3f7c3636833946fb0e65 Mon Sep 17 00:00:00 2001 From: lognz Date: Fri, 10 Mar 2017 06:42:52 -0600 Subject: [PATCH] 2 new area particle distributions: borderellipse and borderrectangle --HG-- branch : particle system new features --- src/modules/graphics/ParticleSystem.cpp | 42 ++++++++++++++++++++++--- src/modules/graphics/ParticleSystem.h | 4 ++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/modules/graphics/ParticleSystem.cpp b/src/modules/graphics/ParticleSystem.cpp index 0da05f0d0..2deee15cc 100644 --- a/src/modules/graphics/ParticleSystem.cpp +++ b/src/modules/graphics/ParticleSystem.cpp @@ -261,6 +261,10 @@ void ParticleSystem::initParticle(Particle *p, float t) p->position = pos; + min = direction - spread/2.0f; + max = direction + spread/2.0f; + float dir = (float) rng.random(min, max); + float rand_x, rand_y; switch (areaSpreadDistribution) { @@ -278,6 +282,38 @@ void ParticleSystem::initParticle(Particle *p, float t) p->position.x += areaSpread.getX() * (rand_x * sqrt(1 - 0.5f*pow(rand_y, 2))); p->position.y += areaSpread.getY() * (rand_y * sqrt(1 - 0.5f*pow(rand_x, 2))); break; + case DISTRIBUTION_BORDER_ELLIPSE: + rand_x = (float) rng.random(0, LOVE_M_PI * 2); + p->position.x += cosf(rand_x) * areaSpread.getX(); + p->position.y += sinf(rand_x) * areaSpread.getY(); + dir += rand_x; + break; + case DISTRIBUTION_BORDER_RECTANGLE: + // Unwraps rectangle border onto straight line and pick random point + rand_x = (float) rng.random((areaSpread.getX() + areaSpread.getY()) * -2, (areaSpread.getX() + areaSpread.getY()) * 2); + min = areaSpread.getY() * 2; + if (rand_x < -min) + { + p->position.x += rand_x + min + areaSpread.getX(); + p->position.y += -areaSpread.getY(); + } + else if (rand_x < 0) + { + p->position.x += -areaSpread.getX(); + p->position.y += rand_x + (areaSpread.getY()); + } + else if (rand_x < min) + { + p->position.x += areaSpread.getX(); + p->position.y += rand_x - (areaSpread.getY()); + } + else + { + p->position.x += rand_x - min - areaSpread.getX(); + p->position.y += areaSpread.getY(); + } + dir += atan2(p->position.y - pos.getY(), p->position.x - pos.getX()); + break; case DISTRIBUTION_NONE: default: break; @@ -289,10 +325,6 @@ void ParticleSystem::initParticle(Particle *p, float t) max = speedMax; float speed = (float) rng.random(min, max); - min = direction - spread/2.0f; - max = direction + spread/2.0f; - float dir = (float) rng.random(min, max); - p->velocity = love::Vector(cosf(dir), sinf(dir)) * speed; p->linearAcceleration.x = (float) rng.random(linearAccelerationMin.x, linearAccelerationMax.x); @@ -1029,6 +1061,8 @@ StringMap ParticleSystem::distributions(ParticleSystem::distributionsEntries, sizeof(ParticleSystem::distributionsEntries)); diff --git a/src/modules/graphics/ParticleSystem.h b/src/modules/graphics/ParticleSystem.h index 3b8a78f7e..8cf1f9e60 100644 --- a/src/modules/graphics/ParticleSystem.h +++ b/src/modules/graphics/ParticleSystem.h @@ -52,7 +52,7 @@ public: static love::Type type; /** - * Type of distribution new particles are drawn from: None, uniform, normal, ellipse. + * Type of distribution new particles are drawn from: None, uniform, normal, ellipse, borderellipse, borderrectangle. */ enum AreaSpreadDistribution { @@ -60,6 +60,8 @@ public: DISTRIBUTION_UNIFORM, DISTRIBUTION_NORMAL, DISTRIBUTION_ELLIPSE, + DISTRIBUTION_BORDER_ELLIPSE, + DISTRIBUTION_BORDER_RECTANGLE, DISTRIBUTION_MAX_ENUM };