Ledge Detection

Attention Topic was automatically imported from the old Question2Answer platform.
Asked By Diet Estus

I am trying to implement 2D ledge grabbing in Godot, but don’t know an elegant way to go about detecting ledges.

When I’ve done it in the past, in Python, my Player had a bounding box with topleft, topright, bottomleft, and bottomright attributes, as did my Walls.

When a scene was loaded, I would iterate through the corners of my walls to check which ones were “grabbable” (the method involved raycasting), and I would add them to a ledge list. If my player’s top corners ever got near these points, I would change my player’s state to grab.

In Godot, my player is a KinematicBody2D, and although I have a bounding box attribute that is a Rect2, Godot’s Rect2 doesn’t have corner attributes. Further, my walls are each a StaticBody2D and also lack these attributes.

So, my question is: What is an elegant way to go about ledge detection in Godot?

The way I detect ledges is by checking two points using interact_point().

I then check two points to determine if the player is grabbing a ledge or not. One in the top corner of the direction the player is facing, and another from the middle to the wall.

If the top corner is not colliding with the tilemap and the middle point is then I consider it a ledge. If both points are touching the tilemap, then it is not a ledge, but a wall.

madcapacity | 2018-03-23 02:13

Reply From: kidscancode

A Rect2’s corners are easily found with position and size:

topleft = position
topright = position + Vector2(size.x, 0)
bottomright = position + size
bottomleft = position + Vector2(0, size.y)

Also, you mentioned that you used raycasting in your other solution. In Godot you can either use a RayCast2D node or directly cast rays by accessing the physics space (see Ray-casting — Godot Engine (latest) documentation in English). Your algorithm should be replicable using those techniques.

Thanks for the answer. By the way, I love your tutorials!

Diet Estus | 2018-03-14 15:45