#How do I get closest point of a CollsionShape2D?

19 messages · Page 1 of 1 (latest)

icy schooner
#

If not that how do I get a list of the points that makes up the CollsionShape2D object?

The image example: Red box is the corner I want to get, yellow is the ray I sent which does not hit the corner but close to it.

limber ridge
#

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.

limber ridge
#

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.

limber ridge
#

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```

icy schooner
#

also thank u for the help 💙

icy schooner
#

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

limber ridge
#

Your solution looks better than anything I would have come up with. On to someone more experienced than me. 🙂

icy schooner
#

:c

icy schooner
#

I kinda figured out a possible way to solve this

#

by basically simulating more samples

#

I will get closer to the result I want

icy schooner