If you're making a bed, consider that this is a problem people have worked on for a very long time and there is an entire vertical industry on it.
One reputable company has an entire section in their catalog on it:
https://www.leevalley.com/en-us/shop/hardware/bed-hardware