/
aoBodyEntity.h
202 lines (181 loc) · 3.1 KB
/
aoBodyEntity.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
ONLY FILE NEEDED TO INCLUDE
*/
#ifndef AOBODYENTITY_H
#define AOBODYENTITY_H
#include "aoEntity.h"
namespace ao
{
namespace graphics
{
class BodyEntity:public BaseEntity
{
protected:
double t;
double c;
double v;
double m;
double s;
public:
BodyEntity();
virtual ~BodyEntity() {}
//Functionality
double Velocity()const
{
return v;
}
void SetVelocity(const double& new_Vel)
{
v = new_Vel;
}
double Mass()const
{
return m;
}
//Vector3 Side()const{return m_Side;}
double Thrust()const
{
return t;
}
void SetThrust(double new_thrust)
{
t = new_thrust;
}
double DragCoefficient()const
{
return c;
}
void SetDragCoefficient(double new_dragCoeffiecient)
{
c = new_dragCoeffiecient;
}
void StepSimulation(double dt);
void StepSimulationAdaptive(double dt);
void StepSimulationImproved(double dt);
void StepSimulationRunge(double dt);
};
inline BodyEntity::BodyEntity()
{
s=0;
t=0;
v=0;
c=0;
m=0;
}
//Physic Stuffs
//Eulers basic method
inline void BodyEntity::StepSimulation(double dt)
{
//placeholders for calculations
double F;
double A;
double V;
double S;
//calculate the total force
F=(t-(c*v));
//calculate acceleration
A = F/m;
//newVelocity
V= v+A*dt;
//update
v=V;
s=S;
}
inline void BodyEntity::StepSimulationAdaptive(double dt)
{
double F;
double A;
double V;
double S;
double V1;
double V2;
double DT;
double ET;
//
double ETO;
//take one step to estimate
F=(t-(c*v));
A = F/m;
V1= v+A*dt;
//take two steps to estimate a further approxomation
F=(t-(c*v));
A = F/m;
V2= v+A*(dt/2);
F=(t-(c*V2));
A = F/m;
V2= V2+A*(dt/2);
//estimate the error difference
ET=abs(V1-V2);
//estiamte a new step size
DT=dt*sqrt(ETO/ET);
if(DT<dt)
{
//Smaller Step
F=(t-(c*v));
A = F/m;
V=v+A*DT;
S=s+V*DT;
}
else
{
//Original Step
V=V1;
S=s+V*dt;
}
v=V;
s=S;
}
inline void BodyEntity::StepSimulationImproved(double dt)
{
//placeholders for calculations
double F;
double A;
double V;
double S;
double K1;
double K2;
F=(t-(c*v));
A = F/m;
K1=dt*A;
F=(t-(c*(v+K1)));
A = F/m;
K2=dt*A;
V=v+(K1+K2)/2;
S=s+V*dt;
//update
v=V;
s=S;
}
inline void BodyEntity::StepSimulationRunge(double dt)
{
//placeholders for calculations
double F;
double A;
double V;
double S;
double K1;
double K2;
double K3;
double K4;
F=(t-(c*v));
A = F/m;
K1=dt*A;
F=(t-(c*(v+K1/2)));
A = F/m;
K2=dt*A;
F=(t-(c*(v+K2/2)));
A = F/m;
K3=dt*A;
F=(t-(c*(v+K3)));
A = F/m;
K4=dt*A;
//
V=v+(K1+2*K2 + 2*K3 + K4)/6;
S=s+V*dt;
//update
v=V;
s=S;
}
}
}
#endif