Skip to content

Commit

Permalink
fix: non-solid collisions no not account for velocity properly
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtome committed Sep 1, 2024
1 parent de882d6 commit 9aa03cc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ class ApplyVelocityBehavior extends PhysicalBehavior {
return;
}

// Up collision
if (collisionInfo.up && velocity.y < 0) {
// Set the top of this to the bottom of the collision on top.
velocity.y = 0;
parent.top = collisionInfo.upCollision!.relativeBottom(parent);
}

// Down collision
if (collisionInfo.down && velocity.y > 0) {
// Set the bottom of this to the top of the collision underneath.
velocity.y = 0;
parent.bottom = collisionInfo.downCollision!.relativeTop(parent);
}

// Right collision
if (collisionInfo.right && velocity.x > 0) {
if (collisionInfo.rightCollision!.isSlopeFromLeft) {
parent.bottom = math.min(
Expand All @@ -41,6 +46,8 @@ class ApplyVelocityBehavior extends PhysicalBehavior {
parent.right = collisionInfo.rightCollision!.left;
}
}

// Left collision
if (collisionInfo.left && velocity.x < 0) {
if (collisionInfo.leftCollision!.isSlopeFromRight) {
parent.bottom = math.min(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CollisionDetectionBehavior extends PhysicalBehavior
void _nonSolidCollisionDetection(double dt) {
// Now that we have up/down/left/right collisions from solid detection phase,
// we can re-check which potential hits would still collide even.
_proxyHitboxForNonSolidHits();
_proxyHitboxForNonSolidHits(dt);
for (final other in _potentialHits) {
if (!parent.isOtherSolid(other) && intersectsOther(_hitboxProxy, other)) {
collisionInfo.addNonSolidCollision(other);
Expand Down Expand Up @@ -240,14 +240,14 @@ class CollisionDetectionBehavior extends PhysicalBehavior
_hitboxProxy.height = height;
_hitboxProxy.width = width;

if (velocity.y > 0) {
if (velocity.y >= 0) {
_hitboxProxy.y = y;
} else {
_hitboxProxy.y = y + velocity.y * dt;
}
_hitboxProxy.height = height + velocity.y.abs() * dt;

if (velocity.x > 0) {
if (velocity.x >= 0) {
_hitboxProxy.x = x;
} else {
_hitboxProxy.x = x + velocity.x * dt;
Expand All @@ -260,7 +260,7 @@ class CollisionDetectionBehavior extends PhysicalBehavior
_hitboxProxy.x = x;
_hitboxProxy.width = width;

if (velocity.y > 0) {
if (velocity.y >= 0) {
_hitboxProxy.y = y;
} else {
_hitboxProxy.y = y + velocity.y * dt;
Expand All @@ -273,19 +273,16 @@ class CollisionDetectionBehavior extends PhysicalBehavior
_hitboxProxy.y = y;
_hitboxProxy.height = height;

if (velocity.x > 0) {
if (velocity.x >= 0) {
_hitboxProxy.x = x;
} else {
_hitboxProxy.x = x + velocity.x * dt;
}
_hitboxProxy.width = width + velocity.x.abs() * dt;
}

void _proxyHitboxForNonSolidHits() {
_hitboxProxy.x = x;
_hitboxProxy.y = y;
_hitboxProxy.width = width;
_hitboxProxy.height = height;
void _proxyHitboxForNonSolidHits(double dt) {
_proxyHitboxForPotentialHits(dt);

// This is intended to run after solid collision detection.
// So, up/down/left/right will only be set if this is moving
Expand Down

0 comments on commit 9aa03cc

Please sign in to comment.