From 43380dd72c8b77f6fa3572daefe23d18cec89a5d Mon Sep 17 00:00:00 2001 From: SnekInAScarf Date: Sat, 31 Oct 2026 12:39:20 -0700 Subject: [PATCH 1/2] Smoothly wrap around ceiling lips and wall tops Instead of relying on physics to slide the round collider around a corner, find the corner with raycasts and swing the slime around it on a quarter circle at crawling speed, rotating the sprite along with it. - Hanging on a ceiling lip + up: swing onto the wall above. - Climbing up past the top of a wall: swing over onto the floor on top. - Tapping up up to 0.15s before reaching a lip still counts. - Falls back to the old behaviour if the corner can't be found or there is no room at the end of the swing. Co-Authored-By: Claude Opus 5.5 --- Characters/player.gd | 123 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/Characters/player.gd b/Characters/player.gd index 3339703..06e4c23 100755 --- a/Characters/player.gd +++ b/Characters/player.gd @@ -11,6 +11,7 @@ const WALL_STICK_SPEED = 20.0 # Small push into the wall that keeps the slime pr # Temp for now, need to use aniamtedspite2d later on @onready var sprite = $Sprite2D +@onready var collisionShape: CollisionShape2D = $CollisionShape2D # How long to press opposite direction before unsticking from a wall/ceiling const MAX_SECS_TO_UNSTICK = 0.5 @@ -29,7 +30,27 @@ var lipDirection := 0.0 # Which way we were moving when we went over the lip. # so don't count that towards unsticking until the direction is released. var ignoreWallUnstickUntilRelease := false +# Edge wrapping: instead of relying on physics to slide around a corner, the +# slime swings around it on a quarter circle so it always feels the same. +const WRAP_MARGIN = 0.5 # Extra gap from the corner so we don't clip into it. +const CLIMB_INPUT_BUFFER = 0.15 # Tapping up this long before reaching a lip still counts. +var isWrapping := false +var wrapCorner := Vector2.ZERO +var wrapFromAngle := 0.0 +var wrapToAngle := 0.0 +var wrapFromDistance := 0.0 +var wrapToDistance := 0.0 +var wrapFromRotation := 0.0 +var wrapToRotation := 0.0 +var wrapDuration := 0.0 +var wrapProgress := 0.0 +var wrapOntoWallNormalX := 0.0 # 0 means we end up on a floor instead of a wall. +var timeSinceUpPressed := 1.0 + func _process(delta: float) -> void: + # The wrap sets the sprite rotation itself. + if isWrapping: return + # Update sprite to be on the wall or ceiling. In a corner only one of them # is ever true (that gets decided at the end of _physics_process). if not is_on_floor() and isStickingToWall: @@ -44,6 +65,19 @@ func _physics_process(delta: float) -> void: var horizontalInput := Input.get_axis("moveLeft", "moveRight") var verticalInput := Input.get_axis("moveDown", "moveUp") + if Input.is_action_just_pressed("moveUp"): timeSinceUpPressed = 0.0 + else: timeSinceUpPressed += delta + + # Swinging around a corner, ignore everything else until we're done. + if isWrapping: + _update_wrap(delta) + return + + # Hanging on a lip and pressing up: swing around onto the wall above. + var wantsToClimb := verticalInput > 0 or timeSinceUpPressed <= CLIMB_INPUT_BUFFER + if isClimbingLip and wantsToClimb and _start_lip_wrap(): + return + # Quick flip sprite thing. if horizontalInput: if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0) @@ -93,10 +127,16 @@ func _physics_process(delta: float) -> void: else: velocity.x = move_toward(velocity.x, 0, GROUND_SPEED) + var wasOnWall := isStickingToWall and not is_on_floor() + move_and_slide() # Check if we should be sticking to walls/ceiling after moving our player. isStickingToWall = is_on_wall() + # Climbed past the top of a wall: swing over onto the floor on top. + if wasOnWall and not isStickingToWall and verticalInput > 0 \ + and timeSpentUnstickingWall < MAX_SECS_TO_UNSTICK and _start_wall_top_wrap(): + return if isStickingToWall: wallNormalX = get_wall_normal().x @@ -126,3 +166,86 @@ func _physics_process(delta: float) -> void: isStickingToWall = false else: isStickingToCeiling = false + +func _get_radius() -> float: + return (collisionShape.shape as CircleShape2D).radius + +func _cast_ray(from: Vector2, to: Vector2) -> Dictionary: + var query := PhysicsRayQueryParameters2D.create(from, to, collision_mask, [get_rid()]) + return get_world_2d().direct_space_state.intersect_ray(query) + +func _is_spot_free(spot: Vector2) -> bool: + var query := PhysicsShapeQueryParameters2D.new() + query.shape = collisionShape.shape + query.transform = Transform2D(0.0, spot) + query.collision_mask = collision_mask + query.exclude = [get_rid()] + return get_world_2d().direct_space_state.intersect_shape(query, 1).is_empty() + +# Just went over a ceiling edge: find the corner and swing up beside the wall. +func _start_lip_wrap() -> bool: + var dir := lipDirection + var radius := _get_radius() + var pos := global_position + # Bottom of the platform we just came off of. + var ceilingHit := _cast_ray(pos - Vector2(dir * (radius + 2), 0), pos - Vector2(dir * (radius + 2), radius * 2)) + if not ceilingHit or ceilingHit.normal.y < 0.5: return false + var ceilingY: float = ceilingHit.position.y + # Side of the platform, just above its bottom. + var faceHit := _cast_ray(Vector2(pos.x + dir * radius, ceilingY - 2), Vector2(pos.x - dir * radius * 2, ceilingY - 2)) + if not faceHit or faceHit.normal.x * dir < 0.5: return false + var corner := Vector2(faceHit.position.x, ceilingY) + return _begin_wrap(corner, Vector2(dir, 0).angle(), PI/2 * dir, dir) + +# Just climbed past the top of a wall: find the corner and swing up on top. +func _start_wall_top_wrap() -> bool: + var n := wallNormalX + var radius := _get_radius() + var pos := global_position + # Top of the platform, just past the wall. + var topHit := _cast_ray(pos + Vector2(-n * (radius + 2), -radius), pos + Vector2(-n * (radius + 2), radius * 2)) + if not topHit or topHit.normal.y > -0.5: return false + var topY: float = topHit.position.y + # Side of the platform, just below its top. + var faceHit := _cast_ray(Vector2(pos.x + n * 2, topY + 2), Vector2(pos.x - n * radius * 2, topY + 2)) + if not faceHit or faceHit.normal.x * n < 0.5: return false + var corner := Vector2(faceHit.position.x, topY) + return _begin_wrap(corner, -PI/2, 0.0, 0.0) + +# Starts swinging around corner, ending at toAngle from it. Returns false if +# there's no room to end up there. +func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNormalX: float) -> bool: + var radius := _get_radius() + WRAP_MARGIN + if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius): return false + var offset := global_position - corner + wrapCorner = corner + wrapFromAngle = offset.angle() + wrapToAngle = toAngle + wrapFromDistance = offset.length() + wrapToDistance = radius + wrapFromRotation = sprite.rotation + wrapToRotation = toRotation + wrapOntoWallNormalX = ontoWallNormalX + # Keep the same speed as crawling along a surface. + var arcLength := absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius + wrapDuration = maxf(arcLength / GROUND_SPEED, 0.05) + wrapProgress = 0.0 + velocity = Vector2.ZERO + isWrapping = true + return true + +func _update_wrap(delta: float) -> void: + wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0) + var angle := lerp_angle(wrapFromAngle, wrapToAngle, wrapProgress) + var distance := lerpf(wrapFromDistance, wrapToDistance, wrapProgress) + global_position = wrapCorner + Vector2.from_angle(angle) * distance + sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, wrapProgress) + if wrapProgress < 1.0: return + + isWrapping = false + isClimbingLip = false + isStickingToCeiling = false + isStickingToWall = wrapOntoWallNormalX != 0.0 + if isStickingToWall: + wallNormalX = wrapOntoWallNormalX + ignoreWallUnstickUntilRelease = true From b098b78934a576c0f0a338824be255964ecb5b05 Mon Sep 17 00:00:00 2001 From: SnekInAScarf Date: Sat, 31 Oct 2026 12:46:08 -0700 Subject: [PATCH 2/2] Stay on the wall after wrapping around a lip The wrap ended with the slime level with the corner and WRAP_MARGIN away from the wall. The wall stick push only covers ~0.33px a frame, so it often didn't touch the wall on the next move_and_slide, stopped sticking and fell. - After the arc, carry on straight up the wall by the radius (or onto the platform for wall tops) so the slime ends flat against the surface. - Close the remaining gap with move_and_collide when the wrap finishes. Co-Authored-By: Claude Opus 5.5 --- Characters/player.gd | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/Characters/player.gd b/Characters/player.gd index 06e4c23..0055896 100755 --- a/Characters/player.gd +++ b/Characters/player.gd @@ -44,6 +44,8 @@ var wrapFromRotation := 0.0 var wrapToRotation := 0.0 var wrapDuration := 0.0 var wrapProgress := 0.0 +var wrapArcLength := 0.0 +var wrapExitOffset := Vector2.ZERO # Straight move after the arc, onto the new surface. var wrapOntoWallNormalX := 0.0 # 0 means we end up on a floor instead of a wall. var timeSinceUpPressed := 1.0 @@ -195,7 +197,8 @@ func _start_lip_wrap() -> bool: var faceHit := _cast_ray(Vector2(pos.x + dir * radius, ceilingY - 2), Vector2(pos.x - dir * radius * 2, ceilingY - 2)) if not faceHit or faceHit.normal.x * dir < 0.5: return false var corner := Vector2(faceHit.position.x, ceilingY) - return _begin_wrap(corner, Vector2(dir, 0).angle(), PI/2 * dir, dir) + # After the arc, carry on up the wall so our side is flat against it. + return _begin_wrap(corner, Vector2(dir, 0).angle(), Vector2(0, -radius), PI/2 * dir, dir) # Just climbed past the top of a wall: find the corner and swing up on top. func _start_wall_top_wrap() -> bool: @@ -210,13 +213,14 @@ func _start_wall_top_wrap() -> bool: var faceHit := _cast_ray(Vector2(pos.x + n * 2, topY + 2), Vector2(pos.x - n * radius * 2, topY + 2)) if not faceHit or faceHit.normal.x * n < 0.5: return false var corner := Vector2(faceHit.position.x, topY) - return _begin_wrap(corner, -PI/2, 0.0, 0.0) + # After the arc, carry on onto the platform instead of perching on its corner. + return _begin_wrap(corner, -PI/2, Vector2(-n * radius, 0), 0.0, 0.0) -# Starts swinging around corner, ending at toAngle from it. Returns false if -# there's no room to end up there. -func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNormalX: float) -> bool: +# Starts swinging around corner to toAngle from it, then moving by exitOffset. +# Returns false if there's no room to end up there. +func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotation: float, ontoWallNormalX: float) -> bool: var radius := _get_radius() + WRAP_MARGIN - if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius): return false + if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius + exitOffset): return false var offset := global_position - corner wrapCorner = corner wrapFromAngle = offset.angle() @@ -225,10 +229,11 @@ func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNor wrapToDistance = radius wrapFromRotation = sprite.rotation wrapToRotation = toRotation + wrapExitOffset = exitOffset wrapOntoWallNormalX = ontoWallNormalX # Keep the same speed as crawling along a surface. - var arcLength := absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius - wrapDuration = maxf(arcLength / GROUND_SPEED, 0.05) + wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius + wrapDuration = maxf((wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05) wrapProgress = 0.0 velocity = Vector2.ZERO isWrapping = true @@ -236,12 +241,24 @@ func _begin_wrap(corner: Vector2, toAngle: float, toRotation: float, ontoWallNor func _update_wrap(delta: float) -> void: wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0) - var angle := lerp_angle(wrapFromAngle, wrapToAngle, wrapProgress) - var distance := lerpf(wrapFromDistance, wrapToDistance, wrapProgress) - global_position = wrapCorner + Vector2.from_angle(angle) * distance - sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, wrapProgress) + # Progress is split between the arc and the straight exit by their lengths. + var travelled := wrapProgress * (wrapArcLength + wrapExitOffset.length()) + var arcProgress := 1.0 if wrapArcLength <= 0.0 else minf(travelled / wrapArcLength, 1.0) + var exitProgress := 0.0 if wrapExitOffset == Vector2.ZERO \ + else maxf(travelled - wrapArcLength, 0.0) / wrapExitOffset.length() + var angle := lerp_angle(wrapFromAngle, wrapToAngle, arcProgress) + var distance := lerpf(wrapFromDistance, wrapToDistance, arcProgress) + global_position = wrapCorner + Vector2.from_angle(angle) * distance + wrapExitOffset * exitProgress + sprite.rotation = lerp_angle(wrapFromRotation, wrapToRotation, arcProgress) if wrapProgress < 1.0: return + # Close the small gap left by WRAP_MARGIN so we're touching the new surface + # and the sticking checks after the next move_and_slide pick it up. + if wrapOntoWallNormalX != 0.0: + move_and_collide(Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0)) + else: + move_and_collide(Vector2(0, WRAP_MARGIN + 1)) + isWrapping = false isClimbingLip = false isStickingToCeiling = false