-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysobj.cc
268 lines (217 loc) · 4.8 KB
/
physobj.cc
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* =====================================================================================
*
* Filename: physobj.cc
*
* Description: physical object class
*
* Version: 1.0
* Created: 14.03.2017 11:27:04
* Revision: none
* Compiler: gcc
*
* Author: Arvid Krein (mn), [email protected]
* Company: -
*
* =====================================================================================
*/
#include "physobj.h"
inline float realmod (float x, float y)
{
float result = fmod(x, y);
return result >= 0 ? result : result + y;
}
//physobj Functions
physobj::physobj(float x, float y, float vx, float vy)
{
pos = {x, y};
vel = {vx, vy};
}
physobj::~physobj()
{
}
float physobj::getX() const
{
return pos.X;
}
float physobj::getY() const
{
return pos.Y;
}
int physobj::setX(float x)
{
pos.X = x;
return 1;
}
int physobj::setY(float y)
{
pos.Y = y;
return 1;
}
float physobj::getxvel() const
{
return vel.X;
}
float physobj::getyvel() const
{
return vel.Y;
}
int physobj::setxvel (float vx)
{
vel.X = vx;
return 1;
}
int physobj::setyvel(float vy)
{
vel.Y = vy;
return 1;
}
//kraftpartikel Functions
kraftpartikel::kraftpartikel(float x, float y, float mass, float charge) : physobj(x,y)
{
mMass=mass;
mCharge=charge;
}
float kraftpartikel::getFx() const
{
return F.X;
}
float kraftpartikel::getFy() const
{
return F.Y;
}
float kraftpartikel::getMass() const
{
return mMass;
}
float kraftpartikel::getCharge() const
{
return mCharge;
}
void kraftpartikel::setFx(float Fx)
{
F.X = Fx;
}
void kraftpartikel::setFy(float Fy)
{
F.Y = Fy;
}
void kraftpartikel::setMass(float m)
{
mMass = m;
}
void kraftpartikel::setCharge(float q)
{
mCharge = q;
}
void kraftpartikel::iterate(float t) //iteration of simulation
{
pos += t*vel;
vel += t/mMass * F;
}
//Worldframe class functions
Worldframe::Worldframe()
{
}
Worldframe::Worldframe(float tcoulombfaktor, float tgravFx, float tgravFy)
{
coulombfaktor = tcoulombfaktor;
gravF.Y = tgravFy;
gravF.X = tgravFx;
}
Worldframe::~Worldframe()
{
}
void Worldframe::iterate(float t)
{
for ( std::vector<kraftpartikel>::iterator i = vKPartikel.begin(); i < vKPartikel.end(); i++)
{
i->setFx(0.);
i->setFy(0.);
for ( std::vector<kraftpartikel>::iterator j = vKPartikel.begin(); j < vKPartikel.end(); j++)
{
if ( i!=j )
{
elasticBounce(&*i,&*j);
radialForce(&*i,&*j,coulombfaktor,-2.);
}
}
gravitationalForce(&*i,gravF.X,gravF.Y);
i->iterate(t);
if (isoutofworld(*i))
{
switch (bouncetype)
{
case 1:
wallbounce(&*i);
break;
case 2:
periodicboundary(&*i);
break;
}
}
}
}
void Worldframe::radialForce(kraftpartikel* part1, kraftpartikel* part2, float kraftfaktor, float exponent)
{
vec2 r = part2->pos - part1->pos;
part1->F += part1->getMass() * part2->getMass() * kraftfaktor * pow( r.abspow2(), 0.5 * (exponent-1)) * r;
}
void Worldframe::gravitationalForce(kraftpartikel* part, float Fx, float Fy)
{
part->F += gravF;
}
void Worldframe::elasticBounce(kraftpartikel* part1, kraftpartikel* part2)
{
vec2 r1 = part1->pos;
vec2 r2 = part2->pos;
vec2 v1 = part1->vel;
vec2 v2 = part2->vel;
float m1 = part1->getMass();
float m2 = part2->getMass();
float faktor = 5;
vec2 rrel = r2 - r1;
vec2 vrel = v2 - v1;
float r = rrel.abs();
float vrelr = rrel * vrel;
if ( (r <= faktor) && (vrelr < 0) )
{
part1->vel = (1 / (m1 + m2)) * (m1 * v1 + m2 * v2 - m2 * (v1 - v2)) ;
part2->vel = (1 / (m1 + m2)) * (m1 * v1 + m2 * v2 - m1 * (v2 - v1));
}
}
bool Worldframe::isoutofworld(const physobj& part) const
{
bool flag = false;
if ( part.getX() >= size.X || part.getX() <= 0 )
flag = true;
if ( part.getY() >= size.Y || part.getY() <= 0 )
flag = true;
return flag;
}
bool Worldframe::collisioncheck(physobj* part1, physobj* part2)
{
return pow( pow( part2->getX() - part1->getX() , 2 ) + pow( part2->getY() - part1->getY() , 2) , 0.5 ) <= 1. ;
}
void Worldframe::periodicboundary(physobj* part)
{
part->setX( realmod( ( part->getX() ), size.X ) );
part->setY( realmod( ( part->getY() ), size.Y ) );
}
float Worldframe::getEnergy()
{
float T = 0;
for ( auto i = vKPartikel.begin(); i < vKPartikel.end(); i++)
{
T += pow( i->getxvel(), 2 ) + pow( i->getyvel(), 2);
}
return T;
}
void Worldframe::wallbounce(physobj* part)
{
if (part->pos.X > size.X || part->pos.X < 0)
part->vel.X *= -1;
if (part->pos.Y > size.Y || part->pos.Y < 0)
part->vel.Y *= -1;
return;
}