-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.cpp
59 lines (56 loc) · 1.93 KB
/
physics.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
#include <cstdlib>
#include <cmath>
#include <iostream>
#include "body.hpp"
#include "physics.hpp"
using namespace std;
void simulateTimeStep(struct Body *bodies, int n, double timeStep, double data[]){
int i,j,k,l;
double G = 6.67408*pow(10,-11);
double R = 0;
double **F;
F = (double**) malloc(n*sizeof **F);
double **a;
a = (double**) malloc(n*sizeof **a);
for(i=0;i<n;i++){
F[i] = (double*) malloc(3*sizeof *F[i]);
a[i] = (double*) malloc(3*sizeof *a[i]);
}
for(i=0;i<n;i++){
for(j=0;j<3;j++){
F[i][j]=0;
a[i][j]=0;
}
}
for(i=0;i<n;i++){ //iteracja po cialach
for(l=0;l<3;l++){ //iteracja po wspolrzednych wektora sily i-tego ciala
for(j=0;j<n;j++){ //iteracja po cialach, wzgledem ktorych liczy sie l-ta wspolrzedna wektora sily
R = 0;
if(j!=i){
for(k=0;k<3;k++){ //liczenie odleglosci euklidesowej dwoch cial
R = R + pow(bodies[i].position[k] - bodies[j].position[k],2);
}
R = sqrt(R);
//cout << "r = " << R << endl;
if(R!=0)
F[i][l]+=G*(bodies[i].position[l]-bodies[j].position[l])*bodies[i].mass*bodies[j].mass/pow(R,3);
//else //na wypadek, jakby ciala mialy to samo polozenie
//F[i][l]+=G*(bodies[i].position[l]-bodies[j].position[l])*bodies[i].mass*bodies[j].mass/pow(1000,3);
}
}
F[i][l] = -1 * F[i][l];
}
for(k=0;k<3;k++){
//cout << F[i][k] << endl;
a[i][k] = F[i][k]/bodies[i].mass;
}
for(k=0;k<3;k++){
data[6*i+k]=bodies[i].velocity[k]+a[i][k]*timeStep;
}
for(k=0;k<3;k++){
data[6*i+k+3]=bodies[i].position[k]+data[6*i+k]*timeStep;
}
}
free(F);
free(a);
}