opengl-psystem/include/particle.h

70 lines
1.4 KiB
C

#ifndef PARTICLE_H
#define PARTICLE_H
#include <stdlib.h>
#define G_CONSTANT 9.807f
typedef struct {
float x, y, z;
} Vec3;
typedef struct {
float r, g, b, a;
} Color;
typedef struct {
Vec3 position;
Vec3 velocity;
Color color;
float life;
} Particle;
// INFO: This adds adder to out, basicaly out + adder
void vec_add(Vec3 &out, Vec3 adder) {
out.x += adder.x;
out.y += adder.y;
out.z += adder.z;
}
Particle particle_init(Vec3 position, Color color) {
Particle p;
p.position = position;
p.color = color;
p.velocity = {0.0f, 0.0f, 0.0f};
p.life = 10;
return p;
}
int particle_update(Particle *particles, unsigned long long size, float time) {
int alive_count = 0;
for (unsigned long long i = 0; i < size; i++) {
Particle p = particles[i];
if (p.life == 0) {
particles[i].position = {0.0f, 0.0f, 0.0f}; // Start at center
particles[i].velocity = {(rand() % 100 - 50) / 50.0f, 5.0f, 0.0f}; // Random spray up
particles[i].life = 2.0f; // Give it 2 seconds of life
continue;
}
p.velocity.y += (G_CONSTANT * time);
p.position.x += p.velocity.x * time;
p.position.y += p.velocity.y * time;
p.position.z += p.velocity.z * time;
vec_add(p.position, (p.velocity));
if (p.life > 0) p.life -= time;
particles[i] = p;
alive_count++;
}
return alive_count;
}
#endif // PARTICLE_H