fix collision bugs
This commit is contained in:
parent
0f38b89e1c
commit
fa7bb205cc
2 changed files with 33 additions and 26 deletions
57
control.c
57
control.c
|
@ -5,6 +5,7 @@
|
|||
#include <GLFW/glfw3.h>
|
||||
|
||||
int is_space;
|
||||
int ticks = 0;
|
||||
|
||||
int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
|
||||
vec3 closestVert = {floor(pos[0]), floor(pos[1] + 0.5), floor(pos[2])};
|
||||
|
@ -13,8 +14,8 @@ int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
|
|||
double yd = dir_temp[1] / (closestVert[1] - (pos[1]));
|
||||
double zd = dir_temp[2] / (closestVert[2] - (pos[2] - 0.5));
|
||||
if (xd < 0 && n == 0) {
|
||||
pos[0] = (closestVert[0] + 0.5);
|
||||
dir_temp[0] = 0;
|
||||
pos[0] = closestVert[0] + 0.5;
|
||||
return 1;
|
||||
}
|
||||
if (yd < 0 && n == 1) {
|
||||
|
@ -23,8 +24,8 @@ int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
|
|||
return 1;
|
||||
}
|
||||
if (zd < 0 && n == 2) {
|
||||
pos[2] = (closestVert[2] + 0.5);
|
||||
dir_temp[2] = 0;
|
||||
pos[2] = closestVert[2] + 0.5;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +37,8 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
|
|||
GLFWwindow *window, struct chunk chunk) {
|
||||
|
||||
double delta = glfwGetTime();
|
||||
if (delta > 1/60.0) delta = 1/60.0;
|
||||
if (delta > 1 / 60.0)
|
||||
delta = 1 / 60.0;
|
||||
glfwSetTime(0);
|
||||
delta *= 30;
|
||||
vec3 direction = {cosf(cy) * sinf(cx), sinf(cy), cosf(cy) * cosf(cx)};
|
||||
|
@ -48,39 +50,22 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
|
|||
|
||||
vec3_dup(pos_temp, pos);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
vec3_sub(dir_temp, dir_temp, front);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
vec3_add(dir_temp, dir_temp, front);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
vec3_add(dir_temp, dir_temp, right);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
vec3_sub(dir_temp, dir_temp, right);
|
||||
}
|
||||
|
||||
vec3_scale(dir_temp, dir_temp, pow(0.9,delta));
|
||||
vec3_scale(dir_temp, dir_temp, pow(0.9, delta));
|
||||
|
||||
vec3 off = {0, 0, 0};
|
||||
int col = 0;
|
||||
for (int a = -2; a < 0; a++) {
|
||||
for (int a = -2; a < 1; a++) {
|
||||
off[1] = a;
|
||||
for (int b = -1; b < 1; b++) {
|
||||
off[0] = (b + 0.5) * 0.95;
|
||||
off[0] = (b + 0.5) * 0.8;
|
||||
for (int c = -1; c < 1; c++) {
|
||||
off[2] = (c + 0.5) * 0.95;
|
||||
off[2] = (c + 0.5) * 0.8;
|
||||
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2],
|
||||
chunk, 0, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
vec3_add(pos, pos, dir_temp);
|
||||
|
||||
dir_temp[1] -= 0.03 * delta;
|
||||
|
@ -99,13 +84,35 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
|
|||
}
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS && !col) {
|
||||
dir_temp[1] = 0.5 * delta;
|
||||
}
|
||||
}
|
||||
|
||||
collision(chunk, pos, dir_temp, col, 0);
|
||||
collision(chunk, pos, dir_temp, col, 2);
|
||||
if (col) {
|
||||
ticks = 4;
|
||||
}
|
||||
ticks--;
|
||||
if (ticks < 0) {
|
||||
ticks = 0;
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
vec3_sub(dir_temp, dir_temp, front);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
vec3_add(dir_temp, dir_temp, front);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
vec3_add(dir_temp, dir_temp, right);
|
||||
}
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
vec3_sub(dir_temp, dir_temp, right);
|
||||
}
|
||||
}
|
||||
|
||||
struct v3f dirOut;
|
||||
vec3_dup(dirOut.pos, direction);
|
||||
|
|
2
main.c
2
main.c
|
@ -271,7 +271,7 @@ int main(void) {
|
|||
mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000);
|
||||
|
||||
vec3 direction;
|
||||
|
||||
|
||||
vec3_dup(direction,
|
||||
control_handler(cx, cy, pos, dir_temp, window, chunk).pos);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue