Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro.h>
- #include <math.h>
- #include <stdio.h>
- #include "vector.h"
- const int GFX_HEIGHT = 600, GFX_WIDTH = 800;
- int page_active=1;
- BITMAP *buffer[3];
- float d_time=1;
- int black=makecol(0,100,10),i,j;
- float alfa=90,v,ruch_check=2;
- void init();
- void deinit();
- /////////////////////////
- //#####################//
- /////////////////////////
- struct bila
- {
- int r, color;
- vec poz;
- vec v,v1,v2;
- float tarcie;
- };
- bila bile[7];
- int ilosc=7;
- /////////////////////////
- //#####################//
- /////////////////////////
- void ruch(bila &a)
- {
- a.poz+=a.v*d_time;
- a.v*=a.tarcie;
- a.tarcie-=0.00001;
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- void kolizja(bila &a, bila &b)
- {
- vec S=a.poz + a.v * d_time -( b.poz + b.v * d_time);
- if(S.len()<=a.r+b.r)
- {
- //odbicie od nieruchomej kulki W=S.perp(); n=S.normalize(); N=dot(-(bila.v),n)*n; L=bila.v+N; bila.v=-bila.v+2*L;
- //odbicie dla nieruchomej kulki z nadaniem jej predkosci alfa=acos(dot(bila.v.normalize(),S.normalize())); biala.v=(bila.v.len()*cos(alfa))*S.normalize(); bila.v=bila.v-biala.v;
- vec n = S.normalize();
- vec x1 = dot(a.v,n)*n;
- vec x2 = dot(b.v,n)*n;
- vec p1 = a.v - x1;
- vec p2 = b.v - x2;
- a.v = x2 + p1;
- b.v = x1 + p2;
- a.v*=a.tarcie;
- b.v*=b.tarcie;
- a.tarcie-=0.001;
- b.tarcie-=0.001;
- //ruch(a);
- //ruch(b);
- }
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- void sciana(bila &a)
- {
- if(a.poz.y-a.r<=0)
- {
- //a.v.y*=-1;
- a.v*=a.tarcie;
- a.tarcie-=0.001;
- a.poz.y=0+a.r;
- vec n=(vec(0,0) - vec(60,0)).perp().normalize();
- vec N = dot(-(a.v), n) * n;
- vec L=a.v+N;
- a.v=-a.v+2*L;
- }
- if(a.poz.y+a.r>=600)
- {
- // a.v.y*=-1;
- a.v*=a.tarcie;
- a.tarcie-=0.001;
- a.poz.y=600-a.r;
- vec n=(vec(0,0) - vec(60,0)).perp().normalize();
- vec N = dot(-(a.v), n) * n;
- vec L=a.v+N;
- a.v=-a.v+2*L;
- }
- if(a.poz.x-a.r<=0)
- {
- // a.v.x*=-1;
- a.v*=a.tarcie;
- a.tarcie-=0.001;
- a.poz.x=0+a.r;
- vec n=(vec(0,0) - vec(0,60)).perp().normalize();
- vec N = dot(-(a.v), n) * n;
- vec L=a.v+N;
- a.v=-a.v+2*L;
- }
- if(a.poz.x+a.r>=800)
- {
- //a.v.x*=-1;
- a.v*=a.tarcie;
- a.tarcie-=0.001;
- a.poz.x=800-a.r;
- vec n=(vec(0,0) - vec(0,60)).perp().normalize();
- vec N = dot(-(a.v), n) * n;
- vec L=a.v+N;
- a.v=-a.v+2*L;
- }
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- void rysuj(bila a)
- {
- //acquire_screen();
- circlefill(buffer[page_active],a.poz.x,a.poz.y,a.r,a.color);
- //release_screen();
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- void sila()
- {
- while (!key[KEY_ENTER]&&!key[KEY_ESC])
- {
- vsync();
- //acquire_screen();
- clear_to_color(buffer[page_active],black);
- for(i=0;i<ilosc;i++)
- {
- rysuj(bile[i]);
- }
- line(buffer[page_active],bile[0].poz.x,bile[0].poz.y,bile[0].poz.x+(bile[0].v.x)*5,bile[0].poz.y+bile[0].v.y,makecol(100,100,250));
- if(key[KEY_RIGHT])
- {
- bile[0].v.x++;
- }
- if(key[KEY_LEFT]&&bile[0].v.x>0)
- {
- bile[0].v.x--;
- }
- release_bitmap(screen);
- draw_sprite(screen, buffer[page_active], 0, 0);
- page_active=3-page_active;
- }
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- void kierunek()
- {
- while(!key[KEY_SPACE]&&!key[KEY_ESC])
- {
- vsync();
- //acquire_screen();
- clear_to_color(buffer[page_active],black);
- bile[0].v.x=(bile[0].v.len()*sin((alfa*M_PI)/180));
- bile[0].v.y=(bile[0].v.len()*cos((alfa*M_PI)/180));
- for(i=0;i<ilosc;i++)
- {
- rysuj(bile[i]);
- }
- line(buffer[page_active],bile[0].poz.x,bile[0].poz.y,bile[0].poz.x+(bile[0].v.x)*5,bile[0].poz.y+(bile[0].v.y)*5,makecol(100,100,250));
- if(key[KEY_UP])
- {
- alfa++;
- }
- if(key[KEY_DOWN])
- {
- alfa--;
- }
- release_bitmap(screen);
- draw_sprite(screen, buffer[page_active], 0, 0);
- page_active=3-page_active;
- }
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- void odbijanie()
- {
- while (!key[KEY_ESC]&&ruch_check>=0.01)
- {
- vsync();
- //acquire_screen();
- clear_to_color(buffer[page_active],black);
- for(i=0;i<ilosc;i++)
- {
- for(j=0;j<ilosc;j++)
- {
- if(i!=j)
- {
- kolizja(bile[i],bile[j]);
- }
- }
- ruch(bile[i]);
- sciana(bile[i]);
- rysuj(bile[i]);
- }
- printf("%1.1f\n",bile[0].v.len());
- release_bitmap(screen);
- draw_sprite(screen, buffer[page_active], 0, 0);
- page_active=3-page_active;
- ruch_check=0;
- for(i=0;i<ilosc;i++)
- {
- ruch_check+=bile[i].v.len();
- }
- }
- }
- /////////////////////////
- //#####################//
- /////////////////////////
- /////////////////////////
- //#####################//
- /////////////////////////
- int main()
- {
- init();
- //acquire_screen();
- clear_to_color(screen,black);
- bile[0].v=vec(8,0);
- bile[0].poz=vec(100,300);
- bile[0].color=makecol(255,255,255);
- bile[0].r=10;
- bile[0].tarcie=1;
- bile[1].color=makecol(0,0,255);
- bile[1].poz=vec(300,300);
- bile[1].v=vec(0,0);
- bile[1].r=10;
- bile[1].tarcie=1;
- bile[2].color=makecol(0,255,0);
- bile[2].poz=vec(400,250);
- bile[2].v=vec(0,0);
- bile[2].r=10;
- bile[2].tarcie=1;
- bile[3].color=makecol(255,0,0);
- bile[3].poz=vec(400,350);
- bile[3].v=vec(0,0);
- bile[3].r=10;
- bile[3].tarcie=1;
- bile[4].color=makecol(0,255,255);
- bile[4].poz=vec(500,300);
- bile[4].v=vec(0,0);
- bile[4].r=10;
- bile[4].tarcie=1;
- bile[5].color=makecol(255,255,0);
- bile[5].poz=vec(500,400);
- bile[5].v=vec(0,0);
- bile[5].r=10;
- bile[5].tarcie=1;
- bile[6].color=makecol(255,0,254);
- bile[6].poz=vec(500,200);
- bile[6].v=vec(0,0);
- bile[6].r=10;
- bile[6].tarcie=1;
- black=makecol(0,100,10);
- ruch_check=666;
- while(ilosc==7);
- {
- sila();
- kierunek();
- odbijanie();
- }
- deinit();
- return 0;
- }
- END_OF_MAIN()
- void init() {
- int depth, res;
- allegro_init();
- depth = desktop_color_depth();
- if (depth == 0) depth = 32;
- set_color_depth(depth);
- res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_WIDTH, GFX_HEIGHT, 0, 0);
- buffer[0]=create_video_bitmap(GFX_WIDTH, GFX_HEIGHT);
- buffer[1]=create_video_bitmap(GFX_WIDTH, GFX_HEIGHT);
- buffer[2]=create_video_bitmap(GFX_WIDTH, GFX_HEIGHT);
- if (res != 0) {
- allegro_message(allegro_error);
- exit(-1);
- }
- install_timer();
- install_keyboard();
- install_mouse();
- /* add other initializations here */
- }
- void deinit() {
- clear_keybuf();
- /* add other deinitializations here */
- }
Add Comment
Please, Sign In to add comment