跳转到内容

游戏开发指南/理论/游戏逻辑/创建粒子类

25% developed
来自维基教科书,开放世界中的开放书籍

注意:以下代码使用 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(){

}


Clipboard

待办事项

  • 确定哪些变量需要是公有的,哪些需要是私有的。
  • 为所有私有变量添加 get 函数
  • 添加更多有用的函数
  • 填充 tick 函数
华夏公益教科书