Advertisement
KeithS

Simple_2D_Orbit

Sep 15th, 2013
1,886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_primitives.h>
  3. #include <math.h>
  4.  
  5. // By request on my C++ blog, here is the source for the simple 2D orbit.
  6.  
  7. const int SCREEN_X = 800;
  8. const int SCREEN_Y = 600;
  9. const float fps = 60.0f;
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13.     const double grav_const = 6.6742e-11;   // The "G" in the formula
  14.     const double earth_mass = 5.975e24;     // The "m" in the formula
  15.     double radius = 6.37814e6;              // The "r" in the formula
  16.     double angle = 0;
  17.     double grav_accel = 0;
  18.     double body_pos_x = 2.4e6;              // An X axis position of the body to be attracted
  19.     double body_pos_y = 7.5e5;              // A Y axis position of the body to be attracted
  20.     double body_vel_x = 0.0;                // Body velocity split into two components, X and Y
  21.     double body_vel_y = 8.5e3;
  22.     double scale = 10000.0;
  23.     bool loop = true;
  24.     // To get different orbit effects, make small changes to the body positions and velocities.
  25.     // It is not advisable to alter the value of the gravitational constant.
  26.     // The program runs at X 60 speed, as the screen update is 60 FPS, and the formulae are
  27.     // for 1 second update. I have left it as such for clarity, though a time factor could
  28.     // easily be added to the calculations to get real time update.
  29.  
  30.     // The following is just Allegro 5 initialization stuff.
  31.     // Replace with your own screen output preferences, as required.
  32.     ALLEGRO_DISPLAY *display = NULL;
  33.     ALLEGRO_TIMER *timer = NULL;
  34.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  35.     al_init();
  36.     al_init_primitives_addon();
  37.     al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
  38.     display = al_create_display(SCREEN_X, SCREEN_Y);
  39.     al_set_window_title(display, *argv);
  40.     al_install_mouse();
  41.     timer = al_create_timer(1.0f / fps);
  42.     event_queue = al_create_event_queue();
  43.     al_register_event_source(event_queue, al_get_display_event_source(display));
  44.     al_register_event_source(event_queue, al_get_mouse_event_source());
  45.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  46.     al_start_timer(timer);
  47.     // Allegro 5 initialization stuff ends here.
  48.     // For practical readability purposes, I have omitted
  49.     // error trapping routines and am taking it on faith
  50.     // that Allegro will initialize properly, though in
  51.     // "real world" applications they should be included.
  52.  
  53.  
  54.     while (loop == true)
  55.     {
  56.         ALLEGRO_EVENT event;
  57.         al_wait_for_event(event_queue, &event);
  58.  
  59.         switch(event.type)
  60.         {
  61.             case ALLEGRO_EVENT_DISPLAY_CLOSE:
  62.                 loop = false;
  63.                 break;
  64.  
  65.             case ALLEGRO_EVENT_TIMER:
  66.                 // The heart of the matter happens here.
  67.                 radius = sqrt(pow(body_pos_x, 2) + pow(body_pos_y, 2));
  68.                 grav_accel = (grav_const * (earth_mass / pow(radius, 2)));
  69.                 angle = atan2(body_pos_x, body_pos_y);
  70.                 body_vel_x += (sin(angle) * grav_accel);
  71.                 body_vel_y += (cos(angle) * grav_accel);
  72.                 body_pos_x -= body_vel_x;
  73.                 body_pos_y -= body_vel_y;
  74.                 // And that is all there is to it.
  75.  
  76.                 // Now, draw calculations to screen.
  77.                 al_clear_to_color(al_map_rgb(0,0,0));
  78.                 al_draw_circle(float(SCREEN_X / 2), float(SCREEN_Y / 2),
  79.                 5, al_map_rgb(235,150,50), 1);
  80.                 al_draw_circle(float(body_pos_x / scale) + float(SCREEN_X / 2),
  81.                                float(body_pos_y / scale) + float(SCREEN_Y / 2),
  82.                                3, al_map_rgb(50, 150, 200), 1);
  83.                 al_flip_display();
  84.                 break;
  85.  
  86.             case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  87.                 loop = false;
  88.                 break;
  89.         }
  90.  
  91.     }
  92.  
  93.     al_stop_timer(timer);
  94.     al_destroy_timer(timer);
  95.     al_destroy_display(display);
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement