Compare commits

..

2 Commits

Author SHA1 Message Date
675a0181f0 Generalized resolve_inertia function. Have a bug on dash after land. 2025-05-25 18:40:21 -07:00
25046c852f Jump bug fixed. 2025-05-25 13:41:14 -07:00

View File

@ -89,14 +89,26 @@ func calculate_velocity(_delta :float,
(movement_parameters.get_speed_end(NEGATIVE_DIRECTION).x * NEGATIVE_DIRECTION)
)
## It's time to start treating horizontal and vertical movement the same
var h_speed = Vector2(start_speed, end_speed)
#var h_speed = Vector2(movement_parameters.get_speed_start(RELATIVE_DIRECTION).x , movement_parameters.get_speed_end(RELATIVE_DIRECTION).x)
#var v_speed = resolve_v_speed(movement_parameters)
var v_speed = Vector2(movement_parameters.get_speed_start(0).y, movement_parameters.get_speed_end(0).y)
start_speed = (
(movement_parameters.get_speed_start(RELATIVE_DIRECTION).y * move_direction.y) +
(movement_parameters.get_speed_start(POSITIVE_DIRECTION).y * POSITIVE_DIRECTION) +
(movement_parameters.get_speed_start(NEGATIVE_DIRECTION).y * NEGATIVE_DIRECTION)
)
end_speed = (
(movement_parameters.get_speed_end(RELATIVE_DIRECTION).y * move_direction.y) +
(movement_parameters.get_speed_end(POSITIVE_DIRECTION).y * POSITIVE_DIRECTION) +
(movement_parameters.get_speed_end(NEGATIVE_DIRECTION).y * NEGATIVE_DIRECTION)
)
var v_speed = Vector2(start_speed, end_speed)
## Speed will now be expected to move in the direction of travel
#h_speed *= move_direction.x
v_speed *= move_direction.y
#v_speed *= move_direction.y
## Now determine placement of current velocity to speed range
var h_range_placement = placement_to_speed_range(calc_velocity.x, h_speed)
@ -120,7 +132,6 @@ 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
@ -128,62 +139,80 @@ func calculate_velocity(_delta :float,
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
if debug and movement_parameters.debug_name == 'move':
## 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))
calc_inertia.x = resolve_inertia(
h_range_placement,
calc_inertia.x ,
h_speed,
(calc_acceleration.x * _delta)
)
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)
## 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
## meaning there's always a downward acceleration so we don't have to check.
if v_speed.x != v_speed.y: ## For now gravity is just the default acceleration
var direction_accel :float = movement_parameters.gravity * _delta #* move_direction.x
if v_speed.x > v_speed.y:
@ -196,8 +225,8 @@ func calculate_velocity(_delta :float,
_v_impulse_applied = true
#impulse_applied_dir = move_direction.x
calc_inertia.y = clamp(calc_inertia.y + direction_accel,
min(calc_inertia.x, v_speed.y),
max(calc_inertia.x, v_speed.y))
min(calc_inertia.y, v_speed.y),
max(calc_inertia.y, v_speed.y))
RANGE_PLACEMENT.WITHIN_RANGE:
## If we're within the range but our speed has just changed
if _v_impulse_applied == false and _v_impulse_speed_tracking != v_speed:
@ -208,13 +237,19 @@ func calculate_velocity(_delta :float,
min(v_speed.x, v_speed.y),
max(v_speed.x, v_speed.y))
RANGE_PLACEMENT.PAST_RANGE:
if _v_impulse_applied == false and abs(v_speed.x) > abs(v_speed.y):
calc_inertia.y = v_speed.y
_v_impulse_applied = true
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
#calc_inertia.y += movement_parameters.gravity * _delta
if v_speed.x < v_speed.y:
v_speed.x = clamp(v_speed.x,0.0,v_speed.y)
## Move back towards the base speed
calc_inertia.y = move_toward(calc_inertia.y, v_speed.x, movement_parameters.gravity * _delta)
## Track or last speed for in range impulses
_h_impulse_speed_tracking = h_speed
@ -272,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:
@ -326,3 +323,74 @@ func resolve_move_direction(_momentum :Vector2,
vertical_movement_direction = sign(_momentum.y)
return Vector2(horizontal_movement_direction, vertical_movement_direction)
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:
# inertia += speed_range.x
# _h_impulse_applied = true
#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
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
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