For a creature that spends all of its time scouring for poop to roll into a ball for dinner, it's pretty incredible that the dung beetle is also a master of the sky, using a primitive form of celestial navigation to make its way home after a long day of dung collecting.

Scientists have known about the dung beetle's unique navigation technique for years, but couldn't figure out exactly how it was done. Now a new study appears to have cracked the dung beetle code, and it turns out their star-gazing abilities are even more impressive than previously thought, because they're essentially taking a mental snapshot of the night sky and using that to find their way around.

"We are the first to have shown that dung beetles are taking these snapshots," said lead researcher Basil el Jundi from Lund University in Sweden. "We are also the first to show how they store and use the images inside their tiny brains."

In the study, the team placed dung beetles inside a facility where they could control a simulation of the night sky. They found that the beetles typically perform a little dance (yes, a dance) atop their dung balls, and during this dance, they're actually recording a mental snapshot of the current positions of the stars, the Moon, and the Sun.

With the snapshot securely memorised, the beetle then goes about its day.

If it happens to get lost, it pulls up this mental image and navigates back home. So basically the dung beetle can recall its exact position based on the location of the Moon, the Sun and stars, which is pretty incredible when you consider it's just a simple insect that doesn't even know what stars are.

"The snapshot is taken when the beetle is dancing, and the image is stored in the brain," the team explains. "When the beetle then starts to roll its ball of dung, it is able to successfully navigate straight ahead by matching the stored snapshot of the sky with the present environment."

To further test this out, the team used their artificial sky to see how the beetles would change their course based on what they projected on the 'sky'. According to Matt McGrath at BBC News, they found that the beetles could 'see' many things we humans can't, such as the spectral gradient of the sky and polarised light, and they used this to navigate their new paths.

While these results could explain how dung beetles get around, the team also believes that understanding the odd navigation system might help with the development of autonomous cars in the future.

"Based on these results you could create robots or algorithms that could be incorporated into autonomous vehicles that could navigate without cues that humans input into the system," el Jundi told BBC News.

The team's findings were published in Current Biology, and here's that amazing dance you've all been waiting for: