Answer:
Well, if you didn't know the brain stays on thought your sleep. If you've ever opened someones eyelid while there were sleeping there eye was problem moving. Dreams are like a second world, whatever your mind thinks of it happens. Sometimes. For example i had a dream and i new i was in it and of course i thought lets have some boobies. But of course, nothing happened. So the reason you got nightmares is because you were thinking of something recently or something from the past.