From 815444f3aff3d1b5981f2b146f9b3c70f7cc23fc Mon Sep 17 00:00:00 2001 From: SnekInAScarf Date: Sat, 31 Oct 2026 13:02:05 -0700 Subject: [PATCH 1/2] Slide over floor edges onto the wall below when pressing down Two RayCast2D edge sensors point down into the floor edgeSensorRange to each side of the slime. When one stops touching floor there's an edge in range on that side, and pressing down slides to it, swings over the corner and carries on down the wall so the slime ends flat against it. - Down + a direction picks that side; down alone goes the way the slime is facing, or the other way if only that side has an edge in range. - Down in the middle of a platform does nothing. - edgeSensorRange is exported (default 24, tuned with a headless sweep). - Wraps can now start with a straight move to the arc, and end with a move_and_slide so is_on_floor()/is_on_wall() aren't stale afterwards. Co-Authored-By: Claude Opus 5.5 --- Characters/player.gd | 85 +++++++++++++++++++++++++++++++++++++----- Characters/player.tscn | 8 ++++ 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/Characters/player.gd b/Characters/player.gd index 0055896..5c15d25 100755 --- a/Characters/player.gd +++ b/Characters/player.gd @@ -12,6 +12,8 @@ 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 +@onready var leftEdgeSensor: RayCast2D = $LeftEdgeSensor +@onready var rightEdgeSensor: RayCast2D = $RightEdgeSensor # How long to press opposite direction before unsticking from a wall/ceiling const MAX_SECS_TO_UNSTICK = 0.5 @@ -48,6 +50,20 @@ 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 +var wrapEntryStart := Vector2.ZERO # Where a straight move before the arc starts from. +var wrapEntryLength := 0.0 + +# Floor edges: the edge sensors point down into the floor a little way to each +# side. If one stops touching floor, there's an edge within edgeSensorRange on +# that side and pressing down will slide over it onto the wall below. +@export var edgeSensorRange := 24.0 + +func _ready() -> void: + var sensorDepth := _get_radius() + 4 + leftEdgeSensor.position = Vector2(-edgeSensorRange, 0) + leftEdgeSensor.target_position = Vector2(0, sensorDepth) + rightEdgeSensor.position = Vector2(edgeSensorRange, 0) + rightEdgeSensor.target_position = Vector2(0, sensorDepth) func _process(delta: float) -> void: # The wrap sets the sprite rotation itself. @@ -80,6 +96,10 @@ func _physics_process(delta: float) -> void: if isClimbingLip and wantsToClimb and _start_lip_wrap(): return + # On a floor near its edge and pressing down: slide over onto the wall below. + if is_on_floor() and verticalInput < 0 and _start_floor_edge_wrap(horizontalInput): + return + # Quick flip sprite thing. if horizontalInput: if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0) @@ -216,16 +236,52 @@ func _start_wall_top_wrap() -> bool: # 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) +# On a floor and pressing down: if there's an edge in range, slide to it and +# over onto the wall below. Holding a direction picks that side, otherwise we +# go the way we're facing, or the other way if only that side has an edge. +func _start_floor_edge_wrap(horizontalInput: float) -> bool: + if horizontalInput: + return _try_floor_edge_wrap(signf(horizontalInput)) + var facing := -1.0 if sprite.flip_h else 1.0 + return _try_floor_edge_wrap(facing) or _try_floor_edge_wrap(-facing) + +func _try_floor_edge_wrap(dir: float) -> bool: + var sensor := rightEdgeSensor if dir > 0 else leftEdgeSensor + sensor.force_raycast_update() + if sensor.is_colliding(): return false + var radius := _get_radius() + var pos := global_position + # Top of the floor we're on, checked from the side away from the edge. + var floorHit := _cast_ray(pos - Vector2(dir * radius * 0.5, 0), pos + Vector2(-dir * radius * 0.5, radius * 2)) + if not floorHit or floorHit.normal.y > -0.5: return false + var floorY: float = floorHit.position.y + # Side of the platform, just below its top, looking back from past the sensor. + var faceHit := _cast_ray(Vector2(pos.x + dir * (edgeSensorRange + 2), floorY + 2), Vector2(pos.x - dir * radius, floorY + 2)) + if not faceHit or faceHit.normal.x * dir < 0.5: return false + var corner := Vector2(faceHit.position.x, floorY) + # Slide to just above the corner, swing down beside the wall, then carry on + # down it so our side ends up flat against it. + return _begin_wrap(corner, Vector2(dir, 0).angle(), Vector2(0, radius), PI/2 * dir, dir, -PI/2) + # Starts swinging around corner to toAngle from it, then moving by exitOffset. +# If fromAngle is given, first moves straight to that point on the arc, +# otherwise the arc starts from where we are now. # 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: +func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotation: float, ontoWallNormalX: float, fromAngle := NAN) -> bool: var radius := _get_radius() + WRAP_MARGIN if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius + exitOffset): return false var offset := global_position - corner wrapCorner = corner - wrapFromAngle = offset.angle() + wrapEntryStart = global_position + if is_nan(fromAngle): + wrapFromAngle = offset.angle() + wrapFromDistance = offset.length() + wrapEntryLength = 0.0 + else: + wrapFromAngle = fromAngle + wrapFromDistance = radius + wrapEntryLength = global_position.distance_to(corner + Vector2.from_angle(fromAngle) * radius) wrapToAngle = toAngle - wrapFromDistance = offset.length() wrapToDistance = radius wrapFromRotation = sprite.rotation wrapToRotation = toRotation @@ -233,7 +289,7 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotatio wrapOntoWallNormalX = ontoWallNormalX # Keep the same speed as crawling along a surface. wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius - wrapDuration = maxf((wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05) + wrapDuration = maxf((wrapEntryLength + wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05) wrapProgress = 0.0 velocity = Vector2.ZERO isWrapping = true @@ -241,8 +297,14 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotatio func _update_wrap(delta: float) -> void: wrapProgress = minf(wrapProgress + delta / wrapDuration, 1.0) - # Progress is split between the arc and the straight exit by their lengths. - var travelled := wrapProgress * (wrapArcLength + wrapExitOffset.length()) + # Progress is split between the straight entry, the arc and the straight + # exit by their lengths. + var travelled := wrapProgress * (wrapEntryLength + wrapArcLength + wrapExitOffset.length()) + var arcStart := wrapCorner + Vector2.from_angle(wrapFromAngle) * wrapFromDistance + if travelled < wrapEntryLength: + global_position = wrapEntryStart.lerp(arcStart, travelled / wrapEntryLength) + return + travelled -= wrapEntryLength 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() @@ -252,12 +314,15 @@ func _update_wrap(delta: float) -> void: 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. + # Close the small gap left by WRAP_MARGIN so we're touching the new surface. + # Done with move_and_slide so is_on_floor()/is_on_wall() are up to date + # instead of still describing where we were before the wrap. if wrapOntoWallNormalX != 0.0: - move_and_collide(Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0)) + velocity = Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0) / delta else: - move_and_collide(Vector2(0, WRAP_MARGIN + 1)) + velocity = Vector2(0, WRAP_MARGIN + 1) / delta + move_and_slide() + velocity = Vector2.ZERO isWrapping = false isClimbingLip = false diff --git a/Characters/player.tscn b/Characters/player.tscn index 98b7034..124552c 100755 --- a/Characters/player.tscn +++ b/Characters/player.tscn @@ -14,3 +14,11 @@ offset = Vector2(0, -6) [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123] shape = SubResource("CircleShape2D_eovys") + +[node name="LeftEdgeSensor" type="RayCast2D" parent="."] +position = Vector2(-24, 0) +target_position = Vector2(0, 14) + +[node name="RightEdgeSensor" type="RayCast2D" parent="."] +position = Vector2(24, 0) +target_position = Vector2(0, 14) -- 2.45.2 From 01eb5adbd169f64ef4b46b631744b5509cad3f37 Mon Sep 17 00:00:00 2001 From: SnekInAScarf Date: Sat, 31 Oct 2026 13:11:14 -0700 Subject: [PATCH 2/2] Rotate edge sensors with the surface so edges work on walls and ceilings The edge sensors now live under an EdgeSensors pivot that's rotated to point into whatever surface the slime is on. Pressing into the surface (down on floors, up on ceilings, towards the wall on walls) near an edge slides over it onto the side of the platform. The other axis picks the side; otherwise facing decides (last direction moved along a wall). - Ceiling edge -> wall above, wall top -> floor, wall bottom -> ceiling underneath, floor edge -> wall below, all through one code path. - Skip sensors that start inside something (inside corners). - _begin_wrap takes the normal of the surface to end up on and works out the sprite rotation from it; wraps can now end on a ceiling. Co-Authored-By: Claude Opus 5.5 --- Characters/player.gd | 120 ++++++++++++++++++++++++++--------------- Characters/player.tscn | 6 ++- 2 files changed, 82 insertions(+), 44 deletions(-) diff --git a/Characters/player.gd b/Characters/player.gd index 5c15d25..687f38b 100755 --- a/Characters/player.gd +++ b/Characters/player.gd @@ -12,8 +12,9 @@ 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 -@onready var leftEdgeSensor: RayCast2D = $LeftEdgeSensor -@onready var rightEdgeSensor: RayCast2D = $RightEdgeSensor +@onready var edgeSensors: Node2D = $EdgeSensors +@onready var leftEdgeSensor: RayCast2D = $EdgeSensors/LeftEdgeSensor +@onready var rightEdgeSensor: RayCast2D = $EdgeSensors/RightEdgeSensor # How long to press opposite direction before unsticking from a wall/ceiling const MAX_SECS_TO_UNSTICK = 0.5 @@ -48,15 +49,17 @@ 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 wrapOntoNormal := Vector2.UP # Normal of the surface we end up on. var timeSinceUpPressed := 1.0 var wrapEntryStart := Vector2.ZERO # Where a straight move before the arc starts from. var wrapEntryLength := 0.0 -# Floor edges: the edge sensors point down into the floor a little way to each -# side. If one stops touching floor, there's an edge within edgeSensorRange on -# that side and pressing down will slide over it onto the wall below. +# Surface edges: the edge sensors are rotated to point into whatever surface +# we're on, a little way to each side of us. If one stops touching the surface, +# there's an edge within edgeSensorRange on that side, and pressing into the +# surface will slide over it onto the side of the platform. @export var edgeSensorRange := 24.0 +var wallFacingY := -1.0 # Which way we last moved along a wall (-1 is up). func _ready() -> void: var sensorDepth := _get_radius() + 4 @@ -96,8 +99,10 @@ func _physics_process(delta: float) -> void: if isClimbingLip and wantsToClimb and _start_lip_wrap(): return - # On a floor near its edge and pressing down: slide over onto the wall below. - if is_on_floor() and verticalInput < 0 and _start_floor_edge_wrap(horizontalInput): + # Near the edge of a surface and pressing into it: slide over onto the + # side of the platform. + if isStickingToWall and verticalInput: wallFacingY = -signf(verticalInput) + if _start_edge_wrap(horizontalInput, verticalInput): return # Quick flip sprite thing. @@ -196,6 +201,13 @@ 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_point_free(point: Vector2) -> bool: + var query := PhysicsPointQueryParameters2D.new() + query.position = point + query.collision_mask = collision_mask + query.exclude = [get_rid()] + return get_world_2d().direct_space_state.intersect_point(query, 1).is_empty() + func _is_spot_free(spot: Vector2) -> bool: var query := PhysicsShapeQueryParameters2D.new() query.shape = collisionShape.shape @@ -218,7 +230,7 @@ func _start_lip_wrap() -> bool: if not faceHit or faceHit.normal.x * dir < 0.5: return false var corner := Vector2(faceHit.position.x, ceilingY) # 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) + return _begin_wrap(corner, Vector2(dir, 0).angle(), Vector2(0, -radius), Vector2(dir, 0)) # Just climbed past the top of a wall: find the corner and swing up on top. func _start_wall_top_wrap() -> bool: @@ -234,40 +246,67 @@ func _start_wall_top_wrap() -> bool: if not faceHit or faceHit.normal.x * n < 0.5: return false var corner := Vector2(faceHit.position.x, topY) # 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) + return _begin_wrap(corner, -PI/2, Vector2(-n * radius, 0), Vector2.UP) -# On a floor and pressing down: if there's an edge in range, slide to it and -# over onto the wall below. Holding a direction picks that side, otherwise we -# go the way we're facing, or the other way if only that side has an edge. -func _start_floor_edge_wrap(horizontalInput: float) -> bool: - if horizontalInput: - return _try_floor_edge_wrap(signf(horizontalInput)) - var facing := -1.0 if sprite.flip_h else 1.0 - return _try_floor_edge_wrap(facing) or _try_floor_edge_wrap(-facing) +# Normal of the surface we're stuck to, or zero if we're in the air. +func _get_surface_normal() -> Vector2: + if is_on_floor(): return Vector2.UP + if isStickingToCeiling: return Vector2.DOWN + if isStickingToWall: return Vector2(wallNormalX, 0) + return Vector2.ZERO -func _try_floor_edge_wrap(dir: float) -> bool: - var sensor := rightEdgeSensor if dir > 0 else leftEdgeSensor +# Which way the slime is facing, in world space. +func _get_facing() -> Vector2: + if is_on_floor(): return Vector2(-1 if sprite.flip_h else 1, 0) + if isStickingToCeiling: return Vector2(1 if sprite.flip_h else -1, 0) + return Vector2(0, wallFacingY) + +# Pressing into the surface we're on: if there's an edge in range, slide to it +# and over onto the side of the platform. Holding a direction along the surface +# picks that side, otherwise we go the way we're facing, or the other way if +# only that side has an edge. +func _start_edge_wrap(horizontalInput: float, verticalInput: float) -> bool: + var normal := _get_surface_normal() + if normal == Vector2.ZERO: return false + var input := Vector2(horizontalInput, -verticalInput) + if input.dot(-normal) < 0.5: return false + # Point the sensors into the surface. Their local +x runs along it. + edgeSensors.rotation = normal.angle() + PI/2 + var tangent := Vector2.from_angle(edgeSensors.rotation) + var along := input.dot(tangent) + if absf(along) >= 0.5: + return _try_edge_wrap(normal, tangent, signf(along)) + var facing := 1.0 if _get_facing().dot(tangent) >= 0 else -1.0 + return _try_edge_wrap(normal, tangent, facing) or _try_edge_wrap(normal, tangent, -facing) + +func _try_edge_wrap(normal: Vector2, tangent: Vector2, side: float) -> bool: + var sensor := rightEdgeSensor if side > 0 else leftEdgeSensor sensor.force_raycast_update() if sensor.is_colliding(): return false + # A sensor starting inside something is an inside corner, not an edge. + if not _is_point_free(sensor.global_position): return false + var dir := tangent * side var radius := _get_radius() var pos := global_position - # Top of the floor we're on, checked from the side away from the edge. - var floorHit := _cast_ray(pos - Vector2(dir * radius * 0.5, 0), pos + Vector2(-dir * radius * 0.5, radius * 2)) - if not floorHit or floorHit.normal.y > -0.5: return false - var floorY: float = floorHit.position.y - # Side of the platform, just below its top, looking back from past the sensor. - var faceHit := _cast_ray(Vector2(pos.x + dir * (edgeSensorRange + 2), floorY + 2), Vector2(pos.x - dir * radius, floorY + 2)) - if not faceHit or faceHit.normal.x * dir < 0.5: return false - var corner := Vector2(faceHit.position.x, floorY) - # Slide to just above the corner, swing down beside the wall, then carry on - # down it so our side ends up flat against it. - return _begin_wrap(corner, Vector2(dir, 0).angle(), Vector2(0, radius), PI/2 * dir, dir, -PI/2) + # The surface we're on, checked from the side away from the edge. + var surfaceFrom := pos - dir * radius * 0.5 + var surfaceHit := _cast_ray(surfaceFrom, surfaceFrom - normal * radius * 2) + if not surfaceHit or surfaceHit.normal.dot(normal) < 0.5: return false + # Side of the platform, just inside its surface, looking back from past the sensor. + var line: Vector2 = surfaceHit.position - normal * 2 + dir * (pos - surfaceHit.position).dot(dir) + var faceHit := _cast_ray(line + dir * (edgeSensorRange + 2), line - dir * radius) + if not faceHit or faceHit.normal.dot(dir) < 0.5: return false + var corner: Vector2 = faceHit.position + normal * 2 + # Slide to just past the corner, swing around it, then carry on along the + # side of the platform so we end up flat against it. + return _begin_wrap(corner, dir.angle(), -normal * radius, dir, normal.angle()) -# Starts swinging around corner to toAngle from it, then moving by exitOffset. +# Starts swinging around corner to toAngle from it, then moving by exitOffset, +# ending up stuck to the surface facing ontoNormal. # If fromAngle is given, first moves straight to that point on the arc, # otherwise the arc starts from where we are now. # Returns false if there's no room to end up there. -func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotation: float, ontoWallNormalX: float, fromAngle := NAN) -> bool: +func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, ontoNormal: Vector2, fromAngle := NAN) -> bool: var radius := _get_radius() + WRAP_MARGIN if not _is_spot_free(corner + Vector2.from_angle(toAngle) * radius + exitOffset): return false var offset := global_position - corner @@ -284,9 +323,9 @@ func _begin_wrap(corner: Vector2, toAngle: float, exitOffset: Vector2, toRotatio wrapToAngle = toAngle wrapToDistance = radius wrapFromRotation = sprite.rotation - wrapToRotation = toRotation + wrapToRotation = ontoNormal.angle() + PI/2 wrapExitOffset = exitOffset - wrapOntoWallNormalX = ontoWallNormalX + wrapOntoNormal = ontoNormal # Keep the same speed as crawling along a surface. wrapArcLength = absf(angle_difference(wrapFromAngle, wrapToAngle)) * radius wrapDuration = maxf((wrapEntryLength + wrapArcLength + exitOffset.length()) / GROUND_SPEED, 0.05) @@ -317,17 +356,14 @@ func _update_wrap(delta: float) -> void: # Close the small gap left by WRAP_MARGIN so we're touching the new surface. # Done with move_and_slide so is_on_floor()/is_on_wall() are up to date # instead of still describing where we were before the wrap. - if wrapOntoWallNormalX != 0.0: - velocity = Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0) / delta - else: - velocity = Vector2(0, WRAP_MARGIN + 1) / delta + velocity = -wrapOntoNormal * (WRAP_MARGIN + 1) / delta move_and_slide() velocity = Vector2.ZERO isWrapping = false isClimbingLip = false - isStickingToCeiling = false - isStickingToWall = wrapOntoWallNormalX != 0.0 + isStickingToCeiling = wrapOntoNormal.y > 0.5 + isStickingToWall = absf(wrapOntoNormal.x) > 0.5 if isStickingToWall: - wallNormalX = wrapOntoWallNormalX + wallNormalX = wrapOntoNormal.x ignoreWallUnstickUntilRelease = true diff --git a/Characters/player.tscn b/Characters/player.tscn index 124552c..7796463 100755 --- a/Characters/player.tscn +++ b/Characters/player.tscn @@ -15,10 +15,12 @@ offset = Vector2(0, -6) [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123] shape = SubResource("CircleShape2D_eovys") -[node name="LeftEdgeSensor" type="RayCast2D" parent="."] +[node name="EdgeSensors" type="Node2D" parent="."] + +[node name="LeftEdgeSensor" type="RayCast2D" parent="EdgeSensors"] position = Vector2(-24, 0) target_position = Vector2(0, 14) -[node name="RightEdgeSensor" type="RayCast2D" parent="."] +[node name="RightEdgeSensor" type="RayCast2D" parent="EdgeSensors"] position = Vector2(24, 0) target_position = Vector2(0, 14) -- 2.45.2