#How do I get closest point of a CollsionShape2D?
19 messages · Page 1 of 1 (latest)
This sounds a bit tricky, because collision shapes could be rect, circle, polygon.. Can you share more context about what you're trying to achieve? There might be an easier way.
easiest method might be to cast subsequent raycasts, rotated clockwise, until you get one that doesn't collide. Then the previous one is very close to the corner.
This is probably overcomplicated, and current only works with collision polygon2d shapes, but you could do something like this on the raycast.. walk through the points in the collision shape: ```func _process(delta):
if is_colliding():
var collider = get_collider()
var shapes = collider.find_children("", "CollisionPolygon2D")
if shapes.size() > 0:
var collisionShape = shapes[0]
var collisionPolygon = collisionShape.polygon
var closestPoint = get_closest_point(collisionPolygon, collider.to_local(get_collision_point()))
$Sprite2D.global_position = collider.global_position + closestPoint
func get_closest_point(polygon, location):
var closest_dist_sq : float = INF
var closest_point : Vector2
for point in polygon:
var newDistSq = location.distance_squared_to(point)
if newDistSq < closest_dist_sq:
closest_dist_sq = newDistSq
closest_point = point
return closest_point```
Ye I already figured I could do like this for collsionpolygons but I wanna make it work for most if not all collider shapes
I am trying to make my own logic for a rope or similar string like object to be able to wrap around objects
also thank u for the help 💙
Right now the string attach at a point intersecting between origin/last attach point
I have a very odd methods for making it detac, but it's basically when Imove the object opposite the angular direction it arrived it will detach
ehh kinda
I'm very open for other ways to go about it
Your solution looks better than anything I would have come up with. On to someone more experienced than me. 🙂
:c
I kinda figured out a possible way to solve this
by basically simulating more samples
I will get closer to the result I want