Simplify the edge wrap: 8 state variables instead of 15 #9

Merged
SnekInAScarf merged 1 commit from simpler-wrap into main 2026-11-01 02:19:41 -06:00

View file

@ -60,20 +60,14 @@ var wallFacingY := -1.0 # Last way we moved along a wall (-1 up). Our "facing" o
var isHoppingAcross := false # No air steering mid-hop, so it lands where it was aimed. var isHoppingAcross := false # No air steering mid-hop, so it lands where it was aimed.
# A wrap is a straight "entry" to the arc, a quarter-circle "arc" around the # A wrap is a straight "entry" to the arc, a quarter-circle "arc" around the
# corner, then a straight "exit" onto the new surface. # corner, then a straight "exit" onto the new surface. Everything else about the
# path is worked out from these (see the _wrap_... helpers).
var isWrapping := false var isWrapping := false
var wrapCorner := Vector2.ZERO var wrapCorner := Vector2.ZERO
var wrapEntryStart := Vector2.ZERO var wrapFromNormal := Vector2.UP # The surface we're leaving.
var wrapEntryLength := 0.0
var wrapFromAngle := 0.0 # Angles around the corner.
var wrapToAngle := 0.0
var wrapFromDistance := 0.0 # Distances from the corner.
var wrapToDistance := 0.0
var wrapArcLength := 0.0
var wrapExitOffset := Vector2.ZERO
var wrapFromRotation := 0.0 # Sprite rotation.
var wrapToRotation := 0.0
var wrapOntoNormal := Vector2.UP # The surface we'll end up on. var wrapOntoNormal := Vector2.UP # The surface we'll end up on.
var wrapEntryStart := Vector2.ZERO # Where we were when the wrap started.
var wrapArcStartAngle := 0.0 # Angle around the corner where the arc begins.
var wrapSpeed := 0.0 var wrapSpeed := 0.0
var wrapDistanceTravelled := 0.0 var wrapDistanceTravelled := 0.0
@ -421,11 +415,8 @@ func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool:
if corner == null: if corner == null:
return false return false
# Slide to the corner, swing around it, then carry on down the side so we end flat against it. # Off the surface we're on, onto the side of the platform past the edge.
var arcEndAngle := direction.angle() return _begin_wrap(corner, normal, direction)
var exitOffset := -normal * _get_radius()
var arcStartAngle := normal.angle()
return _begin_wrap(corner, arcEndAngle, exitOffset, direction, arcStartAngle)
# The corner where our surface ends, if within `reach` ahead in `direction`, else null. # The corner where our surface ends, if within `reach` ahead in `direction`, else null.
@ -507,41 +498,33 @@ func _start_wall_top_wrap() -> bool:
return false return false
var corner := Vector2(sideHit.position.x, topY) var corner := Vector2(sideHit.position.x, topY)
var exitOffset := Vector2(-wallNormal * radius, 0) # Onto the platform, not balanced on the corner. # We're already part way around the corner, so start the arc from where we are.
return _begin_wrap(corner, Vector2.UP.angle(), exitOffset, Vector2.UP) var arcStartAngle := (currentPosition - corner).angle()
return _begin_wrap(corner, Vector2(wallNormal, 0), Vector2.UP, arcStartAngle)
# ---- Edge wrap ---- # ---- Edge wrap ----
# Swings around `corner` to arcEndAngle, then moves by exitOffset, ending stuck # Swings around `corner`, from the surface facing fromNormal onto the one facing
# to the surface facing ontoNormal. With arcStartAngle, first moves straight to # ontoNormal. First moves straight to the start of the arc (at arcStartAngle
# that point on the arc. Returns false if there's no room at the end. # around the corner, or straight out from the surface we're leaving if not
func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoNormal: Vector2, arcStartAngle := NAN) -> bool: # given). Returns false if there's no room at the end.
var arcRadius := _get_radius() + WRAP_MARGIN func _begin_wrap(corner: Vector2, fromNormal: Vector2, ontoNormal: Vector2, arcStartAngle := NAN) -> bool:
var endPosition := corner + Vector2.from_angle(arcEndAngle) * arcRadius + exitOffset # Round to exact directions, e.g. (1, 0.0000001) to (1, 0).
fromNormal = fromNormal.round()
ontoNormal = ontoNormal.round()
var endPosition := corner + ontoNormal * _wrap_radius() - fromNormal * _get_radius()
if not _is_spot_free(endPosition): if not _is_spot_free(endPosition):
return false return false
wrapCorner = corner wrapCorner = corner
wrapEntryStart = global_position wrapFromNormal = fromNormal
if is_nan(arcStartAngle):
var fromCorner := global_position - corner
wrapFromAngle = fromCorner.angle()
wrapFromDistance = fromCorner.length()
wrapEntryLength = 0.0
else:
wrapFromAngle = arcStartAngle
wrapFromDistance = arcRadius
var arcStart := corner + Vector2.from_angle(arcStartAngle) * arcRadius
wrapEntryLength = global_position.distance_to(arcStart)
wrapToAngle = arcEndAngle
wrapToDistance = arcRadius
wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * arcRadius # Angle x radius.
wrapExitOffset = exitOffset
wrapFromRotation = sprite.rotation
wrapToRotation = ontoNormal.angle() + PI / 2
wrapOntoNormal = ontoNormal wrapOntoNormal = ontoNormal
wrapEntryStart = global_position
if is_nan(arcStartAngle): # NAN ("not a number") means it wasn't given.
arcStartAngle = fromNormal.angle()
wrapArcStartAngle = arcStartAngle
wrapDistanceTravelled = 0.0 wrapDistanceTravelled = 0.0
wrapSpeed = minf(velocity.length(), GROUND_SPEED) # Keep our speed going in. wrapSpeed = minf(velocity.length(), GROUND_SPEED) # Keep our speed going in.
@ -553,35 +536,61 @@ func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoN
func _update_wrap(delta: float) -> void: func _update_wrap(delta: float) -> void:
wrapSpeed = move_toward(wrapSpeed, GROUND_SPEED, GROUND_ACCEL * delta) wrapSpeed = move_toward(wrapSpeed, GROUND_SPEED, GROUND_ACCEL * delta)
var totalLength := wrapEntryLength + wrapArcLength + wrapExitOffset.length() var entryLength := _wrap_entry_length()
var arcLength := _wrap_arc_length()
var exitOffset := _wrap_exit_offset()
var totalLength := entryLength + arcLength + exitOffset.length()
wrapDistanceTravelled = minf(wrapDistanceTravelled + wrapSpeed * delta, totalLength) wrapDistanceTravelled = minf(wrapDistanceTravelled + wrapSpeed * delta, totalLength)
# Entry. # Entry.
var distance := wrapDistanceTravelled var distance := wrapDistanceTravelled
if distance < wrapEntryLength: if distance < entryLength:
var arcStart := wrapCorner + Vector2.from_angle(wrapFromAngle) * wrapFromDistance global_position = wrapEntryStart.lerp(_wrap_arc_start(), distance / entryLength)
global_position = wrapEntryStart.lerp(arcStart, distance / wrapEntryLength)
return return
distance -= wrapEntryLength distance -= entryLength
# Arc. Progress goes from 0 to 1. # Arc. Progress goes from 0 to 1.
var arcProgress := 1.0 var arcProgress := 1.0
if wrapArcLength > 0.0: if arcLength > 0.0:
arcProgress = minf(distance / wrapArcLength, 1.0) arcProgress = minf(distance / arcLength, 1.0)
var angle := lerp_angle(wrapFromAngle, wrapToAngle, arcProgress) var angle := lerp_angle(wrapArcStartAngle, wrapOntoNormal.angle(), arcProgress)
var distanceFromCorner := lerpf(wrapFromDistance, wrapToDistance, arcProgress) var positionOnArc := wrapCorner + Vector2.from_angle(angle) * _wrap_radius()
var positionOnArc := wrapCorner + Vector2.from_angle(angle) * distanceFromCorner sprite.rotation = lerp_angle(_rotation_for(wrapFromNormal), _rotation_for(wrapOntoNormal), arcProgress)
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress)
# Exit. # Exit.
var exitProgress := 0.0 var exitProgress := maxf(distance - arcLength, 0.0) / exitOffset.length()
if wrapExitOffset != Vector2.ZERO: global_position = positionOnArc + exitOffset * exitProgress
exitProgress = maxf(distance - wrapArcLength, 0.0) / wrapExitOffset.length()
global_position = positionOnArc + wrapExitOffset * exitProgress
if wrapDistanceTravelled >= totalLength: if wrapDistanceTravelled >= totalLength:
_finish_wrap(delta) _finish_wrap(delta)
# ---- Wrap path, worked out from the wrap... variables ----
# How far from the corner the arc keeps us.
func _wrap_radius() -> float:
return _get_radius() + WRAP_MARGIN
func _wrap_arc_start() -> Vector2:
return wrapCorner + Vector2.from_angle(wrapArcStartAngle) * _wrap_radius()
func _wrap_entry_length() -> float:
return wrapEntryStart.distance_to(_wrap_arc_start())
# The arc ends pointing straight out of the new surface. Arc length = angle x radius.
func _wrap_arc_length() -> float:
return absf(angle_difference(wrapArcStartAngle, wrapOntoNormal.angle())) * _wrap_radius()
# After the arc, carry on along the new surface (away from the one we left) so
# our side ends up flat against it.
func _wrap_exit_offset() -> Vector2:
return -wrapFromNormal * _get_radius()
# The sprite rotation for standing on a surface with this normal, kept between -PI and PI.
func _rotation_for(normal: Vector2) -> float:
return wrapf(normal.angle() + PI / 2, -PI, PI)
func _finish_wrap(delta: float) -> void: func _finish_wrap(delta: float) -> void:
# Close the WRAP_MARGIN gap with move_and_slide() so is_on_floor()/is_on_wall() # Close the WRAP_MARGIN gap with move_and_slide() so is_on_floor()/is_on_wall()
# describe where we are now, not where we were before the wrap. # describe where we are now, not where we were before the wrap.
@ -601,12 +610,7 @@ func _finish_wrap(delta: float) -> void:
floor_block_on_wall = savedBlockOnWall floor_block_on_wall = savedBlockOnWall
# Keep our speed along the new surface so leaving the wrap isn't a sudden stop. # Keep our speed along the new surface so leaving the wrap isn't a sudden stop.
var exitDirection: Vector2 velocity = -wrapFromNormal * wrapSpeed
if wrapExitOffset != Vector2.ZERO:
exitDirection = wrapExitOffset.normalized()
else:
exitDirection = Vector2.from_angle(wrapToAngle).orthogonal()
velocity = exitDirection * wrapSpeed
isWrapping = false isWrapping = false
isStickingToCeiling = wrapOntoNormal.y > 0.5 isStickingToCeiling = wrapOntoNormal.y > 0.5