fixed clipping when multiple collisions occur at the same time + added pause (U)

This commit is contained in:
Alexandre 2025-02-24 16:43:23 +01:00
parent 18bef7ada8
commit 0ab04bb603
6 changed files with 172 additions and 148 deletions

BIN
bin/back

Binary file not shown.

View File

@ -1,16 +1,17 @@
Blocks : Blocks :
[-16.0, -1.0, -16.0, 32.0, 1.0, 32.0, 0.0, 0.0, 128, 128, 128] [-16.0, -1.0, -16.0, 32.0, 1.0, 32.0, 0.0, 0.0, 128, 128, 128]
[-5.0, 13.0, -5.0, 10.0, 1.0, 10.0, 0.0, 0.0, 255, 255, 255] [-4.0, 10.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 192, 192, 192]
[-1.0, 0.0, -15.0, 2.0, 10.0, 2.0, 0.0, 0.0, 128, 128, 128]
[-1.0, 0.0, 13.0, 2.0, 10.0, 2.0, 0.0, 0.0, 128, 128, 128]
[-15.0, 0.0, -1.0, 2.0, 10.0, 2.0, 0.0, 0.0, 128, 128, 128]
[13.0, 0.0, -1.0, 2.0, 10.0, 2.0, 0.0, 0.0, 128, 128, 128]
Entities: Entities:
[-1.0, 15.0, -1.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 10, 0, 0] [-2.0, 1.0 , -2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 128, 1, 0, 8, 100, 1, 128, 128, 128]
[2.0 , 0.0 , 2.0 , 1.0, 1.0, 1.0, 0.0, 0.0, 255, 255, 255, 1 , 0, 6, hope you like parkour, 255, 192, 192] [-1.5, 1.5 , -1.5, 1.0, 1.0, 1.0, 0.0, 0.0, 192, 192, 192, 1, 0, 7, levels/level_01/, 7, great, 192, 192, 192]
[-2.0, 1.0 , -2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 128, 1 , 0, 8, 50, 1, 128, 128, 128] [-8.0, 0.0, -8.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 1]
[-1.5, 1.5 , -1.5, 1.0, 1.0, 1.0, 0.0, 0.0, 192, 192, 192, 1 , 0, 7, levels/level_01/, 7, great, 192, 192, 192] [-6.0, 2.0, -8.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 1]
[-4.0, 4.0, -8.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 0]
[-4.0, 6.0, -6.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 0]
[-4.0, 8.0, -4.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 1]
[-4.0, 10.0, -2.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 255, 255, 1, 0, 9, 2.0, 2.0, 1]
[-4.0, 11.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 192, 192, 192, 1, 0, 5, 0.0, 0.0, 5.0, 0.0, 0.0, 5.0]
Weight : Weight :
0 0

Binary file not shown.

Binary file not shown.

View File

@ -33,12 +33,15 @@ char* newRoomName;
int newRoomCount; int newRoomCount;
bool switchRoom; bool switchRoom;
double jPress = false; bool jPress = false;
double gPress = false; bool gPress = false;
double rPress = false; bool rPress = false;
bool pPress = false;
unsigned int textures[16]; unsigned int textures[16];
bool paused = false;
void reset_everything(GLFWwindow *window, int count, char* folder) { void reset_everything(GLFWwindow *window, int count, char* folder) {
/*hashtbl_free(visited); /*hashtbl_free(visited);
free_proj(); free_proj();
@ -123,142 +126,154 @@ void processInput(GLFWwindow *window, float dtime) {
// horizontal movement // horizontal movement
bool pressed = false; bool pressed = false;
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { if(!paused) {
if(gamemode == 1) { if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
for(int k = 0; k < 10; k++) { if(gamemode == 1) {
camz -= creative_speed*cos(rot_hz)*cos(rot_vt)/10; for(int k = 0; k < 10; k++) {
camx -= creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy += creative_speed*sin(rot_vt)/10;
if(is_colliding(dtime)) {
camz += creative_speed*cos(rot_hz)*cos(rot_vt)/10;
camx += creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= creative_speed*sin(rot_vt)/10;
k=11;
}
}
} else {
camvz = -speed*cos(rot_hz);
camvx = -speed*sin(rot_hz);
}
pressed = true;
//camvy = vtmult*speed*sin(rot_vt);
}
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
if(gamemode == 1) {
for(int k = 0; k < 10; k++) {
camx -= creative_speed*cos(rot_hz)/10;
camz += creative_speed*sin(rot_hz)/10;
if(is_colliding(dtime)) {
camx += creative_speed*cos(rot_hz)/10;
camz -= creative_speed*sin(rot_hz)/10;
k=11;
}
}
} else {
camvx = -speed*cos(rot_hz);
camvz = speed*sin(rot_hz);
}
pressed = true;
}
if(glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) {
if(gamemode == 1) {
for(int k = 0; k < 10; k++) {
camz += creative_speed*cos(rot_hz)*cos(rot_vt)/10;
camx += creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= creative_speed*sin(rot_vt)/10;
if(is_colliding(dtime)) {
camz -= creative_speed*cos(rot_hz)*cos(rot_vt)/10; camz -= creative_speed*cos(rot_hz)*cos(rot_vt)/10;
camx -= creative_speed*sin(rot_hz)*cos(rot_vt)/10; camx -= creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy += creative_speed*sin(rot_vt)/10; camy += creative_speed*sin(rot_vt)/10;
k=11; if(is_colliding(dtime)) {
camz += creative_speed*cos(rot_hz)*cos(rot_vt)/10;
camx += creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= creative_speed*sin(rot_vt)/10;
k=11;
}
} }
} else {
camvz = -speed*cos(rot_hz);
camvx = -speed*sin(rot_hz);
} }
} else { pressed = true;
camvz = speed*cos(rot_hz); //camvy = vtmult*speed*sin(rot_vt);
camvx = speed*sin(rot_hz);
} }
pressed = true; if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
//camvy = -vtmult*speed*sin(rot_vt); if(gamemode == 1) {
} for(int k = 0; k < 10; k++) {
if(glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
if(gamemode == 1) {
for(int k = 0; k < 10; k++) {
camx += creative_speed*cos(rot_hz)/10;
camz -= creative_speed*sin(rot_hz)/10;
if(is_colliding(dtime)) {
camx -= creative_speed*cos(rot_hz)/10; camx -= creative_speed*cos(rot_hz)/10;
camz += creative_speed*sin(rot_hz)/10; camz += creative_speed*sin(rot_hz)/10;
k=11; if(is_colliding(dtime)) {
camx += creative_speed*cos(rot_hz)/10;
camz -= creative_speed*sin(rot_hz)/10;
k=11;
}
} }
} else {
camvx = -speed*cos(rot_hz);
camvz = speed*sin(rot_hz);
}
pressed = true;
}
if(glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) {
if(gamemode == 1) {
for(int k = 0; k < 10; k++) {
camz += creative_speed*cos(rot_hz)*cos(rot_vt)/10;
camx += creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= creative_speed*sin(rot_vt)/10;
if(is_colliding(dtime)) {
camz -= creative_speed*cos(rot_hz)*cos(rot_vt)/10;
camx -= creative_speed*sin(rot_hz)*cos(rot_vt)/10;
camy += creative_speed*sin(rot_vt)/10;
k=11;
}
}
} else {
camvz = speed*cos(rot_hz);
camvx = speed*sin(rot_hz);
}
pressed = true;
//camvy = -vtmult*speed*sin(rot_vt);
}
if(glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
if(gamemode == 1) {
for(int k = 0; k < 10; k++) {
camx += creative_speed*cos(rot_hz)/10;
camz -= creative_speed*sin(rot_hz)/10;
if(is_colliding(dtime)) {
camx -= creative_speed*cos(rot_hz)/10;
camz += creative_speed*sin(rot_hz)/10;
k=11;
}
}
} else {
camvx = speed*cos(rot_hz);
camvz = -speed*sin(rot_hz);
}
pressed = true;
}
// jumps
if(gamemode == 0 && njumps > 0 && glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
if(jPress) {
camvy = vtmult*speed;
njumps -= 1;
jPress = false;
} }
} else { } else {
camvx = speed*cos(rot_hz); jPress = true;
camvz = -speed*sin(rot_hz); }
if(!pressed) {
camvx *= (1.0-1.0*5.0*((double)dtime));
camvz *= (1.0-1.0*5.0*((double)dtime));
}
// gamemode
if(glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS) {
if(!gPress) {
gPress = true;
gamemode = 1-gamemode;
njumps = 1;
camvx = 0.0;
camvy = 0.0;
camvz = 0.0;
}
} else {
gPress = false;
}
// camera rotation
if(glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
rot_hz -= sensitivity;
}
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
rot_hz += sensitivity;
}
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
rot_vt = maxd(-3.14159/2.0, rot_vt-sensitivity);
}
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
rot_vt = mind(3.14159/2.0, rot_vt+sensitivity);
}
// reset
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if(!isMenuOpen() && !rPress) {
rPress = true;
//reset_everything(window, 4, "levels/level_00/");
//reset_everything(window, 7, "levels/level_01/");
reset_everything(window, 1, "levels/level_02/");
}
} else {
rPress = false;
}
if(!isMenuOpen() && switchRoom) {
//printf("-----\n");
switchRoom = false;
reset_everything(window, newRoomCount, newRoomName);
//free(newRoomName);
} }
pressed = true;
} }
// jumps // pause
if(gamemode == 0 && njumps > 0 && glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS) {
if(jPress) { if(!isMenuOpen() && !pPress) {
camvy = vtmult*speed; pPress = true;
njumps -= 1; paused = !paused;
jPress = false;
} }
} else { } else {
jPress = true; pPress = false;
}
if(!pressed) {
camvx *= (1.0-1.0*5.0*((double)dtime));
camvz *= (1.0-1.0*5.0*((double)dtime));
}
// gamemode
if(glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS) {
if(!gPress) {
gPress = true;
gamemode = 1-gamemode;
njumps = 1;
camvx = 0.0;
camvy = 0.0;
camvz = 0.0;
}
} else {
gPress = false;
}
// camera rotation
if(glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
rot_hz -= sensitivity;
}
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
rot_hz += sensitivity;
}
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
rot_vt = maxd(-3.14159/2.0, rot_vt-sensitivity);
}
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
rot_vt = mind(3.14159/2.0, rot_vt+sensitivity);
}
// reset
if(glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if(!isMenuOpen() && !rPress) {
rPress = true;
reset_everything(window, 4, "levels/level_00/");
//reset_everything(window, 7, "levels/level_01/");
//reset_everything(window, 1, "levels/level_02/");
}
} else {
rPress = false;
}
if(!isMenuOpen() && switchRoom) {
//printf("-----\n");
switchRoom = false;
reset_everything(window, newRoomCount, newRoomName);
//free(newRoomName);
} }
} }
@ -602,12 +617,18 @@ int main_alt() {
if(!isInMenu(window, shaderProgramR)) { if(!isInMenu(window, shaderProgramR)) {
processInput(window, delta); processInput(window, delta);
if(gamemode == 0) { if(!paused) {
movePlayerG(delta); if(gamemode == 0) {
movePlayerG(delta);
}
teleport_on_edge();
update_nearby_entities(delta, 1);
updateProj(delta);
} else {
gl_drawRectAlpha(shaderProgramR, -1.0f, -1.0f, 2.0f, 2.0f, 0, 0, 0, 128);
gl_drawString(shaderProgramR, "game Paused", 0.0f, 0.1f, 0.08f, 128, 128, 255, 0.005f, 0);
gl_drawString(shaderProgramR, "press u to resume", 0.0f, -0.1f, 0.08f, 128, 128, 255, 0.005f, 0);
} }
teleport_on_edge();
update_nearby_entities(delta, 1);
updateProj(delta);
} }
usleep(max(0, interval-(int)(1000000*delta))); usleep(max(0, interval-(int)(1000000*delta)));

View File

@ -18,11 +18,11 @@
double sensitivity = 0.06; double sensitivity = 0.06;
double fov = 90.0; double fov = 90.0;
double creative_speed = 0.5; double creative_speed = 0.5;
double speed = 5.0; double speed = 6.0;
double vtmult = 1.5; double vtmult = 2.25;
double min_dist = 0.4; double min_dist = 0.4;
double friction = 0.3; double friction = 0.3;
double gravity_factor = 9.8; double gravity_factor = 25.0;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
bool is_clipping = false; bool is_clipping = false;
@ -197,7 +197,7 @@ void updateF(cube_0* cb, double dtime) {
double alpha = acos(dot3D(normal, (pt_2d){.x = camvx, .y = camvy, .z = camvz})/normv); double alpha = acos(dot3D(normal, (pt_2d){.x = camvx, .y = camvy, .z = camvz})/normv);
double beta = 3.1415926535 - 2*alpha; double beta = 3.1415926535 - 2*alpha;
double nu = sqrt(2)*normv*sqrt(1-cos(beta)); double nu = normv*sqrt(1-cos(beta));
pt_2d u = (pt_2d){.x = normal.x*nu, .y = normal.y*nu, .z = normal.z*nu}; pt_2d u = (pt_2d){.x = normal.x*nu, .y = normal.y*nu, .z = normal.z*nu};
@ -205,9 +205,9 @@ void updateF(cube_0* cb, double dtime) {
camvy += u.y; camvy += u.y;
camvz += u.z; camvz += u.z;
camvx /= 1.41; //camvx /= 1.41;
camvy /= 1.41; //camvy /= 1.41;
camvz /= 1.41; //camvz /= 1.41;
is_clipping = false; is_clipping = false;
} }
@ -215,6 +215,7 @@ void updateF(cube_0* cb, double dtime) {
} }
bool is_colliding(float dtime) { bool is_colliding(float dtime) {
bool globalCollision = false;
for(int w = -1; w <= 1; w++) { for(int w = -1; w <= 1; w++) {
for(int h = -1; h <= 1; h++) { for(int h = -1; h <= 1; h++) {
room* vstd = hashtbl_find_opt(visited, player_chx+w, player_chy+h); room* vstd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
@ -225,7 +226,7 @@ bool is_colliding(float dtime) {
if(updateForces) { if(updateForces) {
updateF(vstd->map[k], (double)dtime); updateF(vstd->map[k], (double)dtime);
} }
return true; globalCollision = true;
} }
} }
@ -242,7 +243,7 @@ bool is_colliding(float dtime) {
vstd = hashtbl_find_opt(visited, player_chx, player_chy); vstd = hashtbl_find_opt(visited, player_chx, player_chy);
set_player_coords(old_chx, old_chy); set_player_coords(old_chx, old_chy);
resetProj(); resetProj();
return true; globalCollision = true;
} }
} }
@ -262,6 +263,7 @@ bool is_colliding(float dtime) {
remove_entity(vstd->ents, &vstd->ent_memlen, &vstd->ent_len, k); remove_entity(vstd->ents, &vstd->ent_memlen, &vstd->ent_len, k);
is_clipping = false; is_clipping = false;
exists = false; exists = false;
k =- 1;
} }
} }
if(exists && updateForces && vstd->ents[k]->entity_type != 0) { if(exists && updateForces && vstd->ents[k]->entity_type != 0) {
@ -270,14 +272,14 @@ bool is_colliding(float dtime) {
if(exists && (vstd->ents[k]->entity_type == 9 || vstd->ents[k]->entity_type == 4 || vstd->ents[k]->entity_type == 5)) { if(exists && (vstd->ents[k]->entity_type == 9 || vstd->ents[k]->entity_type == 4 || vstd->ents[k]->entity_type == 5)) {
is_clipping = false; is_clipping = false;
} }
return true; globalCollision = true;
} }
} }
} }
} }
} }
} }
return false; return globalCollision;
} }
void movePlayerG(float dtime) { void movePlayerG(float dtime) {