I don't know anything about physics because I never used it. But, what are all your waits for? The manual as a "common mistakes" section although I don't know how they named it. It might be helpful for you.
There shouldn't be any wait(1); in an event function. I guess those are the reason for the crash.
And, whatfor is the while loop in the main function? That makes no sense.