Skip to content

Commit

Permalink
Break velocity when hitting ceiling
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Jul 10, 2024
1 parent 905cd94 commit 6c8eee3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/game/actors/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Player extends SpriteComponent with CollisionCallbacks, KeyboardHandler {
final double _moveSpeed = 200;

final Vector2 _up = Vector2(0, -1);
final Vector2 _down = Vector2(0, 1);
final Vector2 _velocity = Vector2.zero();

Player(
Expand Down Expand Up @@ -96,10 +97,14 @@ class Player extends SpriteComponent with CollisionCallbacks, KeyboardHandler {
final separationDistance = (size.x / 2) - collisionNormal.length;
collisionNormal.normalize();

// If collision normal is almost upwards,
// player must be on ground.
if (_up.dot(collisionNormal) > 0.9) {
// If collision normal is almost upwards,
// player must be on ground.
_isOnGround = true;
} else if (_down.dot(collisionNormal) > 0.9) {
// If collision normal is almost downwards,
// player must be hitting the ceiling.
_velocity.y = 0;
}

// Resolve collision by moving player along
Expand Down

0 comments on commit 6c8eee3

Please sign in to comment.