Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- #include <math.h>
- // By request on my C++ blog, here is the source for the simple 2D orbit.
- const int SCREEN_X = 800;
- const int SCREEN_Y = 600;
- const float fps = 60.0f;
- int main(int argc, char *argv[])
- {
- const double grav_const = 6.6742e-11; // The "G" in the formula
- const double earth_mass = 5.975e24; // The "m" in the formula
- double radius = 6.37814e6; // The "r" in the formula
- double angle = 0;
- double grav_accel = 0;
- double body_pos_x = 2.4e6; // An X axis position of the body to be attracted
- double body_pos_y = 7.5e5; // A Y axis position of the body to be attracted
- double body_vel_x = 0.0; // Body velocity split into two components, X and Y
- double body_vel_y = 8.5e3;
- double scale = 10000.0;
- bool loop = true;
- // To get different orbit effects, make small changes to the body positions and velocities.
- // It is not advisable to alter the value of the gravitational constant.
- // The program runs at X 60 speed, as the screen update is 60 FPS, and the formulae are
- // for 1 second update. I have left it as such for clarity, though a time factor could
- // easily be added to the calculations to get real time update.
- // The following is just Allegro 5 initialization stuff.
- // Replace with your own screen output preferences, as required.
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- al_init();
- al_init_primitives_addon();
- al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
- display = al_create_display(SCREEN_X, SCREEN_Y);
- al_set_window_title(display, *argv);
- al_install_mouse();
- timer = al_create_timer(1.0f / fps);
- event_queue = al_create_event_queue();
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_mouse_event_source());
- al_register_event_source(event_queue, al_get_timer_event_source(timer));
- al_start_timer(timer);
- // Allegro 5 initialization stuff ends here.
- // For practical readability purposes, I have omitted
- // error trapping routines and am taking it on faith
- // that Allegro will initialize properly, though in
- // "real world" applications they should be included.
- while (loop == true)
- {
- ALLEGRO_EVENT event;
- al_wait_for_event(event_queue, &event);
- switch(event.type)
- {
- case ALLEGRO_EVENT_DISPLAY_CLOSE:
- loop = false;
- break;
- case ALLEGRO_EVENT_TIMER:
- // The heart of the matter happens here.
- radius = sqrt(pow(body_pos_x, 2) + pow(body_pos_y, 2));
- grav_accel = (grav_const * (earth_mass / pow(radius, 2)));
- angle = atan2(body_pos_x, body_pos_y);
- body_vel_x += (sin(angle) * grav_accel);
- body_vel_y += (cos(angle) * grav_accel);
- body_pos_x -= body_vel_x;
- body_pos_y -= body_vel_y;
- // And that is all there is to it.
- // Now, draw calculations to screen.
- al_clear_to_color(al_map_rgb(0,0,0));
- al_draw_circle(float(SCREEN_X / 2), float(SCREEN_Y / 2),
- 5, al_map_rgb(235,150,50), 1);
- al_draw_circle(float(body_pos_x / scale) + float(SCREEN_X / 2),
- float(body_pos_y / scale) + float(SCREEN_Y / 2),
- 3, al_map_rgb(50, 150, 200), 1);
- al_flip_display();
- break;
- case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
- loop = false;
- break;
- }
- }
- al_stop_timer(timer);
- al_destroy_timer(timer);
- al_destroy_display(display);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement