Skip to content

Commit

Permalink
No escape for you
Browse files Browse the repository at this point in the history
  • Loading branch information
EdgarReynaldo committed Nov 9, 2018
1 parent 94273a5 commit 2e72f6c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Circle.cpp
Expand Up @@ -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;
}


Expand Down
8 changes: 8 additions & 0 deletions Circle.hpp
Expand Up @@ -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);
Expand Down
44 changes: 37 additions & 7 deletions CollTable.cpp
Expand Up @@ -111,7 +111,7 @@ std::vector<CollInfo*> CollTable::GetFirstCollisionsEarlierThanDT(double dt) {
first = info.dt;
}
if (info.dt > first) {
break;
continue;
}
clist.push_back(&ctable[n]);
}
Expand Down Expand Up @@ -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<CollInfo*> 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) {
Expand All @@ -246,6 +275,7 @@ void CollTable::UpdateCollisionTableAndResolve(double dt) {
}
dt = dtrem;
} while ((dt > 0.0) && collisions.size());
return overlapcount;
}


Expand Down
2 changes: 1 addition & 1 deletion CollTable.hpp
Expand Up @@ -67,7 +67,7 @@ public :

void RecalculateCollTable();

void UpdateCollisionTableAndResolve(double dt);
int UpdateCollisionTableAndResolve(double dt);

void MarkDirty();

Expand Down
49 changes: 42 additions & 7 deletions Main.cpp
Expand Up @@ -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;


Expand All @@ -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;
}
Expand Down Expand Up @@ -91,26 +93,37 @@ 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);

al_start_timer(t);

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));
Expand All @@ -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;
Expand All @@ -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) {

Expand Down

0 comments on commit 2e72f6c

Please sign in to comment.