From 1a3b5090a66ff39717c1f45872698ceb896ce0f7 Mon Sep 17 00:00:00 2001 From: Akos Date: Thu, 21 Apr 2022 12:47:54 +0200 Subject: [PATCH] collision changes, world generation fine tuning --- include/app.h | 2 +- include/utils.h | 3 +++ src/app.c | 29 +++++++++++++++++++---------- src/main.c | 8 ++++---- src/utils.c | 11 +++++++++++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/app.h b/include/app.h index 64a576e..8b18a43 100644 --- a/include/app.h +++ b/include/app.h @@ -52,7 +52,7 @@ typedef struct { Chunk *chunks; } App; -void app_generate_world(App *app, int x); +void app_generate_world(App *app, int a); bool chunk_is_block_neighboring_block(Chunk* chunk, vec3i pos); void chunk_set_blocks_visibility(Chunk* chunk); void chunk_create_displayl(App *app, Chunk *chunk); diff --git a/include/utils.h b/include/utils.h index b59b76b..2a739b4 100644 --- a/include/utils.h +++ b/include/utils.h @@ -3,6 +3,7 @@ #include #include +#include "app.h" /** * GLSL-like three dimensional vector @@ -53,4 +54,6 @@ bool matrix_inverse(const float m[16], float invOut[16]); void vec3f_normalize(vec3f *v); int irand_range(int min, int max); + +bool check_index(int x, int y, int z); #endif /* UTILS_H */ diff --git a/src/app.c b/src/app.c index 5f766c5..234ace9 100644 --- a/src/app.c +++ b/src/app.c @@ -269,12 +269,16 @@ void app_generate_world(App *app, int a) c.start_pos.z = 0; for (int x = 0; x < CHUNK_MAX_X; x++) { for (int y = 0; y < CHUNK_MAX_Y; y++) { - printf("x: %d y:%d noise: %f\n", - x, y, (noise_noise2(ptable, x*0.005, y*0.005) + 1) * 0.5 * 60); - int mz = round((noise_noise2(ptable, x*0.005, y*0.005) + 1) * 0.5 * 60); + //printf("x: %d y:%d noise: %f\n", x, y, round((noise_noise2(ptable, + // (c.start_pos.x+x)*0.03, (c.start_pos.y+y)*0.03) + 1) * 0.5 * 15); + int mz = round((noise_noise2(ptable, (c.start_pos.x+x)*0.03, (c.start_pos.y+y)*0.03) + 1) * 0.5 * 15); + if (mz < 0) + mz = 0; + else if (mz > CHUNK_MAX_Z) + mz = CHUNK_MAX_Z-1; for (int z = 0; z < mz; z++) c.blocks[x][y][z].type = BLOCKTYPE_STONE; - for (int z = mz; i < CHUNK_MAX_Z; i++) + for (int z = mz; z < CHUNK_MAX_Z; z++) c.blocks[x][y][z].type = BLOCKTYPE_AIR; } } @@ -320,16 +324,21 @@ bool app_check_collision(App *app) if (i < 0) return false; - for (int x = 0; x < CHUNK_MAX_X; x++) { - for (int y = 0; y < CHUNK_MAX_Y; y++) { - for (int z = 0; z < CHUNK_MAX_Z; z++) { - if (!is_block(app->chunks[i].blocks[x][y][z])) + vec3f c_pos = app->camera.position; + vec3i c_index = { floor(c_pos.x), floor(c_pos.y), floor(c_pos.z) }; + + for (int x = c_index.x-1; x < c_index.x+1; x++) { + for (int y = c_index.y-1; y < c_index.y+1; y++) { + for (int z = c_index.z-1; y < c_index.z+1; z++) { + if(!check_index(x, y, z)) continue; - if (app->camera.position.z <= z+3) - return true; + if(is_block(app->chunks[i].blocks[x][y][z])) { + + } } } } + return false; } diff --git a/src/main.c b/src/main.c index 8ac9d6e..7156457 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ int main() { App app; - init_app(&app, 1280, 720); + init_app(&app, 1600, 900); //Chunk *chunks = malloc(25*sizeof(Chunk)); //memset(chunks, 0, 25*sizeof(Chunk)); @@ -37,7 +37,7 @@ int main() //app.chunks = chunks; //app.chunk_count = 25; - app_generate_world(&app, 5); + app_generate_world(&app, 10); while(app.is_running) { struct timespec start; @@ -53,7 +53,7 @@ int main() for (int i = 0; i < app.chunk_count; i++) { chunk_render(&app.chunks[i]); - printf("c sp: %f\n", app.chunks[i].start_pos.x); + //printf("c sp: %f\n", app.chunks[i].start_pos.x); } // vec3f pos = app.camera.position; @@ -67,7 +67,7 @@ int main() app.frame_time = calc_frame_time(&start, &end); - //printf("fps: %f\n", 1000/app.frame_time); + printf("fps: %f\n", 1000/app.frame_time); //printf("lkat pos x: %f y: %f z: %f\n", pos.x+5*dir.x, pos.y+5*dir.y, pos.z+5*dir.z); //printf("camera x: %f y: %f z: %f\n", pos.x, pos.y, pos.z); //printf("dir x: %f y: %f z: %f\n", dir.x, dir.y, dir.z); diff --git a/src/utils.c b/src/utils.c index 2402228..f599af8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -173,3 +173,14 @@ int irand_range(int min, int max) { return rand() % (max - min + 1) + min; } + +bool check_index(int x, int y, int z) +{ + if (x < 0 || x >= CHUNK_MAX_X) + return false; + if (y < 0 || y >= CHUNK_MAX_Y) + return false; + if (z < 0 || z >= CHUNK_MAX_Z) + return false; + return true; +}