fix: Align player collision box and sprite rendering precisely

This commit is contained in:
Kacper Kostka (aider)
2025-04-05 18:33:19 +02:00
parent 04295f9f9f
commit 8e2575f6fc
2 changed files with 21 additions and 9 deletions

View File

@@ -81,10 +81,13 @@ class Entity {
const numBottomPoints = 5;
let groundCollision = false;
// For player entity, adjust the collision detection to match sprite feet position
const yOffset = this.type === ENTITY_TYPES.PLAYER ? 2 : 0;
for (let i = 0; i < numBottomPoints; i++) {
const ratio = i / (numBottomPoints - 1);
const bottomX = newX - halfWidth + (2 * halfWidth * ratio);
const bottomY = newY + halfHeight;
const bottomY = newY + halfHeight + yOffset;
if (this.isPixelSolid(bottomX, bottomY)) {
groundCollision = true;
@@ -99,8 +102,11 @@ class Entity {
}
// Check side points for horizontal collision
const leftMiddle = { x: newX - halfWidth, y: newY };
const rightMiddle = { x: newX + halfWidth, y: newY };
// For player entity, adjust the collision detection to match sprite position
const yAdjust = this.type === ENTITY_TYPES.PLAYER ? -1 : 0;
const leftMiddle = { x: newX - halfWidth, y: newY + yAdjust };
const rightMiddle = { x: newX + halfWidth, y: newY + yAdjust };
if (this.isPixelSolid(leftMiddle.x, leftMiddle.y)) {
result.collision = true;
@@ -113,7 +119,7 @@ class Entity {
}
// Check top for ceiling collision
const topMiddle = { x: newX, y: newY - halfHeight };
const topMiddle = { x: newX, y: newY - halfHeight + (this.type === ENTITY_TYPES.PLAYER ? -2 : 0) };
if (this.isPixelSolid(topMiddle.x, topMiddle.y)) {
result.collision = true;
result.vertical = true;