I have a rotated BoxShape and given a point outside the shape, I would like to find the closest point that lies on the shape towards that point.

I have developed a method that can do this for non rotated boxes but I am struggling with the math required to do this for a rotated box.

```
public static Vector3 GetClosestPoint(this BoxShape box, Transform boxTransform, Vector3 p)
{
Vector3 worldPoint = Vector3.Zero;
float boxX1 = boxTransform.origin.x - box.Extents.x;
float boxX2 = boxTransform.origin.x + box.Extents.x;
float boxY1 = boxTransform.origin.y - box.Extents.y;
float boxY2 = boxTransform.origin.y + box.Extents.y;
float boxZ1 = boxTransform.origin.z - box.Extents.z;
float boxZ2 = boxTransform.origin.z + box.Extents.z;
worldPoint.x = Mathf.Clamp(p.x, boxX1, boxX2);
worldPoint.y = Mathf.Clamp(p.y, boxY1, boxY2);
worldPoint.z = Mathf.Clamp(p.z, boxZ1, boxZ2);
return worldPoint;
}
```

Solutions I found online say to make the box axis-aligned first but I am unsure how to go about doing that in Godot.

Here are some stack exchange links discussing this:

- https://math.stackexchange.com/questions/3517305/finding-the-nearest-least-distance-point-on-a-cube-from-a-point-lying-outside

- https://math.stackexchange.com/questions/2296533/shortest-distance-between-rectangular-cuboid-and-point-in-3d-space

I am using C# but solutions in GDScript are welcome as I can easily translate between the two.