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
|
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <allegro.h>
//Declairing set variables
double grav_const = 0.00000066742;
double grav_accel = 0;
int m_Xstar = 400;
int m_Ystar = 300;
double m_star_mass = 10000000;
double m_radius_star = 10;
double PI = 3.14159;
class Planet
{
public:
// variables that will change because of physics/ player interaction
char m_strName[25];
int m_nID;
double radius;
double m_Xplanet;
double m_Yplanet;
double distance;
double m_vel_x;
double m_vel_y;
double angle;
double angleraw;
double m_mass_planet;
int m_Xplanet_out;
int m_Yplanet_out;
//planet information and creation
void SetInfo(char *strName, int nID, double vel_x, double vel_y, double Xplanet, double Yplanet, double mass_planet)
{
strncpy(m_strName, strName, 25);
m_nID = nID = nID = nID;
m_vel_x = vel_x;
m_vel_y = vel_y;
m_Xplanet = Xplanet;
m_Yplanet = Yplanet;
m_mass_planet = mass_planet;
}
//Application of newtonian physics
void Apply_Physics(int nID)
{
angleraw = atan2(m_Ystar - m_Yplanet, m_Xstar - m_Xplanet);
angle = angleraw * 180 / PI;
distance = sqrt(((m_Xstar - m_Xplanet)*(m_Xstar - m_Xplanet)) + ((m_Ystar - m_Yplanet)*(m_Ystar - m_Yplanet)));
grav_accel = (grav_const*((m_mass_planet * m_star_mass) / (distance * distance)));
m_vel_x += (sin(angle) * (grav_accel));
m_vel_y += (cos(angle) * (grav_accel));
m_Xplanet = m_Xplanet - (m_vel_x);
m_Yplanet = m_Yplanet - (m_vel_y);
}
//Visual position calculation and creation
void Output_Visual(int nID)
{
BITMAP* buffer=create_bitmap(800,600);
m_Yplanet_out = static_cast<int>(m_Yplanet);
m_Xplanet_out = static_cast<int>(m_Xplanet);
rectfill(buffer,m_Xplanet_out,m_Yplanet_out,m_Xplanet_out+1,m_Yplanet_out+1,makecol(0,255,0));
rectfill(buffer,m_Xstar-3,m_Xstar-3,m_Xstar+3,m_Xstar+3,makecol(255,255,0));
draw_sprite(screen,buffer,0,0);
rectfill(buffer,0,0,800,600,makecol(0,0,0));
rest(10);
}
// Visual debugger
void Output_position(int nID)
{
using namespace std;
cout << "Planet " << nID << " coordinates: (" << m_Xplanet << "," << m_Yplanet<< ")" << endl;
}
};
int main()
{
allegro_init();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0);
install_keyboard();
int loop = 1;
//planet creation and information input place
//-----------------------------------------------------
Planet cMercury;
cMercury.SetInfo("Mercury", 1, 0, 0, 300, 500, 100);
Planet cVenus;
cVenus.SetInfo("Venus", 2, 0, 0, 400, 600, 200);
//-----------------------------------------------------
while(loop == 1)
{
//run physics
cMercury.Apply_Physics(1);
cVenus.Apply_Physics(2);
cMercury.Output_Visual(1);
cVenus.Output_Visual(2);
//system("PAUSE");
//if you remove the "system pause" part, you can see how fast my system calculates planet positions
}
return 0;
}
|