From 80346819885df71d1b61b4248df6bb8a442b9d83 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 5 Jan 2026 03:08:59 -0700 Subject: Make thingy speedy quick --- src/player.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'src/player.c') diff --git a/src/player.c b/src/player.c index 7917545..6cce443 100644 --- a/src/player.c +++ b/src/player.c @@ -7,7 +7,6 @@ Player createPlayer() .position = Vector3Zero(), .direction = (Vector3){0.0, 0.0, 0.0}, .velocity = Vector3Zero(), - .speed = 0.0, .relativeBox = (BoundingBox){ .min = (Vector3){-PLAYER_WIDTH / 2.0, -PLAYER_HEIGHT / 2.0, -PLAYER_WIDTH / 2.0}, @@ -119,10 +118,9 @@ bool playerHandleCollisionWithBuildingBlock(Player* player, if (Vector3DotProduct(player->velocity, normal) < 0.0) { // Handle collision. - float dotProduct = Vector3DotProduct(player->direction, normal); - player->velocity = Vector3Subtract(player->direction, + float dotProduct = Vector3DotProduct(player->velocity, normal); + player->velocity = Vector3Subtract(player->velocity, Vector3Scale(normal, dotProduct)); - player->velocity = Vector3Scale(player->velocity, player->speed); // Is at a corner. if (!*isFirstCollision && !Vector3Equals(*lastNormal, normal)) @@ -138,7 +136,6 @@ bool playerHandleCollisionWithBuildingBlock(Player* player, return false; } -// TODO: Make this fucker speedy quick void playerHandleCollisionWithBuilding(Player* player, Game* game, WorldUID uid) { @@ -157,10 +154,28 @@ void playerHandleCollisionWithBuilding(Player* player, Game* game, bool isFirstCollision = true; Vector3 lastNormal = (Vector3){0.0, 0.0, 0.0}; - for (int y = 0; y < building->height; ++y) + int playerX = (player->position.x - place->position.x) / + ENTITY_BUILDING_CUBE_SIZE.x; + int playerY = (player->position.z - place->position.z) / + ENTITY_BUILDING_CUBE_SIZE.y; + + // Scan near by blocks for collision. + int scanDistance = 2; + + for (int yOffset = -scanDistance; yOffset < scanDistance; ++yOffset) { - for (int x = 0; x < building->width; ++x) + for (int xOffset = -scanDistance; xOffset < scanDistance; ++xOffset) { + int x = playerX + xOffset; + int y = playerY + yOffset; + + // Out of bounds. + if (x < 0 || x >= building->width || + y < 0 || y >= building->height) + { + continue; + } + Color color = building->pixelMap[y * building->width + x]; if (color.r == 0) @@ -168,6 +183,7 @@ void playerHandleCollisionWithBuilding(Player* player, Game* game, continue; } + // Get wall position and bounding box. Vector3 wallPosition = (Vector3){ place->position.x + x * ENTITY_BUILDING_CUBE_SIZE.x, place->position.y + ENTITY_BUILDING_CUBE_SIZE.y / 2.0, @@ -180,7 +196,6 @@ void playerHandleCollisionWithBuilding(Player* player, Game* game, if (CheckCollisionBoxes(player->box, box)) { - DrawBoundingBox(box, PURPLE); if (playerHandleCollisionWithBuildingBlock(player, building, box, wallPosition, x, y, &isFirstCollision, @@ -229,7 +244,6 @@ void updatePlayerMovement(Player* player, Game* game) } player->velocity = Vector3Scale(player->velocity, PLAYER_SPEED); - player->speed = Vector3Length(player->velocity); // Check collision with buildings. WorldUID buildingUID = playerCheckCollisionWithBuildings(player, game); @@ -378,11 +392,6 @@ void updatePlayer(Player* player, Game* game) .direction = player->direction }; - if (game->isCrossHairEnabled) - { - DrawRay(ray, YELLOW); - } - if (player->isInteracting) { playerUpdateSelectedEntity(player, player->selectedEntity, game); -- cgit v1.2.3