That is exactly why the brain paralyses the body during REM sleep, to stop the animal from hurting itself during sleep. The dog is paralysed to start with and its paws are moving. But for some reason the brain releases the paralysis and the dog's legs start moving. Then the dog wakes up startled and runs into the wall.This thread reminded me of this youtube video