跳轉到內容

遊戲開發指南/理論/遊戲邏輯/建立粒子類

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 函式
華夏公益教科書