0 votes

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?

in Engine by (1,557 points)
edited by

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.

1 Answer

+2 votes
Best answer

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 http://docs.godotengine.org/en/latest/tutorials/physics/ray-casting.html). Your algorithm should be replicable using those techniques.

by (20,671 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.