aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2026-01-06 13:20:21 +0000
committernathan <nathansmith@disroot.org>2026-01-06 13:20:21 +0000
commitd55707e4b817390ba268634c3c18d510001f5b13 (patch)
treea3d850d134024758795681d49e77d4ed99582d56
parent1f63f4952efa2a14e63edea65e6bbbe6b0137a66 (diff)
downloadFindThings-d55707e4b817390ba268634c3c18d510001f5b13.tar.gz
FindThings-d55707e4b817390ba268634c3c18d510001f5b13.tar.bz2
FindThings-d55707e4b817390ba268634c3c18d510001f5b13.zip
Way better player collision
-rw-r--r--src/entities/johnsStore.c4
-rw-r--r--src/player.c55
2 files changed, 31 insertions, 28 deletions
diff --git a/src/entities/johnsStore.c b/src/entities/johnsStore.c
index 41217ed..f1a35d2 100644
--- a/src/entities/johnsStore.c
+++ b/src/entities/johnsStore.c
@@ -8,8 +8,8 @@ void initJohnsStore(Entity* entity)
Color colors[] = {
WHITE, WHITE, WHITE, WHITE, WHITE, WHITE,
WHITE, BLACK, BLACK, BLACK, BLACK, WHITE,
- WHITE, BLACK, BLACK, BLACK, BLACK, WHITE,
- WHITE, BLACK, BLACK, BLACK, BLACK, WHITE,
+ WHITE, BLACK, WHITE, BLACK, BLACK, WHITE,
+ WHITE, BLACK, BLACK, WHITE, BLACK, WHITE,
WHITE, BLACK, BLACK, BLACK, BLACK, WHITE,
WHITE, WHITE, BLACK, BLACK, WHITE, WHITE
};
diff --git a/src/player.c b/src/player.c
index 18d36af..7b5b47c 100644
--- a/src/player.c
+++ b/src/player.c
@@ -71,15 +71,12 @@ WorldUID playerCheckCollisionWithBuildings(Player* player, Game* game)
return ENTITY_NONE;
}
-// Returns true if collision detection should end.
-bool playerHandleCollisionWithBuildingBlock(Player* player,
+void playerHandleCollisionWithBuildingBlock(Player* player,
EntityBuilding* building,
BoundingBox block,
Vector3 blockPosition,
int x,
- int y,
- bool* isFirstCollision,
- Vector3* lastNormal)
+ int y)
{
int normalsCount = 4;
@@ -105,6 +102,7 @@ bool playerHandleCollisionWithBuildingBlock(Player* player,
x + normals[index].x,
y + normals[index].z);
+
if (!isNormalBlocked && distance < closestDistance)
{
closestDistance = distance;
@@ -114,26 +112,39 @@ bool playerHandleCollisionWithBuildingBlock(Player* player,
Vector3 normal = normals[closestNormalIndex];
+ Vector3 direction = Vector3Normalize(Vector3Subtract(blockPosition,
+ player->position));
+
+ // Don't touch normals facing away.
+ if (Vector3DotProduct(direction, normal) > 0.0)
+ {
+ return;
+ }
+
// Is facing wall.
if (Vector3DotProduct(player->velocity, normal) < 0.0)
{
- // Handle collision.
+ // Wall slide.
float dotProduct = Vector3DotProduct(player->velocity, normal);
player->velocity = Vector3Subtract(player->velocity,
Vector3Scale(normal, dotProduct));
- // Is at a corner.
- if (!*isFirstCollision && !Vector3Equals(*lastNormal, normal))
+ // Handle collision.
+ if (closestNormalIndex <= 1)
{
- player->velocity = (Vector3){0.0, 0.0, 0.0};
- return true;
+ player->velocity.x = ((blockPosition.x +
+ (ENTITY_BUILDING_CUBE_SIZE.x / 2.0 +
+ PLAYER_WIDTH / 2.0) * normal.x) -
+ player->position.x) / GetFrameTime();
+ }
+ else
+ {
+ player->velocity.z = ((blockPosition.z +
+ (ENTITY_BUILDING_CUBE_SIZE.z / 2.0 +
+ PLAYER_WIDTH / 2.0) * normal.z) -
+ player->position.z) / GetFrameTime();
}
-
- *isFirstCollision = false;
- *lastNormal = normal;
}
-
- return false;
}
void playerHandleCollisionWithBuilding(Player* player, Game* game,
@@ -151,9 +162,6 @@ void playerHandleCollisionWithBuilding(Player* player, Game* game,
ENTITY_BUILDING_CUBE_SIZE.z / 2.0}
};
- bool isFirstCollision = true;
- Vector3 lastNormal = (Vector3){0.0, 0.0, 0.0};
-
int playerX = (player->position.x - place->position.x) /
ENTITY_BUILDING_CUBE_SIZE.x;
int playerY = (player->position.z - place->position.z) /
@@ -196,13 +204,8 @@ void playerHandleCollisionWithBuilding(Player* player, Game* game,
if (CheckCollisionBoxes(player->box, box))
{
- if (playerHandleCollisionWithBuildingBlock(player, building, box,
- wallPosition, x, y,
- &isFirstCollision,
- &lastNormal))
- {
- return;
- }
+ playerHandleCollisionWithBuildingBlock(player, building, box,
+ wallPosition, x, y);
}
}
}
@@ -261,7 +264,7 @@ void updatePlayerMovement(Player* player, Game* game)
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);
+ DrawBoundingBox(player->box, YELLOW);
updatePlayerHeight(player, game);