added remaining collisions

This commit is contained in:
Alexandre 2025-05-15 16:33:02 +02:00
parent 245c6a44e4
commit 2b7016df5f
17 changed files with 121 additions and 18 deletions

BIN
bin/back

Binary file not shown.

View File

@ -1,6 +1,6 @@
5 5 5 5
..S.. ..S14
..214 ....0
...35 ...35
.E15. .E15.
..... .....

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

13
output.txt Normal file
View File

@ -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.
.....

View File

@ -85,3 +85,11 @@ ptf normalize(ptf p) {
double nr = norm(p); double nr = norm(p);
return (ptf){.fx = p.fx/nr, .fy = p.fy/nr}; 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};
}

View File

@ -14,5 +14,7 @@ double distance_pt(int x1, int x2, int y1, int y2);
double norm(ptf p); double norm(ptf p);
double distance(ptf p1, ptf p2); double distance(ptf p1, ptf p2);
ptf normalize(ptf p); ptf normalize(ptf p);
double dot(ptf p1, ptf p2);
ptf add(ptf p1, ptf p2);
#endif #endif

View File

@ -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); double theta = (2.0*MAX_THETA_SPAWN*nPl/((nTotPl-1==0)?(1):(nTotPl-1)) - 1.0*MAX_THETA_SPAWN);
//printf("%d --> %lf", nPl, theta); //printf("%d --> %lf", nPl, theta);
return (ptf){ return (ptf){
.fx = ROOM_SIZE/2.0+cos(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 .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->chy = START_CHX;
//res->vel = (ptf){.fx = 0.0, .fy = 0.0}; //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); 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; return res;
} }

View File

@ -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) { void bumpOtherCars(int nPl) {
for(int p = 0; p < nPlayers; p++) { for(int p = 0; p < nPlayers; p++) {
if(p != nPl) { if(p != nPl) {
@ -182,28 +219,28 @@ bool updateCar(level* lvl, int nPl) {
return true; return true;
case TURN_NE: case TURN_NE:
move_on_TURN(nPl, ROOM_SIZE, 0);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(nPl);
return true; return true;
case TURN_NW: case TURN_NW:
move_on_TURN(nPl, 0, 0);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(nPl);
return true; return true;
case TURN_SE: case TURN_SE:
move_on_TURN(nPl, ROOM_SIZE, ROOM_SIZE);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(nPl);
return true; return true;
case TURN_SW: case TURN_SW:
move_on_TURN(nPl, 0, ROOM_SIZE);
bumpOtherCars(nPl); bumpOtherCars(nPl);
apply_friction(nPl); apply_friction(nPl);
updateChunk(nPl); updateChunk(nPl);

View File

@ -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) { void renderPlayers(SDL_Renderer* renderer, int cx, int cy, int range, int rsize) {
for(int p = 0; p < nPlayers; p++) { 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 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); 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); SDL_RenderFillCircle(renderer, cox, coy, PLAYER_R, players[p].rgb.red, players[p].rgb.green, players[p].rgb.blue, 255);
} else { } else {
// //
} }
} }
//printf("\n");
} }
void renderMap(SDL_Renderer* renderer, level* lvl, int cx, int cy, int range, int rsize) { void renderMap(SDL_Renderer* renderer, level* lvl, int cx, int cy, int range, int rsize) {

View File

@ -77,7 +77,7 @@ int main() {
while(!halt) { while(!halt) {
resetRenderer(rend); resetRenderer(rend);
renderMap(rend, test, 1, 2, 2, 250); renderMap(rend, test, 1, 2, 2, 200);
if(elapsed <= 0.7) { if(elapsed <= 0.7) {
placeRectToRenderer(rend, 0, 0, 50, 50, 255, 255, 32, 192); placeRectToRenderer(rend, 0, 0, 50, 50, 255, 255, 32, 192);
} else if(updateCars(test)) { } else if(updateCars(test)) {
@ -87,14 +87,12 @@ int main() {
} }
updateRenderer(rend); updateRenderer(rend);
elapsed += DT; 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)); usleep((int)(1000000*DT));
playerActions(); playerActions();
} }
write_output("output.txt", test, 2);
// ---------------------------- // // ---------------------------- //
free_digits(digits); free_digits(digits);
free_digits(letters); free_digits(letters);

View File

@ -24,3 +24,41 @@ const double FRICTION = 0.75;
const double DV = 1.5; // m/s const double DV = 1.5; // m/s
const double DT = 1.0/100.0; const double DT = 1.0/100.0;
const double EPSILON = 1.0/4096.0; 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);
}

View File

@ -94,4 +94,11 @@ extern const double DV;
extern const double DT; extern const double DT;
extern const double EPSILON; extern const double EPSILON;
extern const double DELTA_V;
extern const double DELTA_THETA;
// -------------------------------------------------------------------------------- //
void write_output(char* stream, level* lvl, int nPl);
#endif #endif