diff --git a/bin/back b/bin/back index bd7c58b..3d7ffae 100755 Binary files a/bin/back and b/bin/back differ diff --git a/levels/test.txt b/levels/test.txt index bf0588d..dcd511d 100644 --- a/levels/test.txt +++ b/levels/test.txt @@ -1,6 +1,6 @@ 5 5 -..S.. -..214 +..S14 +....0 ...35 .E15. ..... diff --git a/obj/base.o b/obj/base.o index e91f559..7cc3754 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/cars.o b/obj/cars.o index de11045..4747e92 100644 Binary files a/obj/cars.o and b/obj/cars.o differ diff --git a/obj/collisions.o b/obj/collisions.o index e503cd8..d50de6e 100644 Binary files a/obj/collisions.o and b/obj/collisions.o differ diff --git a/obj/display.o b/obj/display.o index 6f740c6..1824e6c 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/main.o b/obj/main.o index eeeb2e9..2099d51 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/structure.o b/obj/structure.o index 09a49d8..00072d0 100644 Binary files a/obj/structure.o and b/obj/structure.o differ diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..d6430eb --- /dev/null +++ b/output.txt @@ -0,0 +1,13 @@ +4 +0 (2 0) (38.73 87.83) +1 (3 0) (12.86 46.67) +2 (2 0) (96.96 38.71) +3 (4 0) (55.58 91.72) +[2] + +5 5 +..S14 +....0 +...35 +.E15. +..... diff --git a/src/base.c b/src/base.c index 749ab3b..1bd7233 100644 --- a/src/base.c +++ b/src/base.c @@ -84,4 +84,12 @@ double distance(ptf p1, ptf p2) { ptf normalize(ptf p) { double nr = norm(p); return (ptf){.fx = p.fx/nr, .fy = p.fy/nr}; +} + +double dot(ptf p1, ptf p2) { + return p1.fx*p2.fx + p1.fy*p2.fy; +} + +ptf add(ptf p1, ptf p2) { + return (ptf){.fx = p1.fx + p2.fx, .fy = p1.fy + p2.fy}; } \ No newline at end of file diff --git a/src/base.h b/src/base.h index 4b426e7..9f67aa9 100644 --- a/src/base.h +++ b/src/base.h @@ -14,5 +14,7 @@ double distance_pt(int x1, int x2, int y1, int y2); double norm(ptf p); double distance(ptf p1, ptf p2); ptf normalize(ptf p); +double dot(ptf p1, ptf p2); +ptf add(ptf p1, ptf p2); #endif \ No newline at end of file diff --git a/src/cars.c b/src/cars.c index 371a0d7..fb9df35 100644 --- a/src/cars.c +++ b/src/cars.c @@ -21,8 +21,8 @@ ptf get_position(int nPl, int nTotPl) { double theta = (2.0*MAX_THETA_SPAWN*nPl/((nTotPl-1==0)?(1):(nTotPl-1)) - 1.0*MAX_THETA_SPAWN); //printf("%d --> %lf", nPl, theta); return (ptf){ - .fx = ROOM_SIZE/2.0+cos(theta*PI/180.0 - PI/2.0*(START_DIR-1))*ROOM_SIZE/3.3, - .fy = ROOM_SIZE/2.0+sin(theta*PI/180.0 - PI/2.0*(START_DIR-1))*ROOM_SIZE/3.3 + .fx = ROOM_SIZE/2.0+cos(theta*PI/180.0 +PI/2.0*(START_DIR+1))*ROOM_SIZE/3.3, + .fy = ROOM_SIZE/2.0+sin(theta*PI/180.0 +PI/2.0*(START_DIR+1))*ROOM_SIZE/3.3 }; } @@ -36,7 +36,7 @@ car* init_car(int nPl, int nTotPl) { res->chy = START_CHX; //res->vel = (ptf){.fx = 0.0, .fy = 0.0}; double theta = (2.0*MAX_THETA_SPAWN*nPl/((nTotPl-1==0)?(1):(nTotPl-1)) - 1.0*MAX_THETA_SPAWN); - res->vel = (ptf){.fx = (rand()%100-50.0)/100.0*2.0*MAX_SPEED, .fy = (rand()%100-50.0)/100.0*2.0*MAX_SPEED}; + res->vel = (ptf){.fx = (rand()%100)/100.0*2.0*MAX_SPEED, .fy = (rand()%100-50.0)/100.0*2.0*MAX_SPEED}; return res; } diff --git a/src/collisions.c b/src/collisions.c index 732ebca..7fbab5d 100644 --- a/src/collisions.c +++ b/src/collisions.c @@ -118,6 +118,43 @@ void move_on_STR_H(int nPl) { } } +void move_on_TURN(int nPl, int cenX, int cenY) { + car* c = players[nPl].c; + ptf prev = (ptf){.fx = c->pos.fx, .fy = c->pos.fy}; + c->pos.fx += c->vel.fx*DT; + c->pos.fy += c->vel.fy*DT; + ptf next = c->pos; + + double prevDist = distance(prev, (ptf){.fx = 1.0*cenX, .fy = 1.0*cenY}); + double nextDist = distance(next, (ptf){.fx = 1.0*cenX, .fy = 1.0*cenY}); + + // inner circle + if(prevDist-PLAYER_R/2 > ROOM_SIZE*DIST_EDGE && nextDist-PLAYER_R/2 <= ROOM_SIZE*DIST_EDGE) { + ptf to_in = normalize((ptf){ + .fx = cenX-prev.fx, + .fy = cenY-prev.fy + }); + double inwards = dot(to_in, c->vel); + c->vel = add(c->vel, (ptf){.fx = -2.0*to_in.fx*inwards, .fy = -2.0*to_in.fy*inwards}); + + c->pos.fx += 2.0*c->vel.fx*DT; + c->pos.fy += 2.0*c->vel.fy*DT; + } + + // outer circle + if(prevDist+PLAYER_R/2 < ROOM_SIZE*(1.0-DIST_EDGE) && nextDist+PLAYER_R/2 >= ROOM_SIZE*(1.0-DIST_EDGE)) { + ptf to_out = normalize((ptf){ + .fx = prev.fx-cenX, + .fy = prev.fy-cenY + }); + double outwards = dot(to_out, c->vel); + c->vel = add(c->vel, (ptf){.fx = -2.0*to_out.fx*outwards, .fy = -2.0*to_out.fy*outwards}); + + c->pos.fx += 2.0*c->vel.fx*DT; + c->pos.fy += 2.0*c->vel.fy*DT; + } +} + void bumpOtherCars(int nPl) { for(int p = 0; p < nPlayers; p++) { if(p != nPl) { @@ -182,28 +219,28 @@ bool updateCar(level* lvl, int nPl) { return true; case TURN_NE: - + move_on_TURN(nPl, ROOM_SIZE, 0); bumpOtherCars(nPl); apply_friction(nPl); updateChunk(nPl); return true; case TURN_NW: - + move_on_TURN(nPl, 0, 0); bumpOtherCars(nPl); apply_friction(nPl); updateChunk(nPl); return true; case TURN_SE: - + move_on_TURN(nPl, ROOM_SIZE, ROOM_SIZE); bumpOtherCars(nPl); apply_friction(nPl); updateChunk(nPl); return true; case TURN_SW: - + move_on_TURN(nPl, 0, ROOM_SIZE); bumpOtherCars(nPl); apply_friction(nPl); updateChunk(nPl); diff --git a/src/display.c b/src/display.c index d1b5c93..d40418d 100644 --- a/src/display.c +++ b/src/display.c @@ -503,16 +503,16 @@ void renderCircles(SDL_Renderer* renderer, level* lvl, int cx, int cy, int range void renderPlayers(SDL_Renderer* renderer, int cx, int cy, int range, int rsize) { for(int p = 0; p < nPlayers; p++) { - if(players[p].c->chx >= cx-range && players[p].c->chx <= cx+range && players[p].c->chy >= cy-range && players[p].c->chy <= cy+range) { + if(players[p].c->chx >= cy-range && players[p].c->chx <= cy+range && players[p].c->chy >= cx-range && players[p].c->chy <= cx+range) { + //printf("[%d]", p); int cox = (WIDTH-rsize)/2+rsize*(players[p].c->chx-cy) + (int)((players[p].c->pos.fx*1.0)/ROOM_SIZE*rsize); int coy = (HEIGHT-rsize)/2+rsize*(players[p].c->chy-cx) + (int)((players[p].c->pos.fy*1.0)/ROOM_SIZE*rsize); - //printf("Abs : (%d %d)\n", players[p].c->chx, players[p].c->chy); - //printf("Relative : (%d : %d)\n", players[p].c->chx-cy, players[p].c->chy-cx); SDL_RenderFillCircle(renderer, cox, coy, PLAYER_R, players[p].rgb.red, players[p].rgb.green, players[p].rgb.blue, 255); } else { // } } + //printf("\n"); } void renderMap(SDL_Renderer* renderer, level* lvl, int cx, int cy, int range, int rsize) { diff --git a/src/main.c b/src/main.c index 47558cf..636b5af 100644 --- a/src/main.c +++ b/src/main.c @@ -77,7 +77,7 @@ int main() { while(!halt) { resetRenderer(rend); - renderMap(rend, test, 1, 2, 2, 250); + renderMap(rend, test, 1, 2, 2, 200); if(elapsed <= 0.7) { placeRectToRenderer(rend, 0, 0, 50, 50, 255, 255, 32, 192); } else if(updateCars(test)) { @@ -87,13 +87,11 @@ int main() { } updateRenderer(rend); elapsed += DT; - //for(int p = 0; p < N_PLAYERS; p++) { - // printf("[%d], (at [%d %d] (%lf %lf) (%lf %lf))\n", p, players[p].c->chx, players[p].c->chy, players[p].c->pos.fx, players[p].c->pos.fy, players[p].c->vel.fx, players[p].c->vel.fy); - //} - //printf("\n"); usleep((int)(1000000*DT)); playerActions(); } + + write_output("output.txt", test, 2); // ---------------------------- // free_digits(digits); diff --git a/src/structure.c b/src/structure.c index b0cc5b8..82e4f8e 100644 --- a/src/structure.c +++ b/src/structure.c @@ -23,4 +23,42 @@ const double BARRIER_WIDTH = 0.05; const double FRICTION = 0.75; const double DV = 1.5; // m/s const double DT = 1.0/100.0; -const double EPSILON = 1.0/4096.0; \ No newline at end of file +const double EPSILON = 1.0/4096.0; + +const double DELTA_V = 0.98; // m/s +const double DELTA_THETA = 5; // degrees + +// ------------------------------------------------------------------------------------------ // + +void write_output(char* stream, level* lvl, int nPl) { + FILE* ptr = fopen(stream, "w"); + fprintf(ptr, "%d\n",nPlayers); + for(int p = 0; p < nPlayers; p++) { + fprintf(ptr, "%d (%d %d) (%.2lf %.2lf)\n", p, players[p].c->chx, players[p].c->chy, players[p].c->pos.fx, players[p].c->pos.fy); + } + fprintf(ptr, "[%d]\n\n", nPl); + fprintf(ptr, "%d %d\n", lvl->lines, lvl->cols); + for(int l = 0; l < lvl->lines; l++) { + for(int c = 0; c < lvl->cols; c++) { + switch (lvl->map[l][c]){ + case NONE: + fprintf(ptr, "."); + break; + + case START: + fprintf(ptr, "S"); + break; + + case END: + fprintf(ptr, "E"); + break; + + default: + fprintf(ptr, "%d", (int)lvl->map[l][c]-3); + break; + } + } + fprintf(ptr, "\n"); + } + fclose(ptr); +} \ No newline at end of file diff --git a/src/structure.h b/src/structure.h index 19f616d..76cc489 100644 --- a/src/structure.h +++ b/src/structure.h @@ -94,4 +94,11 @@ extern const double DV; extern const double DT; extern const double EPSILON; +extern const double DELTA_V; +extern const double DELTA_THETA; + +// -------------------------------------------------------------------------------- // + +void write_output(char* stream, level* lvl, int nPl); + #endif \ No newline at end of file