|
| 1 | +// Simple particle system example for C++. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <math.h> |
| 6 | +#include "lib/color.h" |
| 7 | +#include "lib/effect.h" |
| 8 | +#include "lib/particle.h" |
| 9 | + |
| 10 | +class ParticleTrailEffect : public ParticleEffect |
| 11 | +{ |
| 12 | +public: |
| 13 | + ParticleTrailEffect() |
| 14 | + : angle1(0), angle2(0), baseHue(0) |
| 15 | + {} |
| 16 | + |
| 17 | + float angle1; |
| 18 | + float angle2; |
| 19 | + float baseHue; |
| 20 | + |
| 21 | + virtual void beginFrame(const FrameInfo &f) |
| 22 | + { |
| 23 | + const float tailLength = 8.0f; |
| 24 | + const float speed = 9.0f; |
| 25 | + const float lfoRatio = 0.15f; |
| 26 | + const float hueRate = 0.01f; |
| 27 | + const float brightness = 40.0f; |
| 28 | + const unsigned numParticles = 200; |
| 29 | + |
| 30 | + // Low frequency oscillators |
| 31 | + angle1 = fmodf(angle1 + f.timeDelta * speed, 2 * M_PI); |
| 32 | + angle2 = fmodf(angle2 + f.timeDelta * speed * lfoRatio, 2 * M_PI); |
| 33 | + baseHue = fmodf(baseHue + f.timeDelta * speed * hueRate, 1.0f); |
| 34 | + |
| 35 | + appearance.resize(numParticles); |
| 36 | + for (unsigned i = 0; i < numParticles; i++) { |
| 37 | + float s = float(i) / numParticles; |
| 38 | + float tail = s * tailLength; |
| 39 | + |
| 40 | + float radius = 0.2 + 1.5 * s; |
| 41 | + float x = radius * cos(angle1 + tail); |
| 42 | + float y = radius * sin(angle1 + tail + 10.0 * sin(angle2 + tail * lfoRatio)); |
| 43 | + float hue = baseHue + s * 0.4; |
| 44 | + |
| 45 | + ParticleAppearance& p = appearance[i]; |
| 46 | + p.point = Vec3(x, 0, y); |
| 47 | + p.intensity = (brightness / numParticles) * s; |
| 48 | + p.radius = 0.1 + 0.4f * s; |
| 49 | + hsv2rgb(p.color, hue, 0.5, 0.8); |
| 50 | + } |
| 51 | + |
| 52 | + ParticleEffect::beginFrame(f); |
| 53 | + } |
| 54 | +}; |
0 commit comments