-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparticle.cpp
70 lines (59 loc) · 1.7 KB
/
particle.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
70
/*
* Project name: Food collection
* Version 4
* Student : Albert Eduard Merino Pulido
*/
#include "particle.h"
Translation::Translation()
: x(0), y(0), vx(0), vy(0){ }
Translation::Translation(float x, float y, float vx, float vy)
: x(x), y(y), vx(vx), vy(vy){ }
Particle::Particle(){
state = QUIET;
}
void Particle::init_rotation(float rotation, int duration){
vr = rotation / duration;
this->rotation = 0;
state = ROTATE;
time_remaining = duration;
}
void Particle::init_movement(Translation t, int duration){
translation = Translation(0, 0, t.x / duration, t.y / duration);
state = MOVE;
time_remaining = duration;
}
bool Particle::integrate(long t){
if (state == MOVE) {
return integrate_move(t);
} else if (state == ROTATE) {
return integrate_rotate(t);
}
return false;
}
bool Particle::integrate_move(long t){
if (t < time_remaining) {
translation.x += translation.vx * t;
translation.y += translation.vy * t;
time_remaining -= t;
} else if (t >= time_remaining) {
translation.x += translation.vx * time_remaining;
translation.y += translation.vy * time_remaining;
state = QUIET;
return true;
}
return false;
}
bool Particle::integrate_rotate(long t){
if (t < time_remaining) {
rotation += vr * t;
time_remaining -= t;
} else if (t >= time_remaining) {
rotation += vr * time_remaining;
state = QUIET;
return true;
}
return false;
}
Translation Particle::getTranslation(){ return translation; }
float Particle::getRotation(){ return rotation; }
State Particle::getState(){ return state; }