2004-08-16 03:54:38 UTC
previous
next
#include "allegro.h"
#include "time.h"
#include <math.h>
BITMAP *buffer;
struct body{
float x,y,xs,ys,mass;
};
int main(void){
int num=2,i,j;
float G=100.000,fx,fy,a;
body b[10];
// Start allegro
allegro_init();
install_keyboard();
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
return 1;
}
srand(time(0));
set_palette(desktop_palette);
buffer = create_bitmap(640,480);
clear_to_color(buffer, makecol(0, 0, 0));
b[0].x=320;
b[0].y=200;
b[0].xs=0;//(float)(rand()%100-50)/50.00;
b[0].ys=-.5;//(float)(rand()%100-50)/50.00;
b[0].mass=5;
b[1].x=400;
b[1].y=200;
b[1].xs=0;//(float)(rand()%100-50)/50.00;
b[1].ys=2.5;//(float)(rand()%100-50)/50.00;
b[1].mass=1;
//b[2].x=320;
//b[2].y=400;
//b[2].xs=.3;//(float)(rand()%100-50)/50.00;
//b[2].ys=0;//(float)(rand()%100-50)/50.00;
//b[2].mass=.5;
while(!key[KEY_ESC]){
for(i=0;i<num;i++){
fx=0;
fy=0;
for(j=0;j<num;j++){
if(i!=j){
a=atan2(b[i].y-b[j].y,b[i].x-b[j].x);
fx-=cos(a)*G*b[i].mass*b[j].mass/(pow(b[i].x-b[j].x,2)+pow(b[i].y-b[j].y,2));
fy-=sin(a)*G*b[i].mass*b[j].mass/(pow(b[i].x-b[j].x,2)+pow(b[i].y-b[j].y,2));
}
}
b[i].xs+=fx/b[i].mass;
b[i].ys+=fy/b[i].mass;
}
for(i=0;i<num;i++){
circlefill(buffer,(int)b[i].x,(int)b[i].y,21,makecol(0,0,0));
b[i].x+=b[i].xs;
b[i].y+=b[i].ys;
circlefill(buffer,(int)b[i].x,(int)b[i].y,20,makecol(255,255,255));
}
blit(buffer,screen,0,0,0,0,640,480);
}
exit(0);
};
END_OF_MAIN();