From efce4df3807cc24505644dbc360bd5f9fc0b85e0 Mon Sep 17 00:00:00 2001 From: onezplpl Date: Wed, 17 Jul 2024 09:18:33 -0400 Subject: [PATCH] collision test --- linmath.h | 3 +- main.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/linmath.h b/linmath.h index dca11b1..b4fe554 100644 --- a/linmath.h +++ b/linmath.h @@ -42,7 +42,8 @@ return sqrtf(vec##n##_mul_inner(v, v)); \ } \ LINMATH_H_FUNC void vec##n##_norm(vec##n r, vec##n const v) { \ - float k = 1.f / vec##n##_len(v); \ + float l = vec##n##_len(v); \ + float k = 1.f / (l == 0.0 ? 1.0 : l); \ vec##n##_scale(r, v, k); \ } \ LINMATH_H_FUNC void vec##n##_min(vec##n r, vec##n const a, vec##n const b) { \ diff --git a/main.c b/main.c index 5e29735..4c73a8d 100644 --- a/main.c +++ b/main.c @@ -70,7 +70,8 @@ static void cursor_position_callback(GLFWwindow *window, double x, double y) { return; cx = (x - width / 2.0) / -1000.0; cy = (y - height / 2.0) / -1000.0; - cy = (cy > M_PI / 2) ? M_PI / 2 : ((cy < -M_PI / 2) ? -M_PI / 2 : cy); + cy = (cy > M_PI * 0.48) ? M_PI * 0.48 + : ((cy < -M_PI * 0.48) ? -M_PI * 0.48 : cy); return; } @@ -115,8 +116,7 @@ int main(void) { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow *window = - glfwCreateWindow(640, 480, "VoxelTest", NULL, NULL); + GLFWwindow *window = glfwCreateWindow(640, 480, "VoxelTest", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); @@ -196,6 +196,8 @@ int main(void) { pos[1] += (state ? 1 : -1); } + pos[1] += 2.8; + // int x = 0, y = 25, z = 0; glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); @@ -205,6 +207,9 @@ int main(void) { int last_render = 0; + struct chunk chunk = fetch_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH, + pos[2] / CHUNK_LENGTH, 0, 0); + while (!glfwWindowShouldClose(window)) { glfwGetFramebufferSize(window, &width, &height); @@ -218,28 +223,80 @@ int main(void) { mat4x4_identity(m); mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000); - vec3 direction = {cosf(cy) * sinf(cx) * 0.3, sinf(cy) * 0.3, - cosf(cy) * cosf(cx) * 0.3}; - vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.3, 0, - cosf(cx - 3.14f / 2.0f) * 0.3}; + vec3 direction = {cosf(cy) * sinf(cx) * 0.2, sinf(cy) * 0.2, + cosf(cy) * cosf(cx) * 0.2}; + vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.2, 0, + cosf(cx - 3.14f / 2.0f) * 0.2}; - if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { - vec3_add(pos, pos, direction); + vec3 front = {sinf(cx) * 0.2, 0, cosf(cx) * 0.2}; + vec3 dir_temp = {0, 0, 0}; + vec3 pos_temp = {0, 0, 0}; + + vec3_dup(pos_temp, pos); + + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { + vec3_sub(dir_temp, dir_temp, front); + vec3_sub(pos, pos, front); + } + + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { + vec3_add(dir_temp, dir_temp, front); + vec3_add(pos, pos, front); } if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { + vec3_sub(dir_temp, dir_temp, direction); vec3_sub(pos, pos, direction); } + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { + vec3_add(dir_temp, dir_temp, direction); + vec3_add(pos, pos, direction); + } + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + vec3_add(dir_temp, dir_temp, right); vec3_add(pos, pos, right); } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + vec3_sub(dir_temp, dir_temp, right); vec3_sub(pos, pos, right); } + vec3 closestVert = {floor(pos[0] + 0.5), floor(pos[1] + 0.5), + floor(pos[2] + 0.5)}; + + if ((dir_temp[0] != 0 || dir_temp[1] != 0 || dir_temp[2] != 0) && + (cube_exists(pos[0], pos[1], pos[2], chunk, 0) || + cube_exists(pos[0], pos[1] - 1.0, pos[2], chunk, 0) || + cube_exists(pos[0], pos[1] - 1.8, pos[2], chunk, 0))) { + double xd = dir_temp[0] / (closestVert[0] - pos[0]); + double yd = dir_temp[1] / (closestVert[1] - pos[1]); + double zd = dir_temp[2] / (closestVert[2] - pos[2]); + int s = 0; + if (xd < 0.0) { + + pos[0] = closestVert[0]; + } + if (yd < 0.0) { + + pos[1] = closestVert[1]; + } + if (zd < 0.0) { + + pos[2] = closestVert[2]; + } + + // double d = fabs(1.0 / od); + + // vec3_scale(dir_temp, dir_temp, (d > 1.0) ? 1.0 : d); + // vec3_sub(pos_temp, pos, dir_temp); + // vec3_dup(pos, pos_temp); + } + vec3 dir_pos; + vec3_scale(direction, direction, 100.0); vec3_add(dir_pos, direction, pos); vec3 up = {0, 1, 0}; @@ -264,9 +321,9 @@ int main(void) { args.text = &text; args.cubeO = &cubeO; args.textO = &textO; - args.pos[0] = pos[0] + direction[0] * CHUNK_LENGTH; + args.pos[0] = pos[0] + direction[0] / 400.0 * CHUNK_LENGTH; args.pos[1] = pos[1]; - args.pos[2] = pos[2] + direction[2] * CHUNK_LENGTH; + args.pos[2] = pos[2] + direction[2] / 400.0 * CHUNK_LENGTH; args.vertex_buffer = vertex_buffer; args.is_render = &is_render; args.i2 = i2; @@ -294,8 +351,11 @@ int main(void) { glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(struct v3f), cubeO); // cube); - if (thread_id != 0) + if (thread_id != 0) { pthread_join(thread_id, NULL); + chunk = fetch_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH, + pos[2] / CHUNK_LENGTH, 0, 0); + } thread_id = 0; }