From 675a0181f0d6695cfb7e2e1a3418ff9795b39ad6 Mon Sep 17 00:00:00 2001 From: Dustin Date: Sun, 25 May 2025 18:40:21 -0700 Subject: [PATCH] Generalized resolve_inertia function. Have a bug on dash after land. --- src/classes/velocity_controller.gd | 251 ++++++++++++++--------------- 1 file changed, 125 insertions(+), 126 deletions(-) diff --git a/src/classes/velocity_controller.gd b/src/classes/velocity_controller.gd index 5d6443a..67b8032 100644 --- a/src/classes/velocity_controller.gd +++ b/src/classes/velocity_controller.gd @@ -132,67 +132,83 @@ func calculate_velocity(_delta :float, ## Acceleration is always postive because we use the current inertia ## placement to the movement range to determine direction of movement. - #calc_acceleration.x = abs(resolve_h_acceleration(movement_parameters, move_direction.x)) if sign(move_direction.x) != 0: #calc_acceleration.x = abs(movement_parameters.get_acceleration(0).x) calc_acceleration.x = movement_parameters.get_acceleration().x if calc_acceleration.x < 0: push_warning("Negative Acceleration shouln't happen") calc_acceleration.x = abs(calc_acceleration.x) - - if sign(move_direction.y) != 0: calc_acceleration.y = movement_parameters.get_acceleration().y + + ## Separate impulse function + if _h_impulse_applied == false and _h_impulse_speed_tracking != h_speed: + #(range_placement :int, inertia :float, impulse_speed_range :Vector2) + #_h_impulse_speed_tracking = apply_impulse(h_range_placement, calc_inertia.x, h_speed ) + var impulse_inertia = apply_impulse(h_range_placement, calc_inertia.x, h_speed ) + if impulse_inertia != calc_inertia.x: + #var foo = 2+2 + _h_impulse_speed_tracking = h_speed + _h_impulse_applied = true + calc_inertia.x = impulse_inertia if debug and movement_parameters.debug_name == 'fall': var foo = 2+2 ## We are always moving from h_speed.x towards y at a given rate ## if we have a difference of speed and an acceleration ##TODO: The Min Max could be augmented to just be h_speed.x and prevent the sliding. - if h_speed.x != h_speed.y and calc_acceleration.x != 0.0: - var direction_accel :float = calc_acceleration.x * _delta #* move_direction.x - if h_speed.x > h_speed.y: - direction_accel *= -1 - match h_range_placement: - RANGE_PLACEMENT.BEFORE_RANGE: - ## Also apply impulse here - if _h_impulse_applied == false: - calc_inertia.x += h_speed.x - _h_impulse_applied = true - #impulse_applied_dir = move_direction.x - calc_inertia.x = clamp(calc_inertia.x + direction_accel, - min(calc_inertia.x, h_speed.y), - max(calc_inertia.x, h_speed.y)) - RANGE_PLACEMENT.WITHIN_RANGE: - ## If we're within the range but our speed has just changed - if _h_impulse_applied == false and _h_impulse_speed_tracking != h_speed: - ## Set inertia to starting speed, we're already in range - calc_inertia.x = h_speed.x - _h_impulse_applied = true - calc_inertia.x = clamp(calc_inertia.x + direction_accel, - min(h_speed.x, h_speed.y), - max(h_speed.x, h_speed.y)) - RANGE_PLACEMENT.PAST_RANGE: - if _h_impulse_applied == false and abs(h_speed.x) > abs(h_speed.y): - calc_inertia.x = h_speed.x - _h_impulse_applied = true - calc_inertia.x = clamp(calc_inertia.x - direction_accel, # Friction - min(calc_inertia.x, h_speed.y), - max(calc_inertia.x, h_speed.y)) - - - elif calc_inertia.x != 0.0 and calc_acceleration.x != 0.0: ## We still have inertia but no difference in movement - if h_speed.x < h_speed.y: - h_speed.x = clamp(h_speed.x,0.0,h_speed.y) - - ## Move back towards the base speed - calc_inertia.x = move_toward(calc_inertia.x, h_speed.x, calc_acceleration.x * _delta) - - else: - ## inertia is just base speed - calc_inertia.x = h_speed.x - calc_inertial_dir.x = 0.0 # sign(calc_inertia.x) + calc_inertia.x = resolve_inertia( + h_range_placement, + calc_inertia.x , + h_speed, + (calc_acceleration.x * _delta) + ) + +## Diabled for single inertia function +# if h_speed.x != h_speed.y and calc_acceleration.x != 0.0: +# var direction_accel :float = calc_acceleration.x * _delta #* move_direction.x +# if h_speed.x > h_speed.y: +# direction_accel *= -1 +# match h_range_placement: +# RANGE_PLACEMENT.BEFORE_RANGE: +# ## Also apply impulse here +# if _h_impulse_applied == false: +# calc_inertia.x += h_speed.x +# _h_impulse_applied = true +# +# calc_inertia.x = clamp(calc_inertia.x + direction_accel, +# min(calc_inertia.x, h_speed.y), +# max(calc_inertia.x, h_speed.y)) +# RANGE_PLACEMENT.WITHIN_RANGE: +# ## If we're within the range but our speed has just changed +# if _h_impulse_applied == false and _h_impulse_speed_tracking != h_speed: +# ## Set inertia to starting speed, we're already in range +# calc_inertia.x = h_speed.x +# _h_impulse_applied = true +# calc_inertia.x = clamp(calc_inertia.x + direction_accel, +# min(h_speed.x, h_speed.y), +# max(h_speed.x, h_speed.y)) +# RANGE_PLACEMENT.PAST_RANGE: +# if _h_impulse_applied == false and abs(h_speed.x) > abs(h_speed.y): +# calc_inertia.x = h_speed.x +# _h_impulse_applied = true +# calc_inertia.x = clamp(calc_inertia.x - direction_accel, # Friction +# min(calc_inertia.x, h_speed.y), +# max(calc_inertia.x, h_speed.y)) +# +# +# elif calc_inertia.x != 0.0 and calc_acceleration.x != 0.0: ## We still have inertia but no difference in movement +# if h_speed.x < h_speed.y: +# h_speed.x = clamp(h_speed.x,0.0,h_speed.y) +# +# ## Move back towards the base speed +# calc_inertia.x = move_toward(calc_inertia.x, h_speed.x, calc_acceleration.x * _delta) +# +# else: +# ## inertia is just base speed +# calc_inertia.x = h_speed.x +# calc_inertial_dir.x = 0.0 # sign(calc_inertia.x) ## Another idea, just return the calculated velocity in PPS ## For now, y component of velocity is just gravity @@ -227,7 +243,6 @@ func calculate_velocity(_delta :float, calc_inertia.y = clamp(calc_inertia.y - direction_accel, # Friction min(calc_inertia.y, v_speed.y), max(calc_inertia.y, v_speed.y)) - else: ## The previous vertical movement methods #calc_inertia.y += movement_parameters.gravity * _delta @@ -292,44 +307,6 @@ func placement_to_speed_range(speed: float, speed_range: Vector2) -> int: return RANGE_PLACEMENT.BEFORE_RANGE return 0 -## Returns an Vector where x is MIN_SPEED and y is MAX_SPEED -func resolve_h_speed(_params :MovementParameters) -> Vector2: -# var base_speed :float = _params.base_move_speed.x -# ## if a speed difference applies -# if _params.move_speed_modifier.x != 0: -# var speed_differance = base_speed + _params.move_speed_modifier.x -# -# return(Vector2(base_speed, speed_differance)) -# return Vector2(base_speed,base_speed) - return Vector2(0,0) - -func resolve_v_speed(_params :MovementParameters) -> Vector2: -# var base_speed :float = _params.base_move_speed.y -# ## if a speed difference applies -# if _params.move_speed_modifier.y != 0: -# var speed_differance = base_speed + _params.move_speed_modifier.y -# -# return(Vector2(base_speed, speed_differance)) -# return Vector2(base_speed,base_speed) - return Vector2(0,0) - -func resolve_h_acceleration(_params :MovementParameters, _move_direction :float) -> float: - ## TODO: Adjust for jerk, determine if we're currently experiencing accel - ## if a speed difference applies -# var _acceleration :float = 0.0 -# var base_speed :float = _params.base_move_speed.x -# if _params.move_speed_modifier.x != 0: -# var speed_differance = base_speed + _params.move_speed_modifier.x -# _acceleration = _params.base_move_acceleration.x + _params.move_speed_modifier_acceleration.x -# -# else: -# _acceleration = _params.base_move_acceleration.x -# -# if sign(_move_direction) != 0: -# return _acceleration - - # No accel returned unless intended - return 0.0 func resolve_move_direction(_momentum :Vector2, _movement_direction :Vector2) -> Vector2: @@ -347,51 +324,73 @@ func resolve_move_direction(_momentum :Vector2, return Vector2(horizontal_movement_direction, vertical_movement_direction) -#func resolve_inertia(range_placement :int, impulse_status :bool , speed_range :Vector2, delta_acceleration :float) -> float: -# #DELTA? -# var inertia :float = 0.0 -# if speed_range.x != speed_range.y and delta_acceleration != 0.0: -# var direction_accel :float = delta_acceleration #* move_direction.x -# if speed_range.x > speed_range.y: -# direction_accel *= -1 -# match range_placement: -# RANGE_PLACEMENT.BEFORE_RANGE: -# ## Also apply impulse here +func apply_impulse(range_placement :int, inertia :float, impulse_speed_range :Vector2) -> float: + match range_placement: + RANGE_PLACEMENT.BEFORE_RANGE: + inertia += impulse_speed_range.x + return inertia + RANGE_PLACEMENT.WITHIN_RANGE: + #if impulse_status == false: # and _h_impulse_speed_tracking != impulse_speed: + ## Set inertia to starting speed, we're already in range + inertia = impulse_speed_range.x + return inertia + RANGE_PLACEMENT.PAST_RANGE: + if abs(impulse_speed_range.x) > abs(impulse_speed_range.y): + inertia = impulse_speed_range.x + return inertia + return inertia + +func resolve_inertia(range_placement :int, + inertia :float , + speed_range :Vector2, delta_acceleration :float) -> float: + + if speed_range.x != speed_range.y and delta_acceleration != 0.0: + var direction_accel :float = delta_acceleration #* move_direction.x + if speed_range.x > speed_range.y: + direction_accel *= -1 + match range_placement: + RANGE_PLACEMENT.BEFORE_RANGE: + ## Also apply impulse here # if _h_impulse_applied == false: -# calc_inertia.x += h_speed.x +# inertia += speed_range.x # _h_impulse_applied = true -# #impulse_applied_dir = move_direction.x -# inertia = clamp(calc_inertia.x + direction_accel, -# min(calc_inertia.x, h_speed.y), -# max(calc_inertia.x, h_speed.y)) -# RANGE_PLACEMENT.WITHIN_RANGE: -# ## If we're within the range but our speed has just changed + #impulse_applied_dir = move_direction.x + inertia = clamp(inertia + direction_accel, + min(inertia, speed_range.y), + max(inertia, speed_range.y)) + RANGE_PLACEMENT.WITHIN_RANGE: + ## If we're within the range but our speed has just changed # if _h_impulse_applied == false and _h_impulse_speed_tracking != h_speed: # ## Set inertia to starting speed, we're already in range # calc_inertia.x = h_speed.x # _h_impulse_applied = true -# calc_inertia.x = clamp(calc_inertia.x + direction_accel, -# min(h_speed.x, h_speed.y), -# max(h_speed.x, h_speed.y)) -# RANGE_PLACEMENT.PAST_RANGE: -# if _h_impulse_applied == false and abs(h_speed.x) > abs(h_speed.y): -# calc_inertia.x = h_speed.x + inertia = clamp(inertia + direction_accel, + min(inertia, speed_range.y), + max(inertia, speed_range.y)) + RANGE_PLACEMENT.PAST_RANGE: +# if _h_impulse_applied == false and abs(speed_range.x) > abs(speed_range.y): +# inertia = speed_range.x # _h_impulse_applied = true -# calc_inertia.x = clamp(calc_inertia.x - direction_accel, # Friction -# min(calc_inertia.x, h_speed.y), -# max(calc_inertia.x, h_speed.y)) -# -# -# elif calc_inertia.x != 0.0 and calc_acceleration.x != 0.0: ## We still have inertia but no difference in movement -# if h_speed.x < h_speed.y: -# h_speed.x = clamp(h_speed.x,0.0,h_speed.y) -# -# ## Move back towards the base speed -# calc_inertia.x = move_toward(calc_inertia.x, h_speed.x, calc_acceleration.x * _delta) -# -# else: -# ## inertia is just base speed -# calc_inertia.x = h_speed.x -# calc_inertial_dir.x = 0.0 # sign(calc_inertia.x) -# -# return 0.0 + inertia = clamp(inertia - direction_accel, # Friction + min(inertia, speed_range.y), + max(inertia, speed_range.y)) + + + elif inertia != 0.0 and delta_acceleration != 0.0: ## We still have inertia but no difference in movement + var clamped_speed = speed_range.x + if speed_range.x < speed_range.y: + clamped_speed.x = clamp(speed_range.x,0.0,speed_range.y) + + ## Move back towards the base speed + inertia = move_toward(inertia, clamped_speed.x, delta_acceleration) + + else: + ## inertia is just base speed + inertia = speed_range.x + #calc_inertial_dir.x = 0.0 # sign(calc_inertia.x) + + return inertia + + + +