Image copyright Getty Images Image caption T. rex was one of the largest terrestrial carnivores of all time

A study mapping how dinosaurs spread across the world shows they may have been a victim of their own success.

UK researchers believe they were already in decline before the killer asteroid hit because they had occupied every habitat on Earth.

From their roots in South America, the dinosaurs migrated "in a frenzy of movement to cover the planet".

Hundreds of different dinosaurs appeared, from the ferocious T. rex to the gigantic long-necked Diplodocus.

But by the time the asteroid struck, killing them off, they were starting to decline, as they had ran out of space on Earth.

The theory, outlined in the journal, Nature Ecology and Evolution, reconstructs the paths taken by the dinosaurs as they moved out of South America.

"They burst on to the scene and really quickly moved to all parts of the Earth," said Dr Chris Venditti of the University of Reading, a co-researcher on the study.

The dinosaurs were able to take advantage of a "blank canvas" left by the extinction known as the Great Dying, just before they appeared, he said.

They quickly spread across the devastated planet, taking up every opportunity to expand, with little competition for food, space or resources from other animals.

But towards the end of their reign, their progress slowed, as they became adapted to almost every habitat on Earth. Only avian dinosaurs survived to become the birds we know today.

"They'd filled the Earth, there was nowhere to move to and they were really specialised in their habitat so they couldn't produce new species," said Dr Ciara O'Donovan of the University of Reading. "It would have been the final nail in the coffin for them apart from the birds."

The research is based on a statistical method to work out where every dinosaur and its ancestors existed, in three dimensional space, on the globe.

This gives a more complete picture than studying fossil evidence alone, which is patchy and incomplete.

But not all researchers are convinced by the idea that the dinosaurs were in decline before the asteroid hit Earth.

"Dinosaurs were continually diversifying all through the Cretaceous when the continents had split into smaller units," said Prof David Martill of the University of Portsmouth, who is not connected with the research.

"They were still diversifying at the end of the Cretaceous just before the meteorite impact hit. "

Follow Helen on Twitter.