-
Notifications
You must be signed in to change notification settings - Fork 0
/
RectangularParticle.cpp
69 lines (62 loc) · 1.76 KB
/
RectangularParticle.cpp
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
68
69
/*
* Particle simulation program using the SDL library
* Based on examples by John Purcell's (from Cave of Programming)
*
* @author J. Alvarez
*/
#include <stdlib.h>
#include <iostream>
#include "RectangularParticle.hpp"
#include "Screen.hpp"
#include <math.h>
namespace ParticleSimulation {
RectangularParticle::RectangularParticle(int mode): m_radSpeed(0),
m_angle(0), Particle(0, 0, 0, 0, mode) {
init();
}
void RectangularParticle::init() {
m_coord1 = 0;
m_coord2 = 0;
switch (m_mode) {
case Mode::RECT_RECT:
m_coord1Speed = 0.0005 * ((2.0 * rand() / RAND_MAX) - 1);
m_coord2Speed = 0.0005 * ((2.0 * rand() / RAND_MAX) - 1);
break;
case Mode::RECT_CIRC:
m_angle = 2 * M_PI * rand() / RAND_MAX;
m_radSpeed = 0.001 * rand() / RAND_MAX;
break;
case Mode::RECT_IMPD:
m_angle = 2 * M_PI * rand() / RAND_MAX;
m_radSpeed = 0.05 * rand() / RAND_MAX;
m_radSpeed *= m_radSpeed;
break;
default:
std::cout << "Rectangular mode not implemented!" << std::endl;
break;
}
}
void RectangularParticle::update(int interval) {
switch(m_mode) {
case Mode::RECT_CIRC:
m_coord1Speed = m_radSpeed * cos(m_angle);
m_coord2Speed = m_radSpeed * sin(m_angle);
break;
case Mode::RECT_RECT:
if (m_coord1 + m_coord1Speed < -1 || m_coord1 + m_coord1Speed > 1)
m_coord1Speed *= -1;
if (m_coord2 + m_coord2Speed < -1 || m_coord2 + m_coord2Speed > 1)
m_coord2Speed *= -1;
break;
case Mode::RECT_IMPD:
m_angle += interval * 0.0003;
m_coord1Speed = m_radSpeed * cos(m_angle);
m_coord2Speed = m_radSpeed * sin(m_angle);
if (m_coord1 < -1 || m_coord1 > 1 || m_coord2 < -1 || m_coord2 > 1)
init();
break;
}
m_coord1 += m_coord1Speed * interval;
m_coord2 += m_coord2Speed * interval;
}
} // namespace ParticleSimulation