-
Notifications
You must be signed in to change notification settings - Fork 0
/
particlesystem.h
169 lines (114 loc) · 3.73 KB
/
particlesystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Name: Aranya Meas
// Quarter, Year: Spring 2014
// Lab: 3
//
// This file is to be modified by the student.
// particlesystem.h
////////////////////////////////////////////////////////////
#ifndef __PARTICLESYSTEM_H__
#define __PARTICLESYSTEM_H__
#include "vector3.h"
#include "color.h"
#include <vector>
#include <map>
// Particle information and its functions
struct Particle
{
Vector3 pos;
Vector3 vel;
Vector3 acc;
double mass;
// For particles which may expire can use this value to countdown
double timer;
// Size of the particle to render on the screen
double size;
// Color of the particle
Color4 col;
// Functions which add to the particle's acceleration
void applyForce(const Vector3 & force);
void applyForces(const std::vector<Vector3> & forces);
Particle(const Vector3 & p = Vector3(),
const Vector3 & v = Vector3(),
const Vector3 & a = Vector3(), double m = 1.0,
double t = 1.0, double sz = 1.0, const Color4 & c = Color4());
void update(double dt);
void render() const;
};
// Base class for a Particle System
class ParticleSystem
{
protected:
Vector3 location;
std::vector<Particle*> particles;
public:
ParticleSystem(const Vector3 & startingLocation = Vector3());
virtual ~ParticleSystem();
// A particle system should implement the following functions
// Initializes the particle system generating particles and any other information
virtual void init() = 0;
// Calculates any forces and updates all particles
virtual void update(double dt);
// Renders all particles and anything else particular to that particle system
virtual void render() const;
// Scans the list of particles and removes expired particles (unless overriden)
virtual void cleanup();
// If there are no more particles in the list, the particle system is done
virtual bool isDone() const;
};
// Main functions to update and clean all particle systems
void updateParticleSystems(std::vector<ParticleSystem*> & psystems, double dt);
void cleanupParticleSystems(std::vector<ParticleSystem*> & psystems);
// Sample Implementation of a Particle System
class ParticleSystemFireworks : public ParticleSystem
{
protected:
public:
ParticleSystemFireworks(const Vector3 & startingLocation = Vector3());
virtual ~ParticleSystemFireworks();
// Extended functions from the base class Particle System
virtual void init();
virtual void update(double dt);
virtual void render() const;
virtual void cleanup();
virtual bool isDone() const;
};
// Interface for the Spring-Mass based Particle System
//
// You are to complete this class and free to add
// any additional variables/functions necessary
class ParticleSystemSpringMass : public ParticleSystem
{
protected:
// Helper struct that identifies a connection between two objects
// using Springs
struct SpringJoint
{
// Reference to the two linked particles
Particle* particle1;
Particle* particle2;
// Strength of the connection
double stiffness;
// The length which the spring is at equilibrium
double length;
double damp;
// Constructor
SpringJoint(Particle* p1, Particle* p2, double k, double d, double l);
// Returns the force of the spring applied to particle1
// Note: particle2 is simply the negative return value
Vector3 calculateSpringForce() const;
// Renders the spring joint
void render() const;
};
// Tracks all spring connections in the particle system
std::vector<SpringJoint> springConnections;
public:
ParticleSystemSpringMass(const Vector3 & startingLocation = Vector3());
virtual ~ParticleSystemSpringMass();
// Extended functions from the base class Particle System
virtual void init();
virtual void update(double dt);
virtual void render() const;
virtual void cleanup();
virtual bool isDone() const;
};
#endif