-
Notifications
You must be signed in to change notification settings - Fork 2
Particle.h and .cpp
Hannupekka Sormunen edited this page Dec 29, 2018
·
5 revisions
//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_ */
Particle::Particle() {
//Upon default initilization of an object, private data method Init is called
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;
}
}
//Method for updating the particle position on the screen
void Particle::UpdatePosition(int interval) {
//Modify direction of a particle to rotate to left using negative value or right using positive value on each UpdatePosition call
m_direction += interval * -0.0001;
//Calculate where particle will travel
double xspeed = m_speed * cos(m_direction);
double yspeed = m_speed * sin(m_direction);
//Change the new position for the particle according the speed and direction calculated above
m_xpos += xspeed * interval;
m_ypos += yspeed * interval;
//Check if particle is out of bounds and call Init to create a new one at the middle of the screen
if (m_xpos < -1 || m_xpos > 1 || m_ypos < -1 || m_ypos > 1) {
Init();
}
//Call Init()-method, thus creating a particle middle of the screen, everytime result from rand-function is smaller than 163 (32767 / 200) adding little more randomness to the pattern
if (rand() < RAND_MAX / 200) {
Init();
}
}