Here is something to think about:

Each part of your world has its own XYZ.

You have your worlds XYZ and you have your objects XYZ.

Each object is relative to space in which it is being acted on.

Let’s use your box as an example:

It has six sides, top, bottom, right, left, front, back.

If we rotate the box a full 180 say on the Y axis. Then the Back is rotated to the Front.

Now the worlds XYZ has not changed but the boxes has. It’s positive X is not facing backwards, in respect to the worlds which now has the negitive X axis of the box.

Also if we have a ball inside the box, its X axis will be also rotated also with the box.

In respect to the box you only need to look at the location of the ball in the box. Since both the ball and box current share the same XYZ space.

As respect for gravity, is effect will very based on world location vs. box rotation.

You use this to find force in which to act on the ball

Now well only need to find the angle in which the box is in refernce to the world gravity.

Let’s say at angle 0 the ball is at rest, both the box and world are parallel.

At > 0, the box is tilted to the left and force is starting to move the ball in the +y, and say at < 0 the box is tilted to the other side and ball moves right.

I hope this gives you more of the idea on how to handle gravity.