-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.hh
154 lines (134 loc) · 3.23 KB
/
Object.hh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef OBJECT_HH
#define OBJECT_HH
#include <exception>
#include "ZenMatrix.hh"
#include "Quaternion.hh"
#define RANDOM 0.01
#define DAMPEN 0.9
#define DAMPEN_SPEED 0.001
#define FRICTION 1.0
#define FRICTION_LIMIT 0.05
template<class F>
class Object
{
public:
Object()
: mRotation(1),
mMass(1),
mMomentOfInertia(1),
mRadius(1),
mImmobile(false),
mOrientationValid(false)
{
}
void SetSize(F mass, F radius)
{
mMass = mass;
mRadius = radius;
mMomentOfInertia = mMass * mRadius * mRadius * 2.0 / 5.0;
}
virtual void ApplyAngularVelocity(F timeslice)
{
if (mImmobile) {
return;
}
F length = Length(mAngularVelocity);
mRotation = Quaternion<F>(mAngularVelocity / length, length * timeslice) * mRotation;
mOrientationValid = false;
}
void ApplyVelocity(F timeslice)
{
if (mImmobile) {
return;
}
mLocation += mVelocity * timeslice;
}
void ApplyForce(ZenMatrix<F, 3, 1> force, F timeslice)
{
if (mImmobile) {
return;
}
mVelocity += force * (timeslice / mMass);
}
void ApplyAcceleration(ZenMatrix<F, 3, 1> acceleration, F timeslice)
{
if (mImmobile) {
return;
}
mVelocity += acceleration * timeslice;
}
void ApplyTorque(ZenMatrix<F, 3, 1> torque, F timeslice)
{
if (mImmobile) {
return;
}
mAngularVelocity += torque * (timeslice / mMomentOfInertia);
}
void DampenAngularVelocity(F timeslice)
{
if (mImmobile) {
return;
}
mAngularVelocity *= pow(DAMPEN, timeslice / DAMPEN_SPEED);
// F angularSpeed = Length(mAngularVelocity);
// if (angularSpeed > FRICTION_LIMIT) {
// if (timeslice * FRICTION / angularSpeed > 1.0) {
// mAngularVelocity *= 0;
// } else {
// mAngularVelocity *= (1.0 - timeslice * FRICTION / angularSpeed);
// }
// }
}
void RandomizeAngularVelocity(F timeslice)
{
if (mImmobile) {
return;
}
mAngularVelocity += ZenMatrix<F, 3, 1>::GetRandom(RANDOM * timeslice);
}
void Interact(Object<F>* other, F timeslice)
{
InteractMagnetically(other, timeslice);
InteractPhysically(other, timeslice);
try {
InteractGravitationally(other, timeslice);
} catch(...) {
try {
other->InteractGravitationally(this, timeslice);
} catch(...) {}
}
}
ZenMatrix<F, 3, 1> mLocation;
ZenMatrix<F, 3, 1> mVelocity;
Quaternion<F> mRotation;
ZenMatrix<F, 3, 1> mAngularVelocity;
F mMass;
F mMomentOfInertia;
F mRadius;
protected:
virtual void InteractMagnetically(Object<F>* other, F timeslice) {}
virtual void InteractPhysically(Object<F>* other, F timeslice) {}
virtual void InteractGravitationally(Object<F>* other, F timeslice)
{
throw std::exception();
}
const ZenMatrix<F, 3, 1>& GetOrientation() {
if (!mOrientationValid) {
ZenMatrix<F, 3, 1> x;
x(0, 0) = 1;
mOrientation = mRotation.Rotate(x);
mOrientationValid = true;
}
return mOrientation;
}
bool mImmobile;
private:
ZenMatrix<F, 3, 1> mOrientation;
bool mOrientationValid;
};
template<class F>
std::ostream& operator<<(std::ostream& out, const Object<F>& object)
{
return out << "( " << object.mLocation << " " << object.mRotation << " )";
}
#endif // OBJECT_HH