Compare commits

..

3 commits

Author SHA1 Message Date
f9c20f4de9 Merge pull request 'Edge sensors: slide over the edges of floors, walls and ceilings' (#3) from floor-edge-drop into main 2026-10-31 20:47:28 -05:00
01eb5adbd1 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 <noreply@anthropic.com>
2026-10-31 13:11:14 -07:00
815444f3af 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 <noreply@anthropic.com>
2026-10-31 13:02:05 -07:00
2 changed files with 132 additions and 21 deletions

View file

@ -12,6 +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 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
@ -46,8 +49,24 @@ 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
# 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
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 +99,12 @@ func _physics_process(delta: float) -> void:
if isClimbingLip and wantsToClimb and _start_lip_wrap():
return
# 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.
if horizontalInput:
if isStickingToCeiling: sprite.flip_h = (horizontalInput > 0)
@ -176,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
@ -198,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:
@ -214,26 +246,89 @@ 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)
# Starts swinging around corner to toAngle from it, then moving by exitOffset.
# 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
# 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
# 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,
# 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) -> 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
wrapCorner = corner
wrapEntryStart = global_position
if is_nan(fromAngle):
wrapFromAngle = offset.angle()
wrapToAngle = toAngle
wrapFromDistance = offset.length()
wrapEntryLength = 0.0
else:
wrapFromAngle = fromAngle
wrapFromDistance = radius
wrapEntryLength = global_position.distance_to(corner + Vector2.from_angle(fromAngle) * radius)
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((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 +336,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,17 +353,17 @@ 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.
if wrapOntoWallNormalX != 0.0:
move_and_collide(Vector2(-wrapOntoWallNormalX * (WRAP_MARGIN + 1), 0))
else:
move_and_collide(Vector2(0, WRAP_MARGIN + 1))
# 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.
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

View file

@ -14,3 +14,13 @@ offset = Vector2(0, -6)
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1379572123]
shape = SubResource("CircleShape2D_eovys")
[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="EdgeSensors"]
position = Vector2(24, 0)
target_position = Vector2(0, 14)