collison changes

This commit is contained in:
Akos Horvath 2022-04-21 16:29:05 +02:00
parent 1a3b5090a6
commit b1fd709a00
4 changed files with 59 additions and 30 deletions

View File

@ -8,12 +8,14 @@
/** /**
* Camera, as a moving point with direction * Camera, as a moving point with direction
*/ */
struct App;
typedef struct Camera typedef struct Camera
{ {
vec3f position; vec3f position;
vec3f rotation; vec3f rotation;
vec3f speed; vec3f speed;
struct App *app;
} Camera; } Camera;
/** /**
@ -24,7 +26,7 @@ void init_camera(Camera* camera);
/** /**
* Update the position of the 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. * 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. * 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); void camera_set_vertical_speed(Camera* camera, double speed);
/** /**
* Set the speed of left and right side steps. * 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); vec3f get_camera_dir_vec3f(Camera* camera);

View File

@ -3,7 +3,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "app.h" #define CHUNK_MAX_X 16
#define CHUNK_MAX_Y 16
#define CHUNK_MAX_Z 128
/** /**
* GLSL-like three dimensional vector * GLSL-like three dimensional vector

View File

@ -65,9 +65,8 @@ void update_app(App* app)
{ {
app->camera.speed.z -= GRAVITY * (app->frame_time/1000); app->camera.speed.z -= GRAVITY * (app->frame_time/1000);
if(app_check_collision(app)) app_check_collision(app);
camera_set_vertical_speed(&app->camera, 0.0); update_camera(&(app->camera), app, app->frame_time/1000);
update_camera(&(app->camera), app->frame_time/1000);
} }
void init_opengl() void init_opengl()
@ -134,16 +133,16 @@ void handle_events(App* app)
app->is_running = false; app->is_running = false;
break; break;
case SDL_SCANCODE_W: case SDL_SCANCODE_W:
set_camera_speed(&(app->camera), SPEED); camera_set_speed(&(app->camera), SPEED);
break; break;
case SDL_SCANCODE_S: case SDL_SCANCODE_S:
set_camera_speed(&(app->camera), -SPEED); camera_set_speed(&(app->camera), -SPEED);
break; break;
case SDL_SCANCODE_A: case SDL_SCANCODE_A:
set_camera_side_speed(&(app->camera), SPEED); camera_set_side_speed(&(app->camera), SPEED);
break; break;
case SDL_SCANCODE_D: case SDL_SCANCODE_D:
set_camera_side_speed(&(app->camera), -SPEED); camera_set_side_speed(&(app->camera), -SPEED);
break; break;
case SDL_SCANCODE_SPACE: case SDL_SCANCODE_SPACE:
if (app_check_collision(app)) if (app_check_collision(app))
@ -161,11 +160,11 @@ void handle_events(App* app)
switch (event.key.keysym.scancode) { switch (event.key.keysym.scancode) {
case SDL_SCANCODE_W: case SDL_SCANCODE_W:
case SDL_SCANCODE_S: case SDL_SCANCODE_S:
set_camera_speed(&(app->camera), 0); camera_set_speed(&(app->camera), 0);
break; break;
case SDL_SCANCODE_A: case SDL_SCANCODE_A:
case SDL_SCANCODE_D: case SDL_SCANCODE_D:
set_camera_side_speed(&(app->camera), 0); camera_set_side_speed(&(app->camera), 0);
break; break;
case SDL_SCANCODE_SPACE: case SDL_SCANCODE_SPACE:
case SDL_SCANCODE_LSHIFT: case SDL_SCANCODE_LSHIFT:
@ -327,17 +326,40 @@ bool app_check_collision(App *app)
vec3f c_pos = app->camera.position; vec3f c_pos = app->camera.position;
vec3i c_index = { floor(c_pos.x), floor(c_pos.y), floor(c_pos.z) }; 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 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 y = c_index.y-1; y < c_index.y+1; y++) {
for (int z = c_index.z-1; y < c_index.z+1; z++) { // for (int z = c_index.z-1; y < c_index.z+1; z++) {
if(!check_index(x, y, z)) // if(!check_index(x, y, z))
continue; // continue;
if(is_block(app->chunks[i].blocks[x][y][z])) { // 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; return false;
} }

View File

@ -18,17 +18,20 @@ void init_camera(Camera* camera)
camera->speed.z = 0.0; 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 angle;
double side_angle; double side_angle;
angle = degree_to_radian(camera->rotation.z); angle = degree_to_radian(camera->rotation.z);
side_angle = degree_to_radian(camera->rotation.z + 90.0); 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; if(is_block(app->chunks[i].blocks[(int)x][(int)y][(int)z]))
camera->position.y += sin(angle) * camera->speed.y * time;
camera->position.z += camera->speed.z * time;
camera->position.x += cos(side_angle) * camera->speed.x * time; camera->position.x += cos(side_angle) * camera->speed.x * time;
camera->position.y += sin(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; camera->speed.y = speed;
} }
@ -85,7 +88,7 @@ void camera_set_vertical_speed(Camera* camera, double speed)
camera->speed.z = 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; camera->speed.x = speed;
} }