From 0b84ecdd979cc6a3bc8c1dd6fb3eed5ecaea0a8f Mon Sep 17 00:00:00 2001 From: Kurtis Melby Date: Mon, 2 Sep 2024 12:55:42 -0400 Subject: [PATCH] feat: entities can override the global max gravity Y velocity --- .../behaviors/gravity_acceleration_behavior.dart | 4 +++- packages/leap/lib/src/entities/physical_entity.dart | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/leap/lib/src/entities/behaviors/gravity_acceleration_behavior.dart b/packages/leap/lib/src/entities/behaviors/gravity_acceleration_behavior.dart index bc66a3a..19016b9 100644 --- a/packages/leap/lib/src/entities/behaviors/gravity_acceleration_behavior.dart +++ b/packages/leap/lib/src/entities/behaviors/gravity_acceleration_behavior.dart @@ -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); } } diff --git a/packages/leap/lib/src/entities/physical_entity.dart b/packages/leap/lib/src/entities/physical_entity.dart index 203a42c..b9cfd98 100644 --- a/packages/leap/lib/src/entities/physical_entity.dart +++ b/packages/leap/lib/src/entities/physical_entity.dart @@ -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,