Simplify the edge wrap: 8 state variables instead of 15

Store only what can't be worked out: the corner, the surface normals we
leave and land on, where we started, where the arc begins, and the speed
and distance travelled. Arc radius and length, entry length, the exit
move and the sprite rotations are now small _wrap_... helpers.

_begin_wrap takes (corner, fromNormal, ontoNormal[, arcStartAngle])
instead of five loosely related values.

Same behaviour, except the wall-top wrap now always uses the arc's radius
(a straight sub-pixel entry instead of easing it during the arc) and turns
the sprite from the wall's rotation rather than wherever it was: at most
~0.5px and ~4 degrees different mid-wrap, ending in the same place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SnekInAScarf 2026-11-01 01:13:01 -07:00
parent cfea34861f
commit 06789e2f36

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.
# 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 wrapCorner := Vector2.ZERO
var wrapEntryStart := Vector2.ZERO
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 wrapFromNormal := Vector2.UP # The surface we're leaving.
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 wrapDistanceTravelled := 0.0
@ -421,11 +415,8 @@ func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool:
if corner == null:
return false
# Slide to the corner, swing around it, then carry on down the side so we end flat against it.
var arcEndAngle := direction.angle()
var exitOffset := -normal * _get_radius()
var arcStartAngle := normal.angle()
return _begin_wrap(corner, arcEndAngle, exitOffset, direction, arcStartAngle)
# Off the surface we're on, onto the side of the platform past the edge.
return _begin_wrap(corner, normal, direction)
# 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
var corner := Vector2(sideHit.position.x, topY)
var exitOffset := Vector2(-wallNormal * radius, 0) # Onto the platform, not balanced on the corner.
return _begin_wrap(corner, Vector2.UP.angle(), exitOffset, Vector2.UP)
# We're already part way around the corner, so start the arc from where we are.
var arcStartAngle := (currentPosition - corner).angle()
return _begin_wrap(corner, Vector2(wallNormal, 0), Vector2.UP, arcStartAngle)
# ---- Edge wrap ----
# Swings around `corner` to arcEndAngle, then moves by exitOffset, ending stuck
# to the surface facing ontoNormal. With arcStartAngle, first moves straight to
# that point on the arc. Returns false if there's no room at the end.
func _begin_wrap(corner: Vector2, arcEndAngle: float, exitOffset: Vector2, ontoNormal: Vector2, arcStartAngle := NAN) -> bool:
var arcRadius := _get_radius() + WRAP_MARGIN
var endPosition := corner + Vector2.from_angle(arcEndAngle) * arcRadius + exitOffset
# Swings around `corner`, from the surface facing fromNormal onto the one facing
# ontoNormal. First moves straight to the start of the arc (at arcStartAngle
# around the corner, or straight out from the surface we're leaving if not
# given). Returns false if there's no room at the end.
func _begin_wrap(corner: Vector2, fromNormal: Vector2, ontoNormal: Vector2, arcStartAngle := NAN) -> bool:
# 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):
return false
wrapCorner = corner
wrapEntryStart = global_position
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
wrapFromNormal = fromNormal
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
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:
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)
# Entry.
var distance := wrapDistanceTravelled
if distance < wrapEntryLength:
var arcStart := wrapCorner + Vector2.from_angle(wrapFromAngle) * wrapFromDistance
global_position = wrapEntryStart.lerp(arcStart, distance / wrapEntryLength)
if distance < entryLength:
global_position = wrapEntryStart.lerp(_wrap_arc_start(), distance / entryLength)
return
distance -= wrapEntryLength
distance -= entryLength
# Arc. Progress goes from 0 to 1.
var arcProgress := 1.0
if wrapArcLength > 0.0:
arcProgress = minf(distance / wrapArcLength, 1.0)
var angle := lerp_angle(wrapFromAngle, wrapToAngle, arcProgress)
var distanceFromCorner := lerpf(wrapFromDistance, wrapToDistance, arcProgress)
var positionOnArc := wrapCorner + Vector2.from_angle(angle) * distanceFromCorner
sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress)
if arcLength > 0.0:
arcProgress = minf(distance / arcLength, 1.0)
var angle := lerp_angle(wrapArcStartAngle, wrapOntoNormal.angle(), arcProgress)
var positionOnArc := wrapCorner + Vector2.from_angle(angle) * _wrap_radius()
sprite.rotation = lerp_angle(_rotation_for(wrapFromNormal), _rotation_for(wrapOntoNormal), arcProgress)
# Exit.
var exitProgress := 0.0
if wrapExitOffset != Vector2.ZERO:
exitProgress = maxf(distance - wrapArcLength, 0.0) / wrapExitOffset.length()
global_position = positionOnArc + wrapExitOffset * exitProgress
var exitProgress := maxf(distance - arcLength, 0.0) / exitOffset.length()
global_position = positionOnArc + exitOffset * exitProgress
if wrapDistanceTravelled >= totalLength:
_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:
# 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.
@ -601,12 +610,7 @@ func _finish_wrap(delta: float) -> void:
floor_block_on_wall = savedBlockOnWall
# Keep our speed along the new surface so leaving the wrap isn't a sudden stop.
var exitDirection: Vector2
if wrapExitOffset != Vector2.ZERO:
exitDirection = wrapExitOffset.normalized()
else:
exitDirection = Vector2.from_angle(wrapToAngle).orthogonal()
velocity = exitDirection * wrapSpeed
velocity = -wrapFromNormal * wrapSpeed
isWrapping = false
isStickingToCeiling = wrapOntoNormal.y > 0.5