summaryrefslogtreecommitdiff
path: root/modules/sparkler/particle.h
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@gnugeneration.com>2016-12-13 07:51:23 -0800
committerVito Caputo <vcaputo@gnugeneration.com>2016-12-13 07:51:23 -0800
commit8add1663d9a02db2bc65224cdceb480733a81379 (patch)
treefea6aa880a366c007d2b7fdda87c746e6345b301 /modules/sparkler/particle.h
parentaf49b97cd819cec3a19b1ff5ed6076a0d23f4233 (diff)
sparkler: introduce a particle system
A while ago I made this particle system on SDL, and had the beginnings of an octree implemented within it, but never finished actually using the octree to accelerate the proximity searches. This now has the octree completed and of course more particle interactions now that neighbors could be found more quickly. The simulation somewhat resembles a fireworks display. Every particle is drawn as a single pixel. The visual effect is dominated by spontaneously spawned rockets which explode into thousands of particles accompanied by bursts that thrust particles away from the explosion radially in an expanding sphere resembling a shock wave. When the shock wave happens to strike another rocket, it explodes, resulting in another shock wave. This can produce spectacular chain reactions, so it's worth running for some time and seeing what transpires.
Diffstat (limited to 'modules/sparkler/particle.h')
-rw-r--r--modules/sparkler/particle.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/modules/sparkler/particle.h b/modules/sparkler/particle.h
new file mode 100644
index 0000000..95c117e
--- /dev/null
+++ b/modules/sparkler/particle.h
@@ -0,0 +1,79 @@
+#ifndef _PARTICLE_H
+#define _PARTICLE_H
+
+#include "bsp.h"
+#include "fb.h"
+#include "v3f.h"
+
+typedef struct particle_props_t {
+ v3f_t position; /* position in 3d space */
+ v3f_t direction; /* trajectory in 3d space */
+ float velocity; /* linear velocity */
+ float mass; /* mass of particle */
+ float drag; /* drag of particle */
+ int of_use:1; /* are these properties of use/meaningful? */
+} particle_props_t;
+
+typedef enum particle_status_t {
+ PARTICLE_ALIVE,
+ PARTICLE_DEAD
+} particle_status_t;
+
+typedef struct particle_t particle_t;
+typedef struct particles_t particles_t;
+
+typedef struct particle_ops_t {
+ unsigned context_size; /* size of the particle context (0 for none) */
+ int (*init)(particles_t *, particle_t *); /* initialize the particle, called after allocating context (optional) */
+ void (*cleanup)(particles_t *, particle_t *); /* cleanup function, called before freeing context (optional) */
+ particle_status_t (*sim)(particles_t *, particle_t *); /* simulate the particle for another cycle (required) */
+ void (*draw)(particles_t *, particle_t *, int, int, fb_fragment_t *); /* draw the particle, 3d->2d projection has been done already (optional) */
+} particle_ops_t;
+
+struct particle_t {
+ bsp_occupant_t occupant; /* occupant node in the bsp tree */
+ particle_props_t *props;
+ particle_ops_t *ops;
+ void *ctxt;
+};
+
+
+//#define rand_within_range(_min, _max) ((rand() % (_max - _min)) + _min)
+// the style of random number generator used by c libraries has less entropy in the lower bits meaning one shouldn't just use modulo, while this is slower, the results do seem a little different.
+#define rand_within_range(_min, _max) (int)(((float)_min) + ((float)rand() / (float)RAND_MAX) * (_max - _min))
+
+#define INHERIT_OPS NULL
+#define INHERIT_PROPS NULL
+
+
+static inline int particle_init(particles_t *particles, particle_t *p) {
+ if (p->ops->init) {
+ return p->ops->init(particles, p);
+ }
+
+ return 1;
+}
+
+
+static inline void particle_cleanup(particles_t *particles, particle_t *p) {
+ if (p->ops->cleanup) {
+ p->ops->cleanup(particles, p);
+ }
+}
+
+
+static inline particle_status_t particle_sim(particles_t *particles, particle_t *p) {
+ return p->ops->sim(particles, p);
+}
+
+
+static inline void particle_draw(particles_t *particles, particle_t *p, int x, int y, fb_fragment_t *f) {
+ if (p->ops->draw) {
+ p->ops->draw(particles, p, x, y, f);
+ }
+}
+
+
+void particle_convert(particles_t *particles, particle_t *p, particle_props_t *props, particle_ops_t *ops);
+
+#endif
© All Rights Reserved