Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions Star_Forge_0.1/celestial_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,124 @@ Celestial_Body Celestial_Body::operator -(Phase_vector a)
res.v_y-=a.v_y;
return res;
}

Celestial_Body Celestial_Body::operator =(Celestial_Body a)
{
x = a.x;
y = a.y;
v_x = a.v_x;
v_y = a.v_y;
Radius = a.Radius;
Mass = a.Mass;
}

/*int Celestial_Body::operator ==(Celestial_Body a)
{
if ((x == a.x)&&(y == a.y)&&(v_x == a.v_x)&&(v_y == a.v_y)&&(w_y == a.w_y)&&(w_y == a.w_y)&&(Radius == a.Radius)&&(Mass == a.Mass))
return true;
else return false;
}
int Celestial_Body::operator !=(Celestial_Body a)
{
if ((x != a.x)||(y != a.y)||(v_x != a.v_x)||(v_y != a.v_y)||(w_y != a.w_y)||(w_y != a.w_y)||(Radius != a.Radius)||(Mass != a.Mass))
return true;
else return false;
}*/

//Atlas section

Atlas::Atlas()
{
first = NULL;
last = NULL;
amount = 0;
}

Atlas::Atlas(Celestial_Body a)
{
Atlas_node tmp = new Atlas_node_el;
tmp->body = a;
tmp->next = NULL;
CircleShape avat(a.Radius);
tmp->avatar = avat;
first = tmp;
last = tmp;
amount = 1;
}

void Atlas::add(Celestial_Body a)
{
Atlas_node tmp = new Atlas_node_el;
tmp->body = a;
tmp->next = NULL;
CircleShape avat(a.Radius);
tmp->avatar = avat;
cout<<"1\n";
if(last != NULL)
{
last->next = tmp;
cout<<"2\n";
last = last -> next;
cout<<"3\n";
}
else
{
last = tmp;
first = tmp;
}
amount++;
return;
}

/*void Atlas::remove(Celestial_Body* a)
{
Celestial_Body* tmp = first, tmp2;
if (first == a)
{
first = first->next;
free(tmp);
return;
}
while (tmp != NULL)
{
if(tmp->next == a)
{
tmp2 = tmp->next;
tmp->next = tmp->next->next;
free(tmp2);
}
}
}*/

void Atlas::del()
{
Atlas_node tmp1 = first;
while (tmp1 != NULL)
{
Atlas_node tmp2 = tmp1;
tmp1 = tmp1->next;
delete(tmp2);
}
}






















55 changes: 55 additions & 0 deletions Star_Forge_0.1/celestial_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define CELESTIAL_BODY

#include <iostream>
#include <SFML/Graphics.hpp>
using namespace sf;

class Vector
{
Expand Down Expand Up @@ -61,6 +63,26 @@ class Phase_vector: public Vector
Phase_vector operator /(float a);
Phase_vector operator +(Phase_vector a);
Phase_vector operator -(Phase_vector a);
/////////////////////////////////////Секция ввода/вывода
friend std::ostream& operator<<(std::ostream& os, Phase_vector& a)
{
os<<"("<<a.x<<","<<a.y<<","<<a.v_x<<","<<a.v_y<<")\n";
return os;
}

friend std::istream& operator>>(std::istream& os, Phase_vector& a)
{
float c, b, f, g;
os>>c;
os>>b;
os>>f;
os>>g;
a.x = c;
a.y = b;
a.v_x=f;
a.v_y=g;
return os;
}
};

/////////////////////////////////////////////Celestial Body class is an expansion of Phase_vector class
Expand All @@ -71,14 +93,47 @@ class Celestial_Body: public Phase_vector
float w_y;
float Radius;
float Mass;
Color color;
Texture texture;
Celestial_Body();
Celestial_Body(const Celestial_Body& other);
Celestial_Body(float a, float b, float c, float d, float e, float f, float g, float k);
Celestial_Body operator =(Celestial_Body a);
Celestial_Body operator +(Phase_vector a);
Celestial_Body operator -(Phase_vector a);
int operator ==(Celestial_Body a);
int operator !=(Celestial_Body a);
friend std::ostream& operator<<(std::ostream& os, Celestial_Body& a)
{
os<<"("<<a.x<<","<<a.y<<","<<a.v_x<<","<<a.v_y<<")\n";
return os;
}
void Move(float t_scale, Phase_vector k1, Phase_vector k2, Phase_vector k3, Phase_vector k4);
};

typedef struct _Atlas_node
{
Celestial_Body body;
CircleShape avatar;
_Atlas_node* next;
} *Atlas_node, Atlas_node_el;

class Atlas
{
public:
Atlas_node first;
Atlas_node last;
int amount;
Atlas();
Atlas(Celestial_Body a);
void add(Celestial_Body a);
//void remove(Celestial_Body a);
void del();
void draw(RenderWindow window);
void move();
};




#endif
56 changes: 56 additions & 0 deletions Star_Forge_0.1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#define WIDTH 600
#define T_SCALE 1

#include "celestial_body.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include "method.cpp"
#include "celestial_body.cpp"
using namespace sf;
int main()
{
Clock clock;
RenderWindow window(VideoMode(WIDTH,WIDTH),"test");
Atlas atl;
Celestial_Body Sun(0,0,0,0,0,0,5,1.9885e30), Earth(152.1e9,0,0,29.783e3,0,0,1,5.9726e24), Mars(206.62e9 , 0, 0, 26.50e3, 0, 0, 1, 0.64171e24);
Celestial_Body Mercury(46e9, 0, 0, 58.98e3, 0, 0, 1, 0.33011e24), Venus(107.48e9, 0, 0, 35.26e3, 0, 0, 1, 4.8675e24);//, Jupiter(740.52e9, 0, 0, 13.72e3, 0, 0, 3, 1898.19e24);
/*Atlas_node Sunn = new Atlas_node_el;
Sunn->body = Sun;
CircleShape sunav(Sun.Radius);
Sunn->avatar = sunav;
Atlas_node Earthn = new Atlas_node_el;
Earthn->body = Earth;
CircleShape earthav(Earth.Radius);
Earthn->avatar = earthav;
Sunn->next = Earthn;
Earthn->next = NULL;
atl.first = Sunn;
atl.last = Earthn;
atl.amount = 2;*/
atl.add(Sun);
atl.add(Earth);
atl.add(Mars);
atl.add(Mercury);
atl.add(Venus);
//atl.add(Jupiter);
//Planet2.setPosition(Mars.x,Mars.y);
Phase_vector v1,v2;
while (window.isOpen())
{
float time = clock.getElapsedTime().asMicroseconds(); //дать прошедшее время в микросекундах
clock.restart(); //перезагружает время
time = T_SCALE * time;
Motion(atl, time, WIDTH);
Event event;
while (window.pollEvent(event))
{
if(event.type == Event::KeyPressed && event.key.code == Keyboard::Escape)
window.close();
}
window.clear();
draw(atl, &window);
window.display();
}
atl.del();
return 0;
}
95 changes: 81 additions & 14 deletions Star_Forge_0.1/method.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,85 @@
#define G 1
#define G 6.67408e-11

#include "celestial_body.h"
#include "math.h"


float x(float x_m, float scale)
{
float res = (x_m * (scale))/(6.084e11) + scale/2;
//std::cout<<"x: "<<
return res;
}

float y(float y_m, float scale)
{
float res = scale/2 - (y_m * scale)/(6.084e11);
return res;
}

float distance(Celestial_Body a, Celestial_Body b)
{
float dist = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
return dist;
}

Phase_vector f(Celestial_Body a, Celestial_Body b)
Phase_vector f(Celestial_Body a, Atlas atl) //Тут координатам соответствуют скорости, а скоростям ускорения
{
float dist = distance(a, b);
float dist;
Atlas_node tmp = atl.first;
Phase_vector res = Phase_vector();
res.x = a.v_x;
res.x = a.v_y;
res.v_x =(G * a.Mass * b.Mass * (b.x - a.x) )/ (dist * dist * dist);
res.v_y = (G * a.Mass * b.Mass * (b.y - a.y)) / (dist * dist * dist);
res.y = a.v_y;
res.v_x = 0;
res.v_y = 0;
while (tmp != NULL)
{
if(a.Mass != (tmp -> body.Mass))
{
dist = distance(a, tmp->body);
res.v_x = res.v_x + (G * tmp->body.Mass * (tmp->body.x - a.x) )/(dist * dist * dist);
res.v_y = res.v_y + (G * tmp->body.Mass * (tmp->body.y - a.y))/(dist * dist * dist);
std::cout<<"v_x"<<res.v_x<<"v_y"<<res.v_y<<"\n";
}
tmp = tmp->next;
}
return res;
}

Phase_vector k1(Celestial_Body a, Celestial_Body b, float t_scale)
Phase_vector k1(Celestial_Body& a, Atlas& atl, float t_scale)
{
return f(a, b);
return f(a, atl);
}

Phase_vector k2(Celestial_Body a, Celestial_Body b, float t_scale, Phase_vector k1)
Phase_vector k2(Celestial_Body& a, Atlas& atl, float t_scale, Phase_vector& k1)
{
return f((a + (k1 * t_scale) / 2), b);//Второе тело неподвижно?
return f((a + (k1 * t_scale) / 2), atl);//Второе тело неподвижно?
}

Phase_vector k3(Celestial_Body a, Celestial_Body b, float t_scale, Phase_vector k2)
Phase_vector k3(Celestial_Body& a, Atlas& atl, float t_scale, Phase_vector& k2)
{
return f((a + (k2 * t_scale) / 2), b);//Второе тело неподвижно?
return f((a + (k2 * t_scale) / 2), atl);//Второе тело неподвижно?
}

Phase_vector k4(Celestial_Body a, Celestial_Body b, float t_scale, Phase_vector k3)
Phase_vector k4(Celestial_Body& a, Atlas& atl, float t_scale, Phase_vector& k3)
{
return f((a + (k3 * t_scale)), b);//Второе тело неподвижно?
return f((a + (k3 * t_scale)), atl);//Второе тело неподвижно?
}

Phase_vector Movement(Celestial_Body& a, Atlas& atl, float t_scale)
{
Phase_vector res = Phase_vector();
Phase_vector k12 = k1(a, atl, t_scale);
std::cout<<"k1"<<k12;
Phase_vector k22 = k2(a, atl, t_scale, k12);
std::cout<<"k2"<<k22;
Phase_vector k32 = k3(a, atl, t_scale, k22);
std::cout<<"k3"<<k32;
Phase_vector k42 = k4(a, atl, t_scale, k32);
std::cout<<"k4"<<k42;
res = (k12 + (k22 * 2) + (k32 * 2) + k42) * (t_scale / 6);
std::cout<<"Movement"<<res;
return res;
}

void Celestial_Body::Move(float t_scale, Phase_vector k1, Phase_vector k2, Phase_vector k3, Phase_vector k4)
Expand All @@ -48,3 +90,28 @@ void Celestial_Body::Move(float t_scale, Phase_vector k1, Phase_vector k2, Phase
v_y = v_y + (k1.v_y + (k2.v_y * 2) + (k3.v_y * 2) + k4.v_y) * (t_scale / 6);
return;
}

Atlas Motion(Atlas atl, float t_scale, float scale)
{
Atlas_node tmp = atl.first;
Phase_vector mov;
while(tmp != NULL)
{
mov = Movement(tmp->body, atl, t_scale);
tmp->body = tmp->body+ mov;
tmp->avatar.setPosition(x(tmp->body.x, scale), y(tmp->body.y, scale));
tmp = tmp->next;
}
return atl;
}

void draw(Atlas atl, RenderWindow* window)
{
Atlas_node tmp = atl.first;
while(tmp != NULL)
{
window->draw(tmp->avatar);
tmp = tmp->next;
}
return;
}
Binary file removed blast.jpg
Binary file not shown.
Loading