-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP_State.cpp
91 lines (73 loc) · 2.24 KB
/
P_State.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include "Util.hpp"
#include "P_State.hpp"
#include "Force.hpp"
#include "Archiver.hpp"
#include <iostream>
#include <sstream>
#include <algorithm>
/* a simple class that encompasses the physical state of an object
* Things like it's position, momentum, mass and orientation are here
*/
P_State::P_State(float m, float inertia, v3 pos, Id id) :
mass(m),
inverse_mass(1.0f/m),
inertia(inertia),
inverse_inertia(1.0f/inertia),
position(pos),
id(id) {
}
// just here so that serialize can do it's thing
P_State::P_State() :
mass(0.0f),
inverse_mass(0.0f),
inertia(0.0f),
inverse_inertia(0.0f) {}
m4 P_State::modelMatrix(const v3& scale) const {
//glm::mat4 myModelMatrix = myTranslationMatrix * myRotationMatrix * myScaleMatrix;
m4 model;
m4 rotation(glm::toMat4(orient));
m4 translation(glm::translate(m4(), position));
model = translation * rotation * glm::scale(scale);
return model;
}
v3 P_State::facing() const {
return orient * FORWARD;
}
m4 P_State::viewMatrix(const v3& scale) const {
const v3 dir = facing();
const v3 up_relative = orient * UP;
const float scale_mul = std::max(std::max(scale.x,scale.y),scale.z);
const v3 behindMe = scale_mul * (orient * v3(0.0f,2.0f,3.0f));
//return glm::lookAt(position+behindMe, position + dir, up_relative);
return glm::lookAt(position, position + dir, up_relative);
}
void P_State::set_momentum(const v3& mom) {
momentum = mom;
// recalc velo
velocity = momentum * inverse_mass;
}
void P_State::recalc() {
velocity = momentum * inverse_mass;
ang_velocity = ang_momentum *
inverse_inertia;
orient = glm::normalize(orient);
}
void P_State::apply_force(const Force& f) {
forces.push_back(f);
}
const Forces& P_State::net_forces() const {
return forces;
}
void P_State::clear_forces() {
forces.clear();
}
std::ostream& operator<<(std::ostream& stream, const P_State& state) {
stream << "Mass:" << state.mass << ", pos:" << printV(state.position)
<< ", velo:" << printV(state.velocity);
}