For rolling stock with fragile details, such as brake wheels above the roofline, I use white glue to add a slightly over-size block of soft foam to the car’s box - note how the sides of the box are bulging somewhat…

Once the box lid is put in place, the foam will compress enough to keep the car from moving around inside the box. If necessary, add an elastic band to ensure that the lid stays in place.
For really fragile stuff, like these tank cars from Tangent, I use soft foam, cemented in place, and shaped to accommodate each particular item, placing the car on a sheet of foam material (it’s sometimes used as a packing material, but I have some that’s used as underlay for laminate flooring, which is a little stronger)…

…when the second car is placed in the box, with its wheels against the cardstock divider, and the lid put in place, neither car will move, even if you shake the box…of course, while holding the box’s lid in place.
For passenger cars and locomotives, I get produce boxes, with lift-off lids, from the supermarket…

…then use corrugated cardboard and a hot glue gun to fabricate compartmented trays, which can be stacked in the boxes…

While the box shown above is merely for storage under the layout, I’d add crushed tissue or paper towelling to prevent end-wise movement if I were transporting those cars.
The procedure is similar for locomotives, but