-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFutureCalc.cpp
85 lines (56 loc) · 1.94 KB
/
FutureCalc.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
#include <iostream>
#include "Vars.h"
#include <vector>
#include <array>
#include <thread>
#include <chrono>
#include <mutex>
std::vector<std::array<int, 2>> getNextPos(int, int);
extern std::vector<Body> bodies;
extern std::vector<std::array<int, 2>> futurePos;
extern bool running;
extern std::mutex mtx;
using namespace std::chrono_literals;
void futureCalcLoop() {
while (running) {
std::this_thread::sleep_for(16ms);
if (bodies.size() > 0) {
if (bodies[bodies.size() - 1].movable) {
mtx.lock();
futurePos = getNextPos(bodies.size()-1, 1000);
mtx.unlock();
}
}
}
}
std::vector<std::array<int, 2>> getNextPos(int bodyPos, int anz) {
std::vector<Body> copy(bodies);
std::vector<std::array<int, 2>> fp(anz);
for (int i = 0; i < anz; i++) {
for (int i2 = 0; i2 < copy.size(); i2++) {
if (copy[i2].movable) {
for (int i3 = 0; i3 < copy.size(); i3++) {
if (i3 != i2) {
double a = copy[i2].pos[0] - copy[i3].pos[0];
double b = copy[i2].pos[1] - copy[i3].pos[1];
double c = sqrt(a * a + b * b);
if (c > 0.1) {
double gForce = (200) * (((double)copy[i2].mass * (double)copy[i3].mass) / (c * c));//6.67408e-11
copy[i2].vel[0] += -(gForce * ((double)a / ((double)abs(a) + (double)abs(b)))) / (double)copy[i2].mass; //-(gForce * ((double)a/((double)Math.abs(a)+(double)Math.abs(b))))/(double)b1.mass;
copy[i2].vel[1] += -(gForce * ((double)b / ((double)abs(a) + (double)abs(b)))) / (double)copy[i2].mass; //-(gForce * ((double)b/((double)Math.abs(a)+(double)Math.abs(b))))/(double)b1.mass;
}
}
}
}
}
for (int i2 = 0; i2 < copy.size(); i2++) {
if (copy[i2].movable) {
copy[i2].pos[0] += copy[i2].vel[0];
copy[i2].pos[1] += copy[i2].vel[1];
}
}
fp[i][0] = (int)copy[bodyPos].pos[0];
fp[i][1] = (int)copy[bodyPos].pos[1];
}
return fp;
}