Skip to content

Particle.h and .cpp

Hannupekka Sormunen edited this page Dec 10, 2018 · 5 revisions

Particle.h

//Header guards for PARTICLE_H_
#ifndef PARTICLE_H_
#define PARTICLE_H_

//Define guards for _USE_MATH_DEFINES, so that M_PI can be used.
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES

//Include C++ system libraries
#include <cmath>
#include <cstdlib>

//Create a namespace for this the use of this class
namespace particlefire {

	//Create a class Particle inside namespace ParticleFire.
	class Particle {

		//Private data members of a Particle-class:
	private:
		double m_speed;
		double m_direction;
		double m_xpos;
		double m_ypos;

		//Public data members of a Particle-class:
	public:

		//Constructors and destructor of a Particle-class:
	public:
		Particle();
		virtual ~Particle();

		//Private data methods of a Particle-class:
	private:
		void Init();//Method for initializing a single particle's position, traveling speed and direction

		//Public data methods of a Particle-class:
	public:
		void UpdatePosition(int interval);//Method for updating the particle position on the screen
		double GetXpos() { return m_xpos; };//Method for acquiring the position of a particle on a x-axis
		double GetYpos() { return m_ypos; };//Method for acquiring the position of a particle on a y-axis
	};

} /* end of namespace ParticleFire */

#endif /*_USE_MATH_DEFINES*/
#endif /* PARTICLE_H_ */

Constructor

	Particle::Particle() {
		//Upon default initilization of an object, private data method Init is called
		Init();
	}

Init()

	//Method for initializing a single particle's position, traveling speed and direction
	void Particle::Init() {
		//Set particle to start at the middle of the screen.
		m_xpos = 0;
		m_ypos = 0;

		//Set the direction and speed by random 
		m_direction = (2 * M_PI * rand()) / RAND_MAX;//Direction in radians where 2 * PI is 360 degrees
		m_speed = (0.0004 * rand()) / RAND_MAX;
		
		//Increase the speed of certain particles. So everytime result from rand-function is smaller than 32.767 (32767 / 1000) adding little more randomness to the pattern
		if (rand() < RAND_MAX / 1000) {
		m_speed *= m_speed / 0.75;
		}
	}

Clone this wiki locally