some collision and control fixes

This commit is contained in:
onezplpl 2024-07-18 20:30:55 -04:00
parent 742d655a5f
commit 2c3b354e41
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3

View file

@ -7,15 +7,14 @@
int is_space; int is_space;
int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) { int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
vec3 closestVert = {floor(pos[0] + 0.5), floor(pos[1] + 0.5), vec3 closestVert = {floor(pos[0]), floor(pos[1] + 0.5), floor(pos[2])};
floor(pos[2] + 0.5)};
if (col) { if (col) {
double xd = dir_temp[0] / (closestVert[0] - (pos[0])); double xd = dir_temp[0] / (closestVert[0] - (pos[0] - 0.5));
double yd = dir_temp[1] / (closestVert[1] - (pos[1])); double yd = dir_temp[1] / (closestVert[1] - (pos[1]));
double zd = dir_temp[2] / (closestVert[2] - (pos[2])); double zd = dir_temp[2] / (closestVert[2] - (pos[2] - 0.5));
if (xd < 0 && n == 0) { if (xd < 0 && n == 0) {
dir_temp[0] = 0; dir_temp[0] = 0;
pos[0] = closestVert[0]; pos[0] = closestVert[0] + 0.5;
return 1; return 1;
} }
if (yd < 0 && n == 1) { if (yd < 0 && n == 1) {
@ -25,7 +24,7 @@ int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
} }
if (zd < 0 && n == 2) { if (zd < 0 && n == 2) {
dir_temp[2] = 0; dir_temp[2] = 0;
pos[2] = closestVert[2]; pos[2] = closestVert[2] + 0.5;
return 1; return 1;
} }
} }
@ -35,12 +34,11 @@ int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) {
struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp, struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
GLFWwindow *window, struct chunk chunk) { GLFWwindow *window, struct chunk chunk) {
vec3 direction = {cosf(cy) * sinf(cx) * 0.1, sinf(cy) * 0.1, vec3 direction = {cosf(cy) * sinf(cx), sinf(cy), cosf(cy) * cosf(cx)};
cosf(cy) * cosf(cx) * 0.1}; vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.015, 0,
vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.1, 0, cosf(cx - 3.14f / 2.0f) * 0.015};
cosf(cx - 3.14f / 2.0f) * 0.1};
vec3 front = {sinf(cx) * 0.1, 0, cosf(cx) * 0.1}; vec3 front = {sinf(cx) * 0.015, 0, cosf(cx) * 0.015};
vec3 pos_temp = {0, 0, 0}; vec3 pos_temp = {0, 0, 0};
vec3_dup(pos_temp, pos); vec3_dup(pos_temp, pos);
@ -61,36 +59,42 @@ struct v3f control_handler(double cx, double cy, vec3 pos, vec3 dir_temp,
vec3_sub(dir_temp, dir_temp, right); vec3_sub(dir_temp, dir_temp, right);
} }
vec3_scale(dir_temp, dir_temp, 0.6); vec3_scale(dir_temp, dir_temp, 0.9);
vec3 off = {0, 0, 0}; vec3 off = {0, 0, 0};
int col = 0; int col = 0;
for (int a = -2; a < 0; a++) { for (int a = -2; a < 0; a++) {
off[1] = a; off[1] = a;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2], for (int b = -1; b < 1; b++) {
chunk, 0, -1); off[0] = b + 0.5;
for (int c = -1; c < 1; c++) {
off[2] = c + 0.5;
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); vec3_add(pos, pos, dir_temp);
dir_temp[1] -= 0.05; dir_temp[1] -= 0.03;
if (collision(chunk, pos, dir_temp, col, 1)) { if (collision(chunk, pos, dir_temp, col, 1)) {
col = 0; col = 0;
for (int a = -2; a < 0; a++) {
off[1] = a; off[1] = -2;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2], for (int b = -1; b < 1; b++) {
chunk, 0, -1); off[0] = b + 0.5;
for (int c = -1; c < 1; c++) {
off[2] = c + 0.5;
col = col || gen_cube(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2],
chunk, 0, -1);
}
} }
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
is_space = 3; dir_temp[1] = 0.5;
} }
} }
if (is_space > 0) {
dir_temp[1] = 0.8;
}
is_space--;
collision(chunk, pos, dir_temp, col, 0); collision(chunk, pos, dir_temp, col, 0);
collision(chunk, pos, dir_temp, col, 2); collision(chunk, pos, dir_temp, col, 2);
@ -153,7 +157,9 @@ void control_lclick(double cx, double cy, vec3 pos) {
struct chunk placeChunk; struct chunk placeChunk;
int cube = gen_cube(highlighted.pos[0], highlighted.pos[1], int cube = gen_cube(highlighted.pos[0], highlighted.pos[1],
highlighted.pos[2], placeChunk, 0, -1); highlighted.pos[2], placeChunk, 0, -1);
if (cube == 0) if (cube == 0 ||
(highlighted.pos[0] == (int)pos[0] && highlighted.pos[1] == (int)pos[1] &&
highlighted.pos[2] == (int)pos[2]))
return; return;
gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2], gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2],
placeChunk, 0, 0); placeChunk, 0, 0);
@ -164,7 +170,8 @@ void control_rclick(double cx, double cy, vec3 pos) {
struct chunk placeChunk; struct chunk placeChunk;
int cube = gen_cube(highlighted.pos[0], highlighted.pos[1], int cube = gen_cube(highlighted.pos[0], highlighted.pos[1],
highlighted.pos[2], placeChunk, 0, -1); highlighted.pos[2], placeChunk, 0, -1);
if (cube != 0) if (cube != 0 ||
(fabs(highlighted.pos[0] - pos[0]) < 1 && fabs(highlighted.pos[1] - pos[1] + 0.5) < 1.5 && fabs(highlighted.pos[2] - pos[2]) < 1))
return; return;
gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2], gen_cube(highlighted.pos[0], highlighted.pos[1], highlighted.pos[2],
placeChunk, 0, 1); placeChunk, 0, 1);