From dceb0ad3e8aa7f2e61f805ee313afc05d2588b1c Mon Sep 17 00:00:00 2001 From: Dustin Date: Sat, 26 Apr 2025 13:36:17 -0700 Subject: [PATCH] Getting better on movement but still buggy. --- lib/classes/movement_state_receiver.gd | 78 +++++++++++++-------- src/actors/players/playerE/states/idle.tres | 2 +- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lib/classes/movement_state_receiver.gd b/lib/classes/movement_state_receiver.gd index b048ae4..f823e2b 100644 --- a/lib/classes/movement_state_receiver.gd +++ b/lib/classes/movement_state_receiver.gd @@ -144,6 +144,8 @@ func _on_state_change(old_state_name:String, new_state :State): # Update: Actually should just return movement in PPS ## var calc_inertia = velocity.abs() +var calc_inertial_dir :Vector2 +var debug_speed_tracker :Vector2 func new_move_actor_as_desired(_delta :float, _state :StateAnimatedActor, _movement_override_normal := Vector2(0,0)) -> Vector2: @@ -174,16 +176,15 @@ func new_move_actor_as_desired(_delta :float, ## If an override has been passed (we're ignoring input direction var move_direction = Vector2.ZERO if _movement_override_normal != Vector2.ZERO: - move_direction = resolve_move_direction(inertia, _movement_override_normal) + move_direction = resolve_move_direction(calc_inertial_dir, _movement_override_normal) else: - move_direction = resolve_move_direction(inertia, desired_movement_vector) + move_direction = resolve_move_direction(calc_inertial_dir, desired_movement_vector) calc_acceleration.x = resolve_h_acceleration(movement_params, h_speed, move_direction.x) - if calc_acceleration.x != 0: - acceleration.x = calc_acceleration.x ## if we have a difference of speed if h_speed.x != h_speed.y: + debug_speed_tracker = h_speed if calc_inertia.x == 0.0: #calc_inertia.x = lerp( h_speed.x , h_speed.y, calc_acceleration.x * _delta) calc_inertia.x = clamp( calc_inertia.x + (calc_acceleration.x * _delta), @@ -192,34 +193,49 @@ func new_move_actor_as_desired(_delta :float, #calc_inertia.x = lerp( calc_inertia.x, h_speed.y, calc_acceleration.x * _delta) calc_inertia.x = clamp( calc_inertia.x + ( calc_acceleration.x * _delta), calc_inertia.x, h_speed.y) - elif calc_inertia.x != 0.0: - ## We still have inertia - #calc_inertia.x = lerp(calc_inertia.x, h_speed.x, _delta) - calc_inertia.x = clamp( calc_inertia.x + ( acceleration.x * _delta), - calc_inertia.x, h_speed.x) -# if (current_state.name == 'idle'): -# print(acceleration) + elif calc_inertia.x != 0.0: ## We still have inertia + if calc_acceleration.x != 0.0: # We are applying acceleration + if calc_inertia.x > h_speed.x: + calc_inertia.x = clamp( calc_inertia.x + ( calc_acceleration.x * _delta), + calc_inertia.x, h_speed.x) + else: + calc_inertia.x = clamp( calc_inertia.x + ( calc_acceleration.x * _delta), + h_speed.x, calc_inertia.x) + else: ## No longer applying acceleration + ## Neutralize the inertia? but how? + ## Recalc the acceleration with inertia + var friction :Vector2 + friction.x = resolve_h_acceleration(movement_params,Vector2(calc_inertia.x, h_speed.x), move_direction.x) + if friction.x != 0.0: ## No dapening acceleration applies + if calc_inertia.x > h_speed.x: ## Inertia is higher than the base speed (slow it down) + calc_inertia.x = clamp( calc_inertia.x + ( friction.x * _delta), + h_speed.x, calc_inertia.x) + else: ## Inertia lower than base speed (we need to catch up) + calc_inertia.x = clamp( calc_inertia.x + ( friction.x * _delta), + calc_inertia.x, h_speed.x) + else: + ## no residual acceleration (friction) applies, kill the momentum + calc_inertia.x = 0.0 + + if (h_speed != debug_speed_tracker): + print("Inertia SpeedShift: ", debug_speed_tracker, h_speed, (0.0 == -0.0)) + debug_speed_tracker = h_speed + else: ## inertia is just base speed calc_inertia.x = h_speed.x acceleration = Vector2.ZERO - #calc_acceleration.x = resolve_h_acceleration(movement_params, h_speed, move_direction.x) - - ## One idea, pass all the calculations independently -# var movement = { -# "velocity_PPS" : calc_velocity, -# "acceleration_PPS" : calc_acceleration, -# "inertia_PPS" : calc_inertia -# } - - ## Another idea, just return the calculated velocity in PPS - ## calc x component of felicty - + + ## Another idea, just return the calculated velocity in PPS ## For now, y component of velocity is just gravity calc_velocity.y = movement_params["_gravity"] * Vector2.DOWN.y - - calc_velocity.x = calc_inertia.x * move_direction.x + ## calc x component of felicty + if calc_inertial_dir.x != 0: + calc_velocity.x = calc_inertia.x * calc_inertial_dir.x + else: + calc_velocity.x = calc_inertia.x * move_direction.x + calc_inertial_dir = Vector2(sign(calc_velocity.x),sign(calc_velocity.y)) UiManager.debug_text = ( #"H_Speed x Dir: " + str(h_speed) + str('duh') + "H_Speed: {0}, {1}".format({"0":"%5.2f" % h_speed.x, "1":"%5.2f" % h_speed.y}) + @@ -266,13 +282,15 @@ func resolve_h_acceleration(_params :Dictionary, _speed :Vector2, _move_directio var _acceleration :float = 0.0 if _params["_base_h_move_speed_modifier"] != 0: _acceleration = _params["_base_h_move_acceleration"] + _params["_base_h_move_modifier_move_acceleration"] -# if sign(_acceleration) == sign(_move_direction): -# return _acceleration -# elif _move_direction == 0: -# return _acceleration * -1 + else: + _acceleration = _params["_base_h_move_acceleration"] + + ## Well this didn't work + #_acceleration = _params["_base_h_move_acceleration"] + _params["_base_h_move_modifier_move_acceleration"] + if _speed.x < _speed.y: return _acceleration - else: + elif _speed.x > _speed.y: return _acceleration * -1 return 0.0 diff --git a/src/actors/players/playerE/states/idle.tres b/src/actors/players/playerE/states/idle.tres index e9e77fe..52d8b7f 100644 --- a/src/actors/players/playerE/states/idle.tres +++ b/src/actors/players/playerE/states/idle.tres @@ -10,7 +10,7 @@ debug_state = false timeout_seconds = 0.0 name = "idle" horizontal_speed = 1.36422e-12 -horizontal_acceleration = 0.0 +horizontal_acceleration = 2.0 horizontal_speed_offset = 0.0 horizontal_speed_offset_acceleration = 0.0 jerk_factor = 0.0