From b1fd709a0052bbf9a9875df7b6f88e16f0a54d6c Mon Sep 17 00:00:00 2001 From: Akos Date: Thu, 21 Apr 2022 16:29:05 +0200 Subject: [PATCH] collison changes --- include/camera.h | 8 ++++--- include/utils.h | 4 +++- src/app.c | 62 ++++++++++++++++++++++++++++++++---------------- src/camera.c | 15 +++++++----- 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/include/camera.h b/include/camera.h index b7e7b25..7e86fc3 100644 --- a/include/camera.h +++ b/include/camera.h @@ -8,12 +8,14 @@ /** * Camera, as a moving point with direction */ +struct App; typedef struct Camera { vec3f position; vec3f rotation; vec3f speed; + struct App *app; } Camera; /** @@ -24,7 +26,7 @@ void init_camera(Camera* camera); /** * Update the position of the camera. */ -void update_camera(Camera* camera, double time); +void update_camera(Camera* camera, struct App *app, double time); /** * Apply the camera settings to the view transformation. @@ -40,14 +42,14 @@ void rotate_camera(Camera* camera, double horizontal, double vertical); /** * Set the speed of forward and backward motion. */ -void set_camera_speed(Camera* camera, double speed); +void camera_set_speed(Camera* camera, double speed); void camera_set_vertical_speed(Camera* camera, double speed); /** * Set the speed of left and right side steps. */ -void set_camera_side_speed(Camera* camera, double speed); +void camera_set_side_speed(Camera* camera, double speed); vec3f get_camera_dir_vec3f(Camera* camera); diff --git a/include/utils.h b/include/utils.h index 2a739b4..0ac7d61 100644 --- a/include/utils.h +++ b/include/utils.h @@ -3,7 +3,9 @@ #include #include -#include "app.h" +#define CHUNK_MAX_X 16 +#define CHUNK_MAX_Y 16 +#define CHUNK_MAX_Z 128 /** * GLSL-like three dimensional vector diff --git a/src/app.c b/src/app.c index 234ace9..244aee7 100644 --- a/src/app.c +++ b/src/app.c @@ -65,9 +65,8 @@ void update_app(App* app) { app->camera.speed.z -= GRAVITY * (app->frame_time/1000); - if(app_check_collision(app)) - camera_set_vertical_speed(&app->camera, 0.0); - update_camera(&(app->camera), app->frame_time/1000); + app_check_collision(app); + update_camera(&(app->camera), app, app->frame_time/1000); } void init_opengl() @@ -134,16 +133,16 @@ void handle_events(App* app) app->is_running = false; break; case SDL_SCANCODE_W: - set_camera_speed(&(app->camera), SPEED); + camera_set_speed(&(app->camera), SPEED); break; case SDL_SCANCODE_S: - set_camera_speed(&(app->camera), -SPEED); + camera_set_speed(&(app->camera), -SPEED); break; case SDL_SCANCODE_A: - set_camera_side_speed(&(app->camera), SPEED); + camera_set_side_speed(&(app->camera), SPEED); break; case SDL_SCANCODE_D: - set_camera_side_speed(&(app->camera), -SPEED); + camera_set_side_speed(&(app->camera), -SPEED); break; case SDL_SCANCODE_SPACE: if (app_check_collision(app)) @@ -161,11 +160,11 @@ void handle_events(App* app) switch (event.key.keysym.scancode) { case SDL_SCANCODE_W: case SDL_SCANCODE_S: - set_camera_speed(&(app->camera), 0); + camera_set_speed(&(app->camera), 0); break; case SDL_SCANCODE_A: case SDL_SCANCODE_D: - set_camera_side_speed(&(app->camera), 0); + camera_set_side_speed(&(app->camera), 0); break; case SDL_SCANCODE_SPACE: case SDL_SCANCODE_LSHIFT: @@ -327,17 +326,40 @@ bool app_check_collision(App *app) 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(is_block(app->chunks[i].blocks[x][y][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(is_block(app->chunks[i].blocks[x][y][z])) { + // + // } + // } + // } + //} + + int x = c_index.x; + int y = c_index.y; + int z = c_index.z; + + if (check_index(x-1, y, z)) + if (is_block(app->chunks[i].blocks[x-1][y][z])) + //camera_set_side_speed(&app->camera, 0.0); + if (check_index(x+1, y, z)) + if (is_block(app->chunks[i].blocks[x+1][y][z])) + //camera_set_side_speed(&app->camera, 0.0); + if (check_index(x, y-1, z)) + if (is_block(app->chunks[i].blocks[x][y-1][z])) + //camera_set_speed(&app->camera, 0.0); + if (check_index(x, y+1, z)) + if (is_block(app->chunks[i].blocks[x][y+1][z])) + //camera_set_speed(&app->camera, 0.0); + if (check_index(x, y, z-1)) + if (is_block(app->chunks[i].blocks[x][y][z-1])) + camera_set_vertical_speed(&app->camera, 0.0); + if (check_index(x, y, z+1)) + if (is_block(app->chunks[i].blocks[x][y][z+1])) + camera_set_vertical_speed(&app->camera, 0.0); return false; } diff --git a/src/camera.c b/src/camera.c index 62b3985..5cf5a7c 100644 --- a/src/camera.c +++ b/src/camera.c @@ -18,17 +18,20 @@ void init_camera(Camera* camera) camera->speed.z = 0.0; } -void update_camera(Camera* camera, double time) +void update_camera(Camera* camera, struct App *app, double time) { + int i = app_get_current_chunk_index(app); double angle; double side_angle; angle = degree_to_radian(camera->rotation.z); side_angle = degree_to_radian(camera->rotation.z + 90.0); + + float x = camera->position.x = cos(angle) * camera->speed.y * time; + float y = camera->position.y = sin(angle) * camera->speed.y * time; + float z = camera->position.z = camera->speed.z * time; - camera->position.x += cos(angle) * camera->speed.y * time; - camera->position.y += sin(angle) * camera->speed.y * time; - camera->position.z += camera->speed.z * time; + if(is_block(app->chunks[i].blocks[(int)x][(int)y][(int)z])) camera->position.x += cos(side_angle) * camera->speed.x * time; camera->position.y += sin(side_angle) * camera->speed.x * time; @@ -75,7 +78,7 @@ void rotate_camera(Camera* camera, double horizontal, double vertical) } } -void set_camera_speed(Camera* camera, double speed) +void camera_set_speed(Camera* camera, double speed) { camera->speed.y = speed; } @@ -85,7 +88,7 @@ void camera_set_vertical_speed(Camera* camera, double speed) camera->speed.z = speed; } -void set_camera_side_speed(Camera* camera, double speed) +void camera_set_side_speed(Camera* camera, double speed) { camera->speed.x = speed; }