-
Notifications
You must be signed in to change notification settings - Fork 0
/
galaxy.cpp
125 lines (105 loc) · 3.04 KB
/
galaxy.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
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
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "vect.h"
#include "bodies.h"
#include "consts.h"
using namespace std;
static int GalaxyID = 0;
Vect GalaxySpeed(double Mass1, double Mass2, Vect Pos1, Vect Pos2, double Rmin)
{
Vect Dist = Pos1 - Pos2;
double DistInt = Dist.Int();
double V = sqrt((2*G)/(DistInt * (Mass1 + Mass2))) * Mass1;
double Vmax = sqrt((2*G)/(Rmin * (Mass1 + Mass2))) * Mass1;
double theta = asin((Rmin * Vmax)/(DistInt * V));
Vect ort = Pos1/Pos1.Int();
ort = ort.Rotate(theta);
Vect ret = ort * V;
return ret;
}
Vect OrbitalSpeed(Body* B, double angle, double Dist, double Mass ,double direction)
{
double Speed = sqrt(G * (Mass + B->m) / Dist);
Vect ret;
ret.x = direction * Speed * cos(angle * rad + 90 * rad);
ret.y = direction * Speed * sin(angle * rad + 90 * rad);
return ret;
}
void CreateGalaxy(Body** bodies, double GalaxyMass, Vect GalaxyPos, Vect GalaxyVel, double Scale, int Direction, int Start)
{
int bodyCount = Start;
bodies[bodyCount]->m = GalaxyMass;
bodyCount++;
double r = 0.2 * Rmin * Scale;
for(int theta = 0; theta < 360; theta++)
{
if((theta % (360/12)) == 0)
{
bodies[bodyCount]->r.x = r * cos(theta * rad);
bodies[bodyCount]->r.y = r * sin(theta * rad);
bodies[bodyCount]->v = OrbitalSpeed(bodies[bodyCount], theta, r, GalaxyMass, Direction);
bodyCount++;
}
}
r = 0.3 * Rmin * Scale;
for(int theta = 0; theta < 360; theta++)
{
if((theta % (360/18)) == 0)
{
bodies[bodyCount]->r.x = r * cos(theta * rad);
bodies[bodyCount]->r.y = r * sin(theta * rad);
bodies[bodyCount]->v = OrbitalSpeed(bodies[bodyCount], theta, r, GalaxyMass, Direction);
bodyCount++;
}
}
r = 0.4 * Rmin * Scale;
for(int theta = 0; theta < 360; theta++)
{
if((theta % (360/24)) == 0)
{
bodies[bodyCount]->r.x = r * cos(theta * rad);
bodies[bodyCount]->r.y = r * sin(theta * rad);
bodies[bodyCount]->v = OrbitalSpeed(bodies[bodyCount], theta, r, GalaxyMass, Direction);
bodyCount++;
}
}
r = 0.5 * Rmin * Scale;
for(int theta = 0; theta < 360; theta++)
{
if((theta % (360/30)) == 0)
{
bodies[bodyCount]->r.x = r * cos(theta * rad);
bodies[bodyCount]->r.y = r * sin(theta * rad);
bodies[bodyCount]->v = OrbitalSpeed(bodies[bodyCount], theta, r, GalaxyMass, Direction);
bodyCount++;
}
}
r = 0.6 * Rmin * Scale;
for(int theta = 0; theta < 360; theta++)
{
if((theta % (360/36)) == 0)
{
bodies[bodyCount]->r.x = r * cos(theta * rad);
bodies[bodyCount]->r.y = r * sin(theta * rad);
bodies[bodyCount]->v = OrbitalSpeed(bodies[bodyCount], theta, r, GalaxyMass, Direction);
bodyCount++;
}
}
for(int i = Start; i < Start + 121; i++)
{
bodies[i]->r += GalaxyPos;
bodies[i]->v += GalaxyVel;
bodies[i]->parent = GalaxyID;
}
GalaxyID++;
}
void CreatePointMass(Body** bodies, double GalaxyMass, Vect GalaxyPos, Vect GalaxyVel, int Start)
{
int bodyCount = Start;
bodies[bodyCount]->m = GalaxyMass;
bodies[bodyCount]->r += GalaxyPos;
bodies[bodyCount]->v += GalaxyVel;
bodies[bodyCount]->parent = GalaxyID;
GalaxyID++;
}