From c23947247f76a519795e82c15c1ef01f4b4d58bc Mon Sep 17 00:00:00 2001 From: DrNuget Date: Tue, 4 Mar 2025 21:07:46 +0200 Subject: some broken shit --- src/entity.c | 6 +++--- src/entity.h | 4 ++-- src/globals.h | 2 +- src/main.c | 28 +++++++++++++------------ src/player.h | 2 +- src/world.c | 66 +++++++++++++++++++++++++++++++++++++++-------------------- 6 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/entity.c b/src/entity.c index f2c4190..59f55f1 100644 --- a/src/entity.c +++ b/src/entity.c @@ -1,9 +1,9 @@ #include "entity.h" -Hitbox createHitbox(Vector3* vertices) +Hitbox createHitbox(Vector3* boxes) { return (Hitbox){ - .vertices = vertices, - .num_vertices = sizeof(vertices)/sizeof(Vector3) + .boxes = boxes, + .num_boxes = sizeof(boxes)/sizeof(Vector3) }; } diff --git a/src/entity.h b/src/entity.h index 01705b1..0424275 100644 --- a/src/entity.h +++ b/src/entity.h @@ -3,8 +3,8 @@ #include typedef struct hitbox { - Vector3* vertices; - int num_vertices; + Vector3* boxes; + int num_boxes; } Hitbox; Hitbox createHitbox(Vector3* vertices); diff --git a/src/globals.h b/src/globals.h index 5250e03..f17386b 100644 --- a/src/globals.h +++ b/src/globals.h @@ -1,6 +1,6 @@ #ifndef GLOBALS_H #define GLOBALS_H -#define GRAVITY 1 +#define GRAVITY 9 #endif diff --git a/src/main.c b/src/main.c index 663fe63..d5cbf34 100644 --- a/src/main.c +++ b/src/main.c @@ -65,6 +65,18 @@ int main(void) {1,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1} }, +{ + {1,1,1,1,1,1,1,1,1,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,0,0,0,0,0,0,0,0,1}, + {1,1,1,1,1,1,1,1,1,1} + } } }; @@ -78,17 +90,7 @@ int main(void) .fovy = 60.0f, .projection = CAMERA_PERSPECTIVE, }, - .hitbox = createHitbox((Vector3[]){ - { 1.0f, 0.0f, 1.0f}, - { 1.0f, 0.0f, -1.0f}, - {-1.0f, 0.0f, 1.0f}, - { 1.0f, 0.0f, -1.0f}, - - { 1.0f, 2.0f, 1.0f}, - { 1.0f, 2.0f, -1.0f}, - {-1.0f, 2.0f, 1.0f}, - { 1.0f, 2.0f, -1.0f}, - }), + .hitbox = (Vector3){1.0f, 2.0f, 1.0f} }; player.camera.position = (Vector3){5.0f,10.0f,5.0f}; @@ -107,9 +109,9 @@ int main(void) ); //UpdateCamera(&player.camera, cameraMode); handleInput(&player); - handleCollision(&world, &player); - handleGravity(&world, &player); handleMovement(&player); + handleGravity(&world, &player); + handleCollision(&world, &player); BeginDrawing(); BeginMode3D(player.camera); diff --git a/src/player.h b/src/player.h index 38ae1df..8d58ae3 100644 --- a/src/player.h +++ b/src/player.h @@ -7,7 +7,7 @@ typedef struct player { Vector3 pos; Vector3 velocity; Camera3D camera; - Hitbox hitbox; + Vector3 hitbox; int speed; short is_on_floor; diff --git a/src/world.c b/src/world.c index 17e35b9..65a2095 100644 --- a/src/world.c +++ b/src/world.c @@ -51,40 +51,62 @@ void drawWorld(World *world) } } +int intersects(Vector3 h1, Vector3 h2, Vector3 pos1, Vector3 pos2) +{ + return ( + pos1.x <= h2.x+pos2.x && + h1.x+pos1.x >= h2.x && + pos1.y <= h2.y+pos2.y && + h1.y+pos1.y >= h2.y && + pos1.z <= h2.z+pos2.z && + h1.z+pos1.z >= h2.z + ); +} + void handleCollision(World *world, Player *player) { int posX, posY, posZ; posX = (int)player->pos.x; posY = (int)player->pos.y; posZ = (int)player->pos.z; - //Floor check: - if (world->map[posY][posX][posZ]!=0) { - player->is_on_floor=1; - if (player->velocity.y < 0.0f) { - player->velocity.y = 0.0f; + player->is_on_floor=0; + for(int x=-1;x<2;x++) + for(int y=-1;y<2;y++) + for(int z=-1;z<2;z++) { + int cPosX, cPosY, cPosZ; + cPosX = posX+x; + cPosY = posY+y; + cPosZ = posZ+z; + if (world->map[cPosY][cPosX][cPosZ]!=0) { + int inter = intersects( + player->hitbox, + (Vector3){1.0f, 1.0f, 1.0f}, + player->pos, + (Vector3){ + (float)cPosX, + (float)cPosY-1, + (float)cPosZ, + } + ); + if (inter) { + player->velocity.y=0.0f; + player->pos.y=cPosY+1.0f; + player->is_on_floor=1; + } } - } else { - player->is_on_floor=0; - } - if (world->map[posY][posX][posZ]!=0 - && (float)posY!=player->pos.y) { - player->camera.position.y=(float)posY+2; - //player->velocity.y = 0.0f; } } void handleGravity(World *world, Player *player) { - if (player->velocity.y > 0.0f) { - player->velocity.y *= 0.9f; - } - if (player->velocity.y <= 0.1f && player->velocity.y > 0.0f) { - player->velocity.y = 0.0f; - } + //if (player->velocity.y > 0.0f) { + // player->velocity.y *= 0.9f; + //} int posX, posY, posZ; posX = (int)player->pos.x; posY = (int)player->pos.y; posZ = (int)player->pos.z; + player->velocity.y -= 0.01f; printf( "Player.pos.y:\t%d\n" "Player.pos.x:\t%d\n" @@ -94,8 +116,8 @@ void handleGravity(World *world, Player *player) posZ ); printf("%d\n", (int)world->map[posY][posX][posZ]); - if (player->velocity.y <= 0.0f - && world->map[posY][posX][posZ]==0) { - player->velocity.y -= 0.1f; - } + //if (player->velocity.y <= 0.0f + //&& world->map[posY][posX][posZ]==0) { + // player->velocity.y -= 0.01f; + //} } -- cgit v1.2.3