diff options
| author | nathan <nathansmith@disroot.org> | 2025-12-30 04:11:22 +0000 |
|---|---|---|
| committer | nathan <nathansmith@disroot.org> | 2025-12-30 04:11:22 +0000 |
| commit | e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3 (patch) | |
| tree | 72a0acb1c0e008de2b24471e50dcd7b09e609abe | |
| parent | 91b1c45da05c892a04b6a1daa99b1de89fa1f363 (diff) | |
| download | FindThings-e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3.tar.gz FindThings-e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3.tar.bz2 FindThings-e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3.zip | |
Working on building collision
| -rw-r--r-- | src/entity.c | 3 | ||||
| -rw-r--r-- | src/entity.h | 4 | ||||
| -rw-r--r-- | src/player.c | 71 | ||||
| -rw-r--r-- | src/player.h | 4 | ||||
| -rw-r--r-- | src/world.h | 2 |
5 files changed, 80 insertions, 4 deletions
diff --git a/src/entity.c b/src/entity.c index 1d10e4f..4895d9e 100644 --- a/src/entity.c +++ b/src/entity.c @@ -350,7 +350,8 @@ EntityBuilding* createEntityBuilding(Image heightmap) .roofOffset = (Vector3){ widthInWorld / 2.0 - ENTITY_BUILDING_CUBE_SIZE.x / 2.0, ENTITY_BUILDING_CUBE_SIZE.y + TOUCHING_OFFSET, - heightInWorld / 2.0 - ENTITY_BUILDING_CUBE_SIZE.z / 2.0} + heightInWorld / 2.0 - ENTITY_BUILDING_CUBE_SIZE.z / 2.0 + } }; return entityBuilding; diff --git a/src/entity.h b/src/entity.h index 0469476..e92fbb2 100644 --- a/src/entity.h +++ b/src/entity.h @@ -75,8 +75,8 @@ struct Entity { typedef struct { Model model; Color* pixelMap; - float width; - float height; + int width; + int height; float roofSize; float roofHeight; Vector3 roofOffset; diff --git a/src/player.c b/src/player.c index 2b72477..11bc307 100644 --- a/src/player.c +++ b/src/player.c @@ -7,6 +7,12 @@ Player createPlayer() .position = Vector3Zero(), .direction = (Vector3){0.0, 0.0, 0.0}, .velocity = Vector3Zero(), + .relativeBox = (BoundingBox){ + .min = (Vector3){-PLAYER_WIDTH / 2.0, -PLAYER_HEIGHT / 2.0, + -PLAYER_WIDTH / 2.0}, + .max = (Vector3){PLAYER_WIDTH / 2.0, PLAYER_HEIGHT / 2.0, + PLAYER_WIDTH / 2.0} + }, .camera = (Camera){ .position = (Vector3){0.0, 0.0, 0.0}, .target = Vector3Zero(), @@ -48,6 +54,63 @@ void updatePlayerLookingAround(Player* player, Game* game) player->direction = (Vector3){matrix.m2, matrix.m6, matrix.m10}; } +WorldUID playerCheckCollisionWithBuildings(Player* player, Game* game) +{ + for (int index = 0; index < WORLD_PLACE_COUNT; ++index) + { + WorldUID uid = game->world.places[index]; + Entity* place = &game->world.entities[uid]; + + if (entityIsBuilding(place->id) + && CheckCollisionBoxes(place->box, player->box)) + { + return uid; + } + } + + return ENTITY_NONE; +} + +// TODO: Make this fucker speedy quick +void playerHandleCollisionWithBuilding(Player* player, Game* game, + WorldUID uid) +{ + Entity* place = &game->world.entities[uid]; + EntityBuilding* building = (EntityBuilding*)place->data; + + BoundingBox wallBox = (BoundingBox){ + .min = (Vector3){-ENTITY_BUILDING_CUBE_SIZE.x / 2.0, + -ENTITY_BUILDING_CUBE_SIZE.y / 2.0, + -ENTITY_BUILDING_CUBE_SIZE.z / 2.0}, + .max = (Vector3){ENTITY_BUILDING_CUBE_SIZE.x / 2.0, + ENTITY_BUILDING_CUBE_SIZE.y / 2.0, + ENTITY_BUILDING_CUBE_SIZE.z / 2.0} + }; + + for (int y = 0; y < building->height; ++y) + { + for (int x = 0; x < building->width; ++x) + { + Color color = building->pixelMap[y * building->width + x]; + + if (color.r == 0) + { + continue; + } + + Vector3 wallPosition = (Vector3){ + place->position.x + x * ENTITY_BUILDING_CUBE_SIZE.x, + place->position.y + ENTITY_BUILDING_CUBE_SIZE.y / 2.0, + place->position.z + y * ENTITY_BUILDING_CUBE_SIZE.z + }; + + BoundingBox box = wallBox; + box.min = Vector3Add(box.min, place->position); + box.max = Vector3Add(box.max, place->position); + } + } +} + void updatePlayerMovement(Player* player, Game* game) { Camera* camera = &player->camera; @@ -85,10 +148,18 @@ void updatePlayerMovement(Player* player, Game* game) player->velocity = Vector3Scale(player->velocity, PLAYER_SPEED); + printf("%d\n", playerCheckCollisionWithBuildings(player, game)); + // Apply velocity. player->position = Vector3Add( player->position, Vector3Scale(player->velocity, GetFrameTime())); + // Update box. + player->box = player->relativeBox; + player->box.min = Vector3Add(player->box.min, player->position); + player->box.max = Vector3Add(player->box.max, player->position); + //DrawBoundingBox(player->box, YELLOW); + updatePlayerHeight(player, game); // Apply camera. diff --git a/src/player.h b/src/player.h index 6be9258..7755ef9 100644 --- a/src/player.h +++ b/src/player.h @@ -4,6 +4,7 @@ #ifndef PLAYER_H #define PLAYER_H +#define PLAYER_WIDTH 1.0 #define PLAYER_HEIGHT 2.0 #define PLAYER_SPEED 8.0 #define PLAYER_MAX_SELECT_DISTANCE 10.0 @@ -12,6 +13,9 @@ typedef struct { Vector3 position; Vector3 direction; Vector3 velocity; + + BoundingBox relativeBox; + BoundingBox box; Camera camera; Vector2 cameraAngle; diff --git a/src/world.h b/src/world.h index 723db8b..2b51485 100644 --- a/src/world.h +++ b/src/world.h @@ -5,7 +5,7 @@ #ifndef WORLD_H #define WORLD_H -#define USE_WORLD_DEBUG_TEXTURE +//#define USE_WORLD_DEBUG_TEXTURE #define WORLD_ENTITY_MAX 5000 #define WORLD_PLANT_COUNT 2500 |
