Guide to Game Development/Theory/Game logic/Creating a particle class
Note: the following code uses a Vector3 class.
This is an outline for what a particle class could look like written in C++:
Particle.h:
#include "Vector3.h"
class Particle{
public:
enum ForceMode {
Impulse, //A quick 'jab' of force
Constant //A constant force acting on the particle
};
void AddForce(Vector3 f, ForceMode m);
void Tick();
Particle();
~Particle();
private:
bool _isKinematic;
bool _detectCollisions;
Vector3 _location;
Vector3 _rotation; //Stored as an Euler angle
//Scale can be added to the child class entity
Vector3 _acceleration;
Vector3 _velocity;
Vector3 _constantForce; //All of the constant forces on the particle resolved
Vector3 _impulseForce; //All of the impulseForces to be added this tick resolved (cleared after tick)
int _mass;
};
Particle.cpp:
#include "Particle.h"
void Particle::AddForce(Vector3 f, ForceMode m){
switch (m)
{
case Particle::Impulse:
_impulseForce += f; //Resolve force into current force buffer & Use of operator overloading, incrementing a vector by another vector.
break;
case Particle::Constant:
_constantForce += f; //Resolve force into current force buffer & Use of operator overloading, incrementing a vector by another vector.
break;
default:
break;
}
}
void Particle::Tick(){
//Add constant force
//TODO
//Add impulse force
//TODO
_impulseForce = Vector3::Null; //Resetting impulse
//Add drag force
//TODO
}
Particle::Particle(){
}
Particle::~Particle(){
}