-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.cpp
More file actions
67 lines (50 loc) · 1.67 KB
/
Particle.cpp
File metadata and controls
67 lines (50 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "Particle.h"
#include "Random.h"
#include "Draw.h"
#include <cmath>
bool Particle::gravityField = false;
float Particle::opacity = 30;
float Particle::sizeMult = 1;
Particle::Particle( const vector_type& pos, const vector_type& v,
value_type minSpeed, value_type maxSpeed, float scale,
const Color& c )
: Actor( pos ), c( c ), scale( scale*sizeMult ), maxSpeed( maxSpeed )
{
value_type speed = random( minSpeed, maxSpeed );
float angle = random_angle();
this->v.x( std::cos(angle) * speed );
this->v.y( std::sin(angle) * speed );
this->v += v;
this->c *= random( 0.1f, 1.5f );
this->c.a( 0.7 );
isVisible = true;
}
void Particle::draw()
{
// If =a, particles will represent the gravities affecting them.
// If =v, they will be drawn according to their motion.
vector_type& direction = gravityField? a : v;
float max = gravityField? 0.001f : maxSpeed;
float angle = std::atan2( direction.y(), direction.x() ) * (180/3.145f);
float widthRatio = 1.0;
if( gravityField )
widthRatio += max / magnitude(direction) * 11;
else
widthRatio += magnitude(direction) / max * 11;
if( widthRatio > 15 )
widthRatio = 15;
if( direction == a )
angle += 90;
float verts[] = {
-scale * widthRatio, -scale,
scale * widthRatio, -scale,
scale * widthRatio, scale,
-scale * widthRatio, scale,
};
glTranslatef( s.x(), s.y(), 1 );
glColor4f( c.r(), c.g(), c.b(), opacity );
glRotatef( angle, 0, 0, 1 );
draw::draw( verts, 4 );
glDisable( GL_DEPTH_TEST );
glLoadIdentity();
}