Oh that pesky impulse control.

This commit is contained in:
Dustin 2025-05-04 23:32:28 -07:00
parent 061ceb76b9
commit c6de01ba1a
5 changed files with 54 additions and 13 deletions

View File

@ -42,6 +42,8 @@ func apply_state_modifier(_modifier :StateModifierMovement, _movement_direction
##TODO: Um, modifiers are a little weirder for this
move_speed_modifier.x += _modifier.horizontal_speed_offset
move_speed_modifier_acceleration.x += _modifier.horizontal_speed_offset_acceleration
move_speed_modifier.y += _modifier.vertical_speed_offset
move_speed_modifier_acceleration.y += _modifier.vertical_speed_offset_acceleration
_:
base_move_speed.x += _modifier.horizontal_speed
base_move_acceleration.x += _modifier.horizontal_acceleration

View File

@ -191,9 +191,9 @@ 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(calc_inertial_dir, _movement_override_normal)
move_direction = resolve_move_direction(calc_inertia, _movement_override_normal)
else:
move_direction = resolve_move_direction(calc_inertial_dir, desired_movement_vector)
move_direction = resolve_move_direction(calc_inertia, desired_movement_vector)
##WIP: the impulse logic just doesn't work still, below is another attempt
# if impulse_applied_dir != move_direction.x and sign(calc_velocity.x) != impulse_applied_dir:
@ -221,8 +221,10 @@ func new_move_actor_as_desired(_delta :float,
# We move towards h_speed.y at the acceleration rate
##
var h_speed = resolve_h_speed(movement_parameters)
var v_speed = resolve_v_speed(movement_parameters)
## Speed will now be expected to move in the direction of travel
h_speed *= move_direction.x
v_speed *= move_direction.y
## Reset the impulse when back in range
# may also want to reset when hspeed is static
@ -259,7 +261,7 @@ func new_move_actor_as_desired(_delta :float,
## Start speed really shouldn't be less than zero (modifiers can do this)
## If we have mismatched signs we'll get really bad behavior
if h_speed.x != 0 and sign(h_speed.y) != sign(h_speed.x):
if (h_speed.x != 0 and h_speed.y != 0 ) and sign(h_speed.y) != sign(h_speed.x):
if h_speed.x < h_speed.y:
h_speed.x = clamp(h_speed.x, 0.0, h_speed.y)
else:
@ -325,9 +327,22 @@ func new_move_actor_as_desired(_delta :float,
## 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
if v_speed.x != v_speed.y: ## For now gravity is just the default acceleration
if (v_speed.x != 0 and v_speed.y != 0) and sign(v_speed.y) != sign(v_speed.x):
if v_speed.x < v_speed.y:
v_speed.x = clamp(v_speed.x, 0.0, v_speed.y)
else:
v_speed.y = clamp(v_speed.y, 0.0, v_speed.x)
#print (is_out_of_range(calc_inertia.y, v_speed.x - sign(v_speed.x), v_speed.y - sign(v_speed.y)))
if is_out_of_range(calc_inertia.y, v_speed.x , v_speed.y):
## ? But we only want to do this once though.
calc_inertia.y += v_speed.x
## Apply acceleration
calc_inertia.y = move_toward(calc_inertia.y, v_speed.y, movement_parameters.gravity * _delta)
else:
## The previous vertical movement methods
calc_inertia.y += movement_parameters.gravity * _delta
calc_velocity.y = calc_inertia.y
## calc x component of felicty
@ -347,6 +362,7 @@ func new_move_actor_as_desired(_delta :float,
UiManager.debug_text = ( #"H_Speed x Dir: " + str(h_speed) + str('duh') +
modifier_indicator + "H_Speed Fm:{0} To:{1}".format({"0":"%5.2f" % h_speed.x, "1":"%5.2f" % h_speed.y}) +
"\nV_Speed Fm:{0} To:{1}".format({"0":"%5.2f" % v_speed.x, "1":"%5.2f" % v_speed.y}) +
"\nVelocity_Calc: {0}, {1}".format({"0":"%5.2f" % calc_velocity.x, "1":"%5.2f" % calc_velocity.y}) +
"\nInertia_Calc: {0}, {1}".format({"0":"%5.2f" % calc_inertia.x, "1":"%5.2f" % calc_inertia.y}) +
"\nVelocity_Real: {0}, {1}".format({"0":"%5.2f" % velocity.x, "1":"%5.2f" % velocity.y}) +
@ -362,7 +378,7 @@ func new_move_actor_as_desired(_delta :float,
func is_out_of_range(value: float, a: float, b: float) -> bool:
var lower = min(a, b)
var upper = max(a, b)
return value < lower or value > upper
return value <= lower or value >= upper
## Passed in acceleration, vel, etc is applied to whatever
## the parent is. If Kinematic then move_and_slide, otherwise manually adjusted
@ -385,6 +401,15 @@ func resolve_h_speed(_params :MovementParameters) -> Vector2:
return(Vector2(base_speed, speed_differance))
return Vector2(base_speed,base_speed)
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)
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
@ -423,7 +448,13 @@ func resolve_move_direction(_momentum :Vector2,
elif sign(_momentum.x): ## We still have momentum but not trying to move
horizontal_movement_direction = sign(_momentum.x)
return Vector2(horizontal_movement_direction,0)
var vertical_movement_direction:float
if sign(_movement_direction.y):
vertical_movement_direction = sign(_movement_direction.y)
elif sign(_momentum.y):
vertical_movement_direction = sign(_momentum.y)
return Vector2(horizontal_movement_direction, vertical_movement_direction)
## About to DEPR this one for redesigned one above

View File

@ -42,6 +42,7 @@ func wants_jump() -> bool:
##Bugfix: Both players jump.
#desired_movement_vector.y = UP
_wants_jump = true
desired_movement_vector.y = UP
return true
else:
_wants_jump = false
@ -308,6 +309,7 @@ func _state_process_physics_jump():
# request_state_change.call_func('idle')
if velocity.y > 0:
request_state_change.call_func('fall')
desired_movement_vector.y = UP
#move_actor_as_desired()
apply_movement_to_parent(new_move_actor_as_desired( physics_delta , current_state ))
@ -447,9 +449,9 @@ func _state_process_physics_crouch_move():
#func _on_state_entered_idle():
# print("Movement State Idle State Entered!")
func _on_state_entered_jump():
##TODO: Make this a state export variable
# I used to call it 'jump_force' like an idiot.
velocity.y = -200
##WIP: Make this a state export variable
#func _on_state_entered_jump():
# # I used to call it 'jump_force' like an idiot.
# velocity.y = -200

View File

@ -14,9 +14,9 @@ horizontal_acceleration = 0.0
horizontal_speed_offset = 0.0
horizontal_speed_offset_acceleration = 0.0
jerk_factor = 1.0
vertical_speed = 0.0
vertical_speed = 200.0
vertical_acceleration = 0.0
vertical_speed_offset = 200.0
vertical_speed_offset = -200.0
vertical_speed_offset_acceleration = 0.0
preserve_inertia = true
is_grounded = false

View File

@ -14,4 +14,10 @@ horizontal_acceleration = 70.0
horizontal_speed_offset = 80.0
horizontal_speed_offset_acceleration = 0.0
jerk_factor = 0.0
vertical_speed = 0.0
vertical_acceleration = 0.0
vertical_speed_offset = 0.0
vertical_speed_offset_acceleration = 0.0
preserve_inertia = true
is_grounded = true
animation_sequence = [ "run" ]