-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Stars.cpp
More file actions
153 lines (118 loc) · 3.38 KB
/
Binary_Stars.cpp
File metadata and controls
153 lines (118 loc) · 3.38 KB
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
#include <cmath>
#include <raylib.h>
#include <vector>
#include <iostream>
using namespace std;
struct star
{
float x, y;
float radius;
float speedx, speedy;
float accelx, accely;
float mass;
void Draw()
{
DrawCircle((int)x, (int)y, radius, YELLOW);
}
};
const float G_const = 500000.0f;
const int Limit_Bezier = 300;
Vector2 points1[Limit_Bezier];
Vector2 points2[Limit_Bezier];
int numofpoints1 = 0;
int numofpoints2 = 0;
int main()
{
InitWindow(1920, 1000, "Binary_Stars");
SetWindowState(FLAG_VSYNC_HINT);
star star1;
star1.x = 2.4*GetScreenWidth() / 4.0f;
star1.y = GetScreenHeight() / 3.0f;
star1.radius = 10;
star1.speedx = -20;
star1.speedy = -280;
star1.mass = 500.0f;
star star2;
star2.x = 1.6* GetScreenWidth() / 4.0f;
star2.y = GetScreenHeight() / 3.0f;
star2.radius = 25.0f;
star2.speedx = 20;
star2.speedy = 200;
star2.mass = 750.0f;
while (!WindowShouldClose())
{
float position_vector_magnitude = sqrt((pow(star1.x - star2.x, 2)) + (pow(star1.y - star2.y, 2)));
star1.accelx = abs(G_const * star2.mass * (1 / pow(position_vector_magnitude, 2)) * ((star1.x - star2.x) / sqrt(pow(star1.x - star2.x, 2) + pow(star1.y - star2.y, 2))));
if (star1.x > star2.x)
{
star1.accelx *= -1;
}
star1.accely = abs(G_const * star2.mass * (1 / pow(position_vector_magnitude, 2)) * ((star1.y - star2.y) / sqrt(pow(star1.x - star2.x, 2) + pow(star1.y - star2.y, 2))));
if (star1.y > star2.y)
{
star1.accely *= -1;
}
star2.accelx = abs(G_const * star1.mass * (1 / pow(position_vector_magnitude, 2)) * ((star1.x - star2.x) / sqrt(pow(star1.x - star2.x, 2) + pow(star1.y - star2.y, 2))));
if (star2.x > star1.x)
{
star2.accelx *= -1;
}
star2.accely = abs(G_const * star1.mass * (1 / pow(position_vector_magnitude, 2)) * ((star1.y - star2.y) / sqrt(pow(star1.x - star2.x, 2) + pow(star1.y - star2.y, 2))));
if (star2.y > star1.y)
{
star2.accely *= -1;
}
// Equations of Acceleration Using Newton's Law of Gravitation and Vector Decomposition
star1.speedx += star1.accelx * GetFrameTime(); // Update Speed of Stars
star1.speedy += star1.accely * GetFrameTime();
star2.speedx += star2.accelx * GetFrameTime();
star2.speedy += star2.accely * GetFrameTime();
star1.x += star1.speedx * GetFrameTime(); // Update Position of Stars
star1.y += star1.speedy * GetFrameTime();
star2.x += star2.speedx * GetFrameTime();
star2.y += star2.speedy * GetFrameTime();
if (numofpoints1 < Limit_Bezier)
{
points1[numofpoints1] = {star1.x, star1.y};
numofpoints1++;
}
else
{
// Shift points to the left and discard the first point
for (int i = 1; i < Limit_Bezier; i++)
{
points1[i - 1] = points1[i];
}
points1[Limit_Bezier - 1] = {star1.x, star1.y };
}
if (numofpoints2 < Limit_Bezier)
{
points2[numofpoints2] = {star2.x, star2.y};
numofpoints2++;
}
else
{
// Shift points to the left and discard the first point
for (int i = 1; i < Limit_Bezier; i++)
{
points2[i - 1] = points2[i];
}
points2[Limit_Bezier - 1] = {star2.x, star2.y};
}
BeginDrawing();
ClearBackground(BLACK);
for (int i = 0; i < numofpoints1 - 1; i++)
{
DrawLineBezier(points1[i], points1[i + 1], 1.5f, PINK);
}
for (int i = 0; i < numofpoints2 - 1; i++)
{
DrawLineBezier(points2[i], points2[i + 1], 1.5f, SKYBLUE);
}
star1.Draw();
star2.Draw();
EndDrawing();
}
CloseWindow();
return 0;
}