aboutsummaryrefslogtreecommitdiffstats
path: root/src/player.c
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2025-12-30 04:11:22 +0000
committernathan <nathansmith@disroot.org>2025-12-30 04:11:22 +0000
commite507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3 (patch)
tree72a0acb1c0e008de2b24471e50dcd7b09e609abe /src/player.c
parent91b1c45da05c892a04b6a1daa99b1de89fa1f363 (diff)
downloadFindThings-e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3.tar.gz
FindThings-e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3.tar.bz2
FindThings-e507ecbb95a2c7a1ffb8c13f1bd39433d6c49ba3.zip
Working on building collision
Diffstat (limited to 'src/player.c')
-rw-r--r--src/player.c71
1 files changed, 71 insertions, 0 deletions
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.