Skip to content

Commit

Permalink
feat: entities can override the global max gravity Y velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtome committed Sep 2, 2024
1 parent e9770d8 commit 0b84ecd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class GravityAccelerationBehavior extends PhysicalBehavior {
final gAccel = world.gravity * dt;
final y = parent.velocity.y;
final desiredVelocity = (gAccel * parent.gravityRate) + y;
parent.velocity.y = min(desiredVelocity, world.maxGravityVelocity);

// Max out at termincal velocity
parent.velocity.y = min(desiredVelocity, parent.maxGravityVelocity);
}
}
9 changes: 8 additions & 1 deletion packages/leap/lib/src/entities/physical_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ abstract class PhysicalEntity extends PositionedEntity {
/// manages its own position state it should set this itself
final Vector2 prevPosition = Vector2.zero();

/// Multiplier on standard gravity, see [LeapWorld].
/// Multiplier on standard gravity, see [GravityAccelerationBehavior].
double gravityRate = 1;

/// Override the world [maxGravityVelocity] just for this entity
double? maxGravityVelocityOverride;

/// Capped downward velocity (positive y), see [GravityAccelerationBehavior]
double get maxGravityVelocity =>
maxGravityVelocityOverride ?? leapWorld.maxGravityVelocity;

PhysicalEntity({
this.static = false,
super.position,
Expand Down

0 comments on commit 0b84ecd

Please sign in to comment.