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.