I have some drag-and-drop objects I'd like to keep within the confines of an irregular shape. Is there a way to contain it in the shape using clamp? My understanding is that only square or rectangular shapes can be set for that, but please correct me if that's not true. Or are there better methods for achieving this?
What kind of shape did you have in mind?
Squares, circles and convex polygons are simpler.
The logic would be if not inside the shape move the object to the closest point on the edge of the shape.
For polygons the prices needed are:
Finding of a point is inside of a polygon, distance to a point, and distance to a line segment.
For irregular polygons with concave parts I think the above wouldn’t work super well.
For that I think you could consider a line from its position to its new position, and stop and slide on the first edge it intersects.
If you had a particular shape in mind there may be simpler ways to do it.
You could also use raycasting. Something along the lines of:
is not overlapping object
Repeat 359 times
- cast ray at loopindex
on ray intersected
- add X&Y positions and distance to array
- wait 0
- call move function
on move function
- pick lowest distance in array & move to X&Y positions
Develop games in your browser. Powerful, performant & highly capable.
Thanks very much for your help, you two. Being new, it helped me experiment with a few features and techniques I hadn't before, and I've learned a lot.