fix: Align player collision box and sprite rendering precisely
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user