diff --git a/Circle.cpp b/Circle.cpp index 7a2bd3e..9347e83 100644 --- a/Circle.cpp +++ b/Circle.cpp @@ -226,7 +226,7 @@ bool Overlaps(const Circle& c1 , const Circle& c2) { const double rsq = c1.rad*c1.rad + c2.rad*c2.rad; const double dx = c2.cx - c1.cx; const double dy = c2.cy - c1.cy; - return rsq >= dx*dx + dy*dy; + return rsq > dx*dx + dy*dy; } diff --git a/Circle.hpp b/Circle.hpp index 7c18d68..149e47a 100644 --- a/Circle.hpp +++ b/Circle.hpp @@ -43,6 +43,14 @@ public : inline bool Overlaps(const Circle& c) {return ::Overlaps(*this , c);} inline void Draw(ALLEGRO_COLOR col) { + al_draw_filled_circle(cx , cy , rad , al_map_rgb(0,0,0)); + al_draw_circle(cx , cy , rad , col , 3.0); + Vec2 c(cx,cy); + Vec2 v(vx,vy); + DrawArrow(c , c + v , al_map_rgb(0,0,255)); + } + inline void DrawHollow(ALLEGRO_COLOR col) { +/// al_draw_filled_circle(cx , cy , rad , al_map_rgb(0,0,0)); al_draw_circle(cx , cy , rad , col , 3.0); Vec2 c(cx,cy); Vec2 v(vx,vy); diff --git a/CollTable.cpp b/CollTable.cpp index 9178d59..8f1ca25 100644 --- a/CollTable.cpp +++ b/CollTable.cpp @@ -111,7 +111,7 @@ std::vector CollTable::GetFirstCollisionsEarlierThanDT(double dt) { first = info.dt; } if (info.dt > first) { - break; + continue; } clist.push_back(&ctable[n]); } @@ -208,32 +208,61 @@ void CollTable::RecalculateCollTable() { -void CollTable::UpdateCollisionTableAndResolve(double dt) { - if (dt <= 0.0) {return;} +int CollTable::UpdateCollisionTableAndResolve(double dt) { + int overlapcount = 0; + if (dt <= 0.0) {return 0;} std::vector collisions; double dtrem = dt; do { + overlapcount = 0; RecalculateCollTable(); - collisions = GetFirstCollisionsEarlierThanDT(dt); + collisions = GetFirstCollisionsEarlierThanDT(dtrem); + if (collisions.size()) {printf("%d collisions\n" , (int)collisions.size());} double tfirst = collisions.size()?collisions[0]->dt:dtrem;/// The first collision, or dt if none dtrem -= tfirst; for (unsigned int ci = 0 ; ci < circles.size() ; ++ci) { Circle* c = circles[ci]; if (c && c->active) { - c->Update(tfirst); + + Circle copy = *c; + copy.Update(tfirst); + + for (unsigned int cj = 0 ; cj < circles.size() ; ++cj) { + Circle* c2 = circles[cj]; + if (ci == cj) {continue;} + if (c2 && Overlaps(copy , *c2)) { + printf("Circle %u started to overlap circle %u.\n" , ci , cj); + } + } + + *c = copy; + } } for (unsigned int i = 0 ; i < ctable.size() ; ++i) { CollInfo* cinfo = &ctable[i]; cinfo->dt -= tfirst; } - +/** + for (unsigned int i = 0 ; i < ctable.size() ; ++i) { + CollInfo* cinfo = &ctable[i]; + cinfo->dt -= tfirst; + Circle* c1 = circles[cinfo->circles.first]; + Circle* c2 = circles[cinfo->circles.second]; + if (c1 && c2 && Overlaps(*c1 , *c2)) { + overlapcount++; + } + } +*/ for (unsigned int cl = 0 ; cl < collisions.size() ; ++cl) { CollInfo* cinfo = collisions[cl]; + if (cinfo->dt != 0.0) { + printf("Houston we have a problem.\n"); + } if (cinfo->dt == 0.0) { - printf("collision\n"); +// printf("collision\n"); Circle* c1 = circles[cinfo->circles.first]; Circle* c2 = circles[cinfo->circles.second]; if (cresolver) { @@ -246,6 +275,7 @@ void CollTable::UpdateCollisionTableAndResolve(double dt) { } dt = dtrem; } while ((dt > 0.0) && collisions.size()); + return overlapcount; } diff --git a/CollTable.hpp b/CollTable.hpp index c1be94c..e06b431 100644 --- a/CollTable.hpp +++ b/CollTable.hpp @@ -67,7 +67,7 @@ public : void RecalculateCollTable(); - void UpdateCollisionTableAndResolve(double dt); + int UpdateCollisionTableAndResolve(double dt); void MarkDirty(); diff --git a/Main.cpp b/Main.cpp index 4d1e595..b74963d 100644 --- a/Main.cpp +++ b/Main.cpp @@ -16,8 +16,8 @@ #include "Vector2.hpp" #include "Arrow.hpp" -const int sw = 800; -const int sh = 600; +const int sw = 1024; +const int sh = 768; const double DT = 1.0/60.0; @@ -42,7 +42,9 @@ int main(int argc , char** argv) { ALLEGRO_FONT* f = al_load_ttf_font("Verdana.ttf" , -36 , 0); - if (!d || !q || !t) { + ALLEGRO_BITMAP* buf = al_create_bitmap(sw,sh); + + if (!d || !q || !t || !f || !buf) { printf("Setup incomplete.\n"); return 1; } @@ -91,10 +93,12 @@ int main(int argc , char** argv) { int msx = 0; int msy = 0; + int ticks = 0; bool lmb = false; bool rmb = false; bool quit = false; bool redraw = true; + bool clear = true; al_show_mouse_cursor(d); @@ -102,15 +106,24 @@ int main(int argc , char** argv) { while (!quit) { if (redraw) { - al_set_target_backbuffer(d); - al_clear_to_color(al_map_rgb(0,0,0)); + al_set_target_bitmap(buf); + if (clear) { + al_clear_to_color(al_map_rgb(0,0,0)); + } /// Draw board here + int ec = 0; for (unsigned int i = 0 ; i < 4 ; ++i) { Circle* c = &circvec[i]; - c->Draw(al_map_rgb(255,255,255)); + c->DrawHollow(al_map_rgb(255,255,255)); } + for (unsigned int j = 4 ; j < NC ; ++j) { + Circle* c = &circvec[j]; + if (c->cx < 0.0 || c->cx > sw || c->cy < 0.0 || c->cy > sh) { + ++ec; + } + } Vec2 c(sw/2,sh/2); DrawArrow(c , p1 , al_map_rgb(255,255,255)); DrawArrow(c , p2 , al_map_rgb(0,255,0)); @@ -120,11 +133,26 @@ int main(int argc , char** argv) { Circle* c = &circvec[i]; if (c->active) { c->Draw(al_map_rgb(0,255,0)); + al_draw_textf(f , al_map_rgb(0,255,0) , c->cx , c->cy - al_get_font_line_height(f)/2 , ALLEGRO_ALIGN_CENTRE , "%u" , i); + } + for (unsigned int j = 0 ; j < NC ; ++j) { + if (i == j) {continue;} + Circle* c2 = &circvec[j]; + if (c && c2 && Overlaps(*c , *c2)) { + c->Draw(al_map_rgb(255,0,0)); + c2->Draw(al_map_rgb(255,0,0)); + al_draw_textf(f , al_map_rgb(0,0,255) , c->cx , c->cy - al_get_font_line_height(f)/2 , ALLEGRO_ALIGN_CENTRE , "%u" , i); + } } } + + al_draw_textf(f , al_map_rgb(0,255,255) , sw/2 , 10 , ALLEGRO_ALIGN_CENTER , "Escape count %d" , ec); + al_set_target_backbuffer(d); + al_draw_bitmap(buf , 0 , 0 , 0); al_flip_display(); redraw = false; + ticks = 0; } do { ALLEGRO_EVENT ev; @@ -135,11 +163,18 @@ int main(int argc , char** argv) { if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { quit = true; } + if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_R) { + clear = !clear; + redraw = true; + } if (ev.type == ALLEGRO_EVENT_TIMER) { if (lmb) { } - ctable.UpdateCollisionTableAndResolve(DT); redraw = true; + ++ticks; + if (ticks == 1) { + ctable.UpdateCollisionTableAndResolve(DT); + } } if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {