fixed collisions

This commit is contained in:
Alexandre 2024-08-24 15:19:33 +02:00
parent 2c35a2d6a9
commit 5d80d12382
7 changed files with 77 additions and 29 deletions

BIN
bin/back

Binary file not shown.

View File

@ -196,5 +196,5 @@ void drawHashToRenderer(SDL_Renderer* renderer, int chx, int chy, int xmin, int
void drawMapToRenderer(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax) { void drawMapToRenderer(SDL_Renderer* renderer, int xmin, int xmax, int ymin, int ymax) {
drawHashToRenderer(renderer, player_cx, player_cy, xmin, xmax, ymin, ymax, 0, 0); drawHashToRenderer(renderer, player_cx, player_cy, xmin, xmax, ymin, ymax, 0, 0);
placeRectToRenderer(renderer, __width__ /2 - 5, __height__ /2 + 5, 10, 10, 255, 255, 32, SDL_ALPHA_OPAQUE); placeRectToRenderer(renderer, __width__ /2 - 5*zoom/100, __height__ /2 - 5*zoom/100, 10*zoom/100, 10*zoom/100, 255, 255, 32, SDL_ALPHA_OPAQUE);
} }

View File

@ -43,7 +43,7 @@ int main(int argc, char** argv) {
/* -------------------------------------------------------- */ /* -------------------------------------------------------- */
//one_debug(); //one_debug();
parse_configs("templates.txt", 48); parse_configs("templates.txt", 47);
initialize(rend); initialize(rend);

View File

@ -21,24 +21,75 @@
double speed = 0.5 ; double speed = 0.5 ;
int precision = 12 ; int precision = 12 ;
double epsilon_x = 0.0; double εx = 0.0;
double epsilon_y = 0.0; double εy = 0.0;
bool checkCollision() { bool checkCollision() {
return (unpack_coord(gridGet(map, player_cx, player_cy)->chdata.lines, player_x, player_y)); return (unpack_coord(gridGet(map, player_cx, player_cy)->chdata.lines, player_x, player_y));
} }
bool checkCollision2(int cx, int cy, int x, int y) {
return (unpack_coord(gridGet(map, cx, cy)->chdata.lines, x, y));
}
void normalize(int* ch_coord, int* coord, double* ε) {
if(*ε >= 1.0) {
*ε -= 1.0 ;
*coord += 1 ;
if(*coord >= 8) {
*coord -= 8 ;
*ch_coord += 1;
}
} else if(*ε < 0.0) {
*ε += 1.0 ;
*coord -= 1 ;
if(*coord < 0) {
*coord += 8 ;
*ch_coord -= 1;
}
}
}
bool checkCollisionSquare(double square_size) {
double εxmin = εx - square_size;
double εxmax = εx + square_size;
double εymin = εy - square_size;
double εymax = εy + square_size;
int xmin = player_x ;
int xmax = player_x ;
int ymin = player_y ;
int ymax = player_y ;
int cxmin = player_cx ;
int cxmax = player_cx ;
int cymin = player_cy ;
int cymax = player_cy ;
normalize(&cxmin, &xmin, &εxmin);
normalize(&cxmax, &xmax, &εxmax);
normalize(&cymin, &ymin, &εymin);
normalize(&cymax, &ymax, &εymax);
return (
checkCollision2(cxmin, cymin, xmin, ymin) ||
checkCollision2(cxmin, cymax, xmin, ymax) ||
checkCollision2(cxmax, cymin, xmax, ymin) ||
checkCollision2(cxmax, cymax, xmax, ymax)
);
}
void increment_x(double dx) { void increment_x(double dx) {
epsilon_x = epsilon_x + dx ; εx = εx + dx ;
if(epsilon_x >= 1.0) { if(εx >= 1.0) {
epsilon_x -= 1.0 ; εx -= 1.0 ;
player_x += 1 ; player_x += 1 ;
if(player_x >= 8) { if(player_x >= 8) {
player_x -= 8 ; player_x -= 8 ;
player_cx += 1; player_cx += 1;
} }
} else if(epsilon_x < 0.0) { } else if(εx < 0.0) {
epsilon_x += 1.0 ; εx += 1.0 ;
player_x -= 1 ; player_x -= 1 ;
if(player_x < 0) { if(player_x < 0) {
player_x += 8 ; player_x += 8 ;
@ -48,16 +99,16 @@ void increment_x(double dx) {
} }
void increment_y(double dy) { void increment_y(double dy) {
epsilon_y += dy ; εy += dy ;
if(epsilon_y >= 1.0) { if(εy >= 1.0) {
epsilon_y -= 1.0 ; εy -= 1.0 ;
player_y += 1 ; player_y += 1 ;
if(player_y >= 8) { if(player_y >= 8) {
player_y -= 8 ; player_y -= 8 ;
player_cy += 1; player_cy += 1;
} }
} else if(epsilon_y < 0.0) { } else if(εy < 0.0) {
epsilon_y += 1.0 ; εy += 1.0 ;
player_y -= 1 ; player_y -= 1 ;
if(player_y < 0) { if(player_y < 0) {
player_y += 8 ; player_y += 8 ;
@ -69,7 +120,7 @@ void increment_y(double dy) {
bool moveRequest(double dx, double dy) { bool moveRequest(double dx, double dy) {
increment_x(dx); increment_x(dx);
increment_y(dy); increment_y(dy);
if(false && checkCollision()) { if(checkCollisionSquare(0.15)) {
increment_x(-dx); increment_x(-dx);
increment_y(-dy); increment_y(-dy);
return false; return false;
@ -125,13 +176,13 @@ void moveFunctionMaster(SDL_Renderer* renderer) {
while(!halt) { while(!halt) {
moveControl(&halt); moveControl(&halt);
resetRenderer(renderer); resetRenderer(renderer);
drawMapToRenderer(renderer, -300 * 250/zoom + to_int(50 * (8*player_cx + player_x + epsilon_x)), 300 * 250/zoom + to_int(50 * (8*player_cx + player_x + epsilon_x)), -300 * 250/zoom + to_int(50 * (8*player_cy + player_y + epsilon_y)), 300 * 250/zoom + to_int(50 * (8*player_cy + player_y + epsilon_y))); drawMapToRenderer(renderer, -300 * 250/zoom + to_int(50 * (8*player_cx + player_x + εx)), 300 * 250/zoom + to_int(50 * (8*player_cx + player_x + εx)), -300 * 250/zoom + to_int(50 * (8*player_cy + player_y + εy)), 300 * 250/zoom + to_int(50 * (8*player_cy + player_y + εy)));
drawNumberToRenderer(renderer, digits, player_cx, 10, 10, 50, 70, 0); drawNumberToRenderer(renderer, digits, player_cx, 10, 10, 50, 70, 0);
drawNumberToRenderer(renderer, digits, player_cy, 10, 80, 50, 70, 0); drawNumberToRenderer(renderer, digits, player_cy, 10, 80, 50, 70, 0);
drawNumberToRenderer(renderer, digits, player_x, __width__ / 3, 10, 50, 70, 0); drawNumberToRenderer(renderer, digits, player_x, __width__ / 3, 10, 50, 70, 0);
drawNumberToRenderer(renderer, digits, player_y, __width__ / 3, 80, 50, 70, 0); drawNumberToRenderer(renderer, digits, player_y, __width__ / 3, 80, 50, 70, 0);
drawNumberToRenderer(renderer, digits, to_int(epsilon_x*100), 2 * __width__ / 3, 10, 50, 70, 0); drawNumberToRenderer(renderer, digits, to_int(εx*100), 2 * __width__ / 3, 10, 50, 70, 0);
drawNumberToRenderer(renderer, digits, to_int(epsilon_y*100), 2 * __width__ / 3, 80, 50, 70, 0); drawNumberToRenderer(renderer, digits, to_int(εy*100), 2 * __width__ / 3, 80, 50, 70, 0);
updateRenderer(renderer); updateRenderer(renderer);
draw_par += 1 ; draw_par += 1 ;
usleep(20000); usleep(20000);

View File

@ -3,6 +3,12 @@
bool checkCollision(); bool checkCollision();
bool checkCollision2(int cx, int cy, int x, int y);
void normalize(int* ch_coord, int* coord, double* ε);
bool checkCollisionSquare(double square_size);
void increment_x(double dx); void increment_x(double dx);
void increment_y(double dy); void increment_y(double dy);

View File

@ -22,8 +22,8 @@ extern int render_distance ;
extern double speed ; extern double speed ;
extern int precision ; extern int precision ;
extern double epsilon_x ; extern double εx ;
extern double epsilon_y ; extern double εy ;
// are in [0.0, 1.0[ // are in [0.0, 1.0[
extern int player_x ; extern int player_x ;

View File

@ -223,15 +223,6 @@
0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0
1 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1
1 1 1 0 0 1 1 1
0 0 1 0 0 1 1 1
1 0 0 0 0 0 0 1
1 0 0 1 1 0 0 0
1 0 0 1 1 0 0 0
1 0 0 0 0 0 0 1
0 0 1 0 0 1 1 1
1 1 1 0 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 1 0 0 1 1 1 0 0 1 0 0 1 1 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1