游戏开发指南/理论/游戏逻辑/创建粒子类
外观
注意:以下代码使用 Vector3 类。
这是一个用 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(){
}