worked on block destroying

This commit is contained in:
koma 2022-04-20 15:12:14 +02:00
parent 2096c220d9
commit 135aa8c3f6
7 changed files with 218 additions and 68 deletions

View File

@ -55,12 +55,13 @@ 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);
void chunk_render(Chunk* chunk);
void check_intersection_block(App *app, vec3i *retv);
bool is_block(Block b);
void chunk_update(App *app, Chunk *chunk);
int app_get_current_chunk_index(App *app);
int check_intersection_block(App *app, vec3i *retv, int *chunk_index);
void app_break_block(App *app);
bool is_block(Block b);
bool app_check_collision(App *app);

View File

@ -46,4 +46,7 @@ bool is_oob(int i, int min, int max);
void mult_matrix(float *matrix, vec4f vector, vec4f *ret);
bool matrix_inverse(const float m[16], float invOut[16]);
void vec3f_normalize(vec3f *v);
#endif /* UTILS_H */

View File

@ -111,8 +111,8 @@ void reshape(GLsizei width, GLsizei height)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(
-.08, .08,
-.06, .06,
-.16, .16,
-.09, .09,
.1, 1000
);
}
@ -175,13 +175,11 @@ void handle_events(App* app)
}
break;
case SDL_MOUSEBUTTONDOWN:
is_mouse_down = true;
app_break_block(app);
break;
case SDL_MOUSEMOTION:
SDL_GetMouseState(&x, &y);
if (is_mouse_down) {
rotate_camera(&(app->camera), mouse_x - x, mouse_y - y);
}
mouse_x = x;
mouse_y = y;
break;
@ -198,18 +196,6 @@ void handle_events(App* app)
}
void app_raycast(App *app)
{
vec4f v = {0, 0, -1, 1};
vec4f retv;
float projection[16];
glGetFloatv(GL_PROJECTION_MATRIX, projection);
mult_matrix(projection, v, &retv);
retv.z = -1.0;
retv.w = 0.0;
}
void draw_cube(App *app, float x, float y, float z)
{
/*x*=2;
@ -305,7 +291,7 @@ bool app_check_collision(App *app)
for (int z = 0; z < CHUNK_MAX_Z; z++) {
if (!is_block(app->chunks[i].blocks[x][y][z]))
continue;
if (app->camera.position.z <= z+5)
if (app->camera.position.z <= z+3)
return true;
}
}
@ -313,37 +299,60 @@ bool app_check_collision(App *app)
return false;
}
void check_intersection_block(App *app, vec3i *retv)
int check_intersection_block(App *app, vec3i *retv, int *chunk_index)
{
printf("%s\n", __func__);
int i = app_get_current_chunk_index(app);
*chunk_index = i;
vec3f c_pos = app->camera.position;
vec3f c_dir = get_camera_dir_vec3f(&app->camera);
retv->x = 0;
retv->y = 0;
retv->z = 0;
if (i < 0)
return;
return -1;
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++) {
for (int i = 0; i < 8; i+=0.1) {
vec3f ray = {c_pos.x + c_dir.x * i,
c_pos.y + c_dir.y * i,
c_pos.z + c_dir.z * i};
vec3i rayi = {floor(ray.x/1), floor(ray.y/1), floor(ray.z/1)};
if (app->chunks[i].blocks[x][y][z].visible) {
for (float j = 0; j < 8; j+=0.2) {
vec3f ray = {(c_pos.x - app->chunks[i].start_pos.x) + c_dir.x * j,
(c_pos.y - app->chunks[i].start_pos.y) + c_dir.y * j,
(c_pos.z - app->chunks[i].start_pos.z) + c_dir.z * j};
printf("ray x: %f y: %f z: %f\n", ray.x, ray.y, ray.z);
vec3i rayi = {round(ray.x/1), round(ray.y/1), round(ray.z/1)};
if (is_block(app->chunks[i].blocks[rayi.x][rayi.y][rayi.z])) {
retv->x = ray.x;
retv->y = ray.y;
retv->z = ray.z;
retv->x = rayi.x;
retv->y = rayi.y;
retv->z = rayi.z;
return 0;
}
}
}
}
}
}
return -1;
}
void app_break_block(App *app)
{
printf("%s\n", __func__);
vec3i block;
int i;
int ret = check_intersection_block(app, &block, &i);
printf("chunk index: %d\n", i);
printf("player x: %f y: %f z: %f\n", app->camera.position.x, app->camera.position.y, app->camera.position.z);
printf("chunk %d. min x: %f y: %f max x: %f y: %f\n", i, app->chunks[i].start_pos.x,
app->chunks[i].start_pos.y, app->chunks[i].start_pos.x+CHUNK_MAX_X, app->chunks[i].start_pos.y+CHUNK_MAX_Y);
printf("broken block x: %f y: %f z: %f\n", block.x+app->chunks[i].start_pos.x, block.y+app->chunks[i].start_pos.y, block.z+app->chunks[i].start_pos.z);
if(ret < 0)
return;
app->chunks[i].blocks[block.x][block.y][block.z].type = BLOCKTYPE_AIR;
chunk_update(app, &(app->chunks[i]));
}

View File

@ -40,7 +40,7 @@ void set_view(const Camera* camera)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-(camera->rotation.x), 1.0, 0, 0);
glRotatef(-(camera->rotation.x + 90), 1.0, 0, 0);
glRotatef(-(camera->rotation.z - 90), 0, 0, 1.0);
glTranslatef(-camera->position.x, -camera->position.y, -camera->position.z);
}
@ -67,11 +67,11 @@ void rotate_camera(Camera* camera, double horizontal, double vertical)
}
if (camera->rotation.x < 0.0) {
camera->rotation.x = 0.0;
camera->rotation.x += 360.0;
}
if (camera->rotation.x > 180.0) {
camera->rotation.x = 180.0;
if (camera->rotation.x > 360.0) {
camera->rotation.x -= 360.0;
}
}
@ -90,13 +90,15 @@ void set_camera_side_speed(Camera* camera, double speed)
camera->speed.x = speed;
}
vec3f get_camera_dif_vec3f(Camera *camera)
vec3f get_camera_dir_vec3f(Camera *camera)
{
vec3f dir = {cos(degree_to_radian(camera->rotation.z)) *
cos(degree_to_radian(camera->rotation.x)),
vec3f dir = {cos(degree_to_radian(camera->rotation.x)) *
cos(degree_to_radian(camera->rotation.z)),
sin(degree_to_radian(camera->rotation.z)) *
cos(degree_to_radian(camera->rotation.x)),
sin(degree_to_radian(camera->rotation.x))};
vec3f_normalize(&dir);
return dir;
}

View File

@ -59,3 +59,9 @@ void chunk_render(Chunk* chunk)
{
glCallList(chunk->id);
}
void chunk_update(App *app, Chunk *chunk)
{
chunk_set_blocks_visibility(chunk);
chunk_create_displayl(app, chunk);
}

View File

@ -53,20 +53,10 @@ int main()
chunk_render(&chunks[i]);
}
vec3f dir = {cos(degree_to_radian(app.camera.rotation.z)) *
cos(degree_to_radian(app.camera.rotation.x)),
sin(degree_to_radian(app.camera.rotation.z)) *
cos(degree_to_radian(app.camera.rotation.x)),
sin(degree_to_radian(app.camera.rotation.x))};
// vec3f pos = app.camera.position;
// vec3f dir = get_camera_dir_vec3f(&app.camera);
vec3f pos = {app.camera.position.x, app.camera.position.y, app.camera.position.z };
glLineWidth(10);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(pos.x, pos.y, pos.z);
glVertex3f(pos.x+3*dir.x, pos.y+3*dir.y, pos.z+3*dir.z);
glEnd();
// draw_cube(&app, pos.x + dir.x*3, pos.y + dir.y*3, pos.z + dir.z*3);
SDL_GL_SwapWindow(app.window);
@ -74,10 +64,10 @@ 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);
//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);
//printf("dird x: %f y: %f z: %f\n", app.camera.rotation.x, app.camera.rotation.y, app.camera.rotation.z);
}

View File

@ -1,4 +1,4 @@
#include "utils.h"
#include "../include/utils.h"
#include <math.h>
#include <stdbool.h>
@ -29,3 +29,142 @@ void mult_matrix(float *matrix, vec4f vector, vec4f *ret)
}
ret->x = ret1[0]; ret->y = ret1[1]; ret->z=ret1[2]; ret->w = ret1[3];
}
bool matrix_inverse(const float m[16], float invOut[16])
{
float inv[16], det;
int i;
inv[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];
inv[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];
inv[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];
inv[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];
inv[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];
inv[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];
inv[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];
inv[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];
inv[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];
inv[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];
inv[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];
inv[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];
inv[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];
inv[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];
inv[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];
inv[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0)
return false;
det = 1.0 / det;
for (i = 0; i < 16; i++)
invOut[i] = inv[i] * det;
return true;
}
void vec3f_normalize(vec3f *v)
{
float w = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
v->x /= w;
v->y /= w;
v->z /= w;
}