collision and motion test (very scuffed)

This commit is contained in:
onezplpl 2024-07-17 10:43:09 -04:00
parent efce4df380
commit 255bb51373
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3

98
main.c
View file

@ -106,6 +106,33 @@ void *render_chunks(void *args) {
return NULL; return NULL;
} }
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),
floor(pos[2] + 0.5)};
if (col) {
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]));
if (xd < 0 && n == 0) {
dir_temp[0] = 0;
pos[0] = closestVert[0];
return 1;
}
if (yd < 0 && n == 1) {
dir_temp[1] = 0;
pos[1] = closestVert[1];
return 1;
}
if (zd < 0 && n == 2) {
dir_temp[2] = 0;
pos[2] = closestVert[2];
return 1;
}
}
return 0;
}
#define WinMain main #define WinMain main
int main(void) { int main(void) {
@ -196,7 +223,7 @@ int main(void) {
pos[1] += (state ? 1 : -1); pos[1] += (state ? 1 : -1);
} }
pos[1] += 2.8; pos[1] += 15;
// int x = 0, y = 25, z = 0; // int x = 0, y = 25, z = 0;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -210,6 +237,8 @@ int main(void) {
struct chunk chunk = fetch_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH, struct chunk chunk = fetch_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH,
pos[2] / CHUNK_LENGTH, 0, 0); pos[2] / CHUNK_LENGTH, 0, 0);
vec3 dir_temp = {0, 0, 0};
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
@ -223,78 +252,69 @@ int main(void) {
mat4x4_identity(m); mat4x4_identity(m);
mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000); mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000);
vec3 direction = {cosf(cy) * sinf(cx) * 0.2, sinf(cy) * 0.2, // TODO: make control code its own thing
cosf(cy) * cosf(cx) * 0.2}; vec3 direction = {cosf(cy) * sinf(cx) * 0.1, sinf(cy) * 0.1,
vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.2, 0, cosf(cy) * cosf(cx) * 0.1};
cosf(cx - 3.14f / 2.0f) * 0.2}; vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.1, 0,
cosf(cx - 3.14f / 2.0f) * 0.1};
vec3 front = {sinf(cx) * 0.2, 0, cosf(cx) * 0.2}; vec3 front = {sinf(cx) * 0.1, 0, cosf(cx) * 0.1};
vec3 dir_temp = {0, 0, 0};
vec3 pos_temp = {0, 0, 0}; vec3 pos_temp = {0, 0, 0};
vec3_dup(pos_temp, pos); vec3_dup(pos_temp, pos);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
vec3_sub(dir_temp, dir_temp, front); vec3_sub(dir_temp, dir_temp, front);
vec3_sub(pos, pos, front);
} }
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
vec3_add(dir_temp, dir_temp, front); vec3_add(dir_temp, dir_temp, front);
vec3_add(pos, pos, front);
} }
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
vec3_sub(dir_temp, dir_temp, direction); vec3_sub(dir_temp, dir_temp, direction);
vec3_sub(pos, pos, direction);
} }
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
vec3_add(dir_temp, dir_temp, direction); vec3_add(dir_temp, dir_temp, direction);
vec3_add(pos, pos, direction);
} }
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
vec3_add(dir_temp, dir_temp, right); vec3_add(dir_temp, dir_temp, right);
vec3_add(pos, pos, right);
} }
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
vec3_sub(dir_temp, dir_temp, right); vec3_sub(dir_temp, dir_temp, right);
vec3_sub(pos, pos, right);
} }
vec3 closestVert = {floor(pos[0] + 0.5), floor(pos[1] + 0.5), vec3_scale(dir_temp, dir_temp, 0.6);
floor(pos[2] + 0.5)};
if ((dir_temp[0] != 0 || dir_temp[1] != 0 || dir_temp[2] != 0) && vec3 off = {0, 0, 0};
(cube_exists(pos[0], pos[1], pos[2], chunk, 0) || int col = 0;
cube_exists(pos[0], pos[1] - 1.0, pos[2], chunk, 0) || for (int a = -2; a < 0; a++) {
cube_exists(pos[0], pos[1] - 1.8, pos[2], chunk, 0))) { off[1] = a;
double xd = dir_temp[0] / (closestVert[0] - pos[0]); col = col || cube_exists(pos[0] + off[0], pos[1] + off[1],
double yd = dir_temp[1] / (closestVert[1] - pos[1]); pos[2] + off[2], chunk, 0);
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_add(pos, pos, dir_temp);
dir_temp[1] -= 0.07;
// vec3_scale(dir_temp, dir_temp, (d > 1.0) ? 1.0 : d); if (collision(chunk, pos, dir_temp, col, 1)) {
// vec3_sub(pos_temp, pos, dir_temp); col = 0;
// vec3_dup(pos, pos_temp); for (int a = -2; a < 0; a++) {
off[1] = a;
col = col || cube_exists(pos[0] + off[0], pos[1] + off[1],
pos[2] + off[2], chunk, 0);
} }
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
dir_temp[1] = 3;
}
}
collision(chunk, pos, dir_temp, col, 0);
collision(chunk, pos, dir_temp, col, 2);
vec3 dir_pos; vec3 dir_pos;
vec3_scale(direction, direction, 100.0); vec3_scale(direction, direction, 100.0);
vec3_add(dir_pos, direction, pos); vec3_add(dir_pos, direction, pos);