One hypothesis for the sudden contraction of muscles is that your brain is losing feeling of the bed supporting your body when going to sleep: https://en.wikipedia.org/wiki/Hypnic_jerk
When this happens to me, it always feels like I'm about to fall forward. The best I can describe it is as the feeling that I just took a step forward but instead of my foot finding the ground, the surface I was walking on ended (as if I stepped off a ledge or something).