The last gasps of dying stars are some of the most beautiful objects in the galaxy.

They're called planetary nebulae, clouds of stellar material ejected out into space as a red giant star enters the last stage of its life. The dying star shucks off its outer layers, which are illuminated from within by the hot, exposed core.

These clouds are complex, and gorgeous, with mandala-like waves, strange discs, even bilobed jets akin to wings. The stunning complexity and variety of these shapes seems at odds with the uniform shape of their precursor stars.

"The Sun - which will ultimately become a red giant - is as round as a billiard ball, so we wondered: how can such a star produce all these different shapes?" said astronomer Leen Decin of KU Leuven in Belgium.

Now, through a detailed collection of observations and hydrodynamical simulations, scientists have discovered how planetary nebulae might get their shapes: through gravitational interactions with binary star companions, and large planets like Jupiter that survive the violent deaths of their host stars.

Initially, the team wasn't looking at planetary nebulae at all. The focus of their studies was a slightly earlier life stage called the asymptotic giant branch (AGB).

This is when the red giant is in the last stages of evolution before the planetary nebula phase, and powerful winds from the star are blowing out into the space around it, scattering gas and dust.

Red giants are the old age of a particular kind of star, less than about eight times the mass of the Sun. It's how the Sun is going to end its life, puffing up to engulf Mercury, Venus and maybe even Earth, before its core collapses into a tiny white dwarf gleaming brightly with residual heat.

So, how these stars die is very interesting to astronomers. And yet Decin's international team found that a detailed database of observational data on the winds of AGB stars has not been compiled. So they set about creating one.

"The lack of such detailed observational data caused us to initially assume that the stellar winds have an overall spherical geometry, much like the stars they surround," said astronomer Carl Gottlieb of the Harvard-Smithsonian Center for Astrophysics.

"Our new observational data shapes a much different story of individual stars, how they live, and how they die. We now have an unprecedented view of how stars like our Sun will evolve during the last stages of their evolution."

star winds(L. Decin, ESO/ALMA)

Using the Atacama Large Millimeter/submillimeter Array in Chile, the team took observations of a sample of AGB stars. In those data, they noticed a range of structures - including arcs, shells, bipolar structures, clumps, spirals, doughnut shapes, and rotating discs.

Since the radially outflowing winds were smooth, the team quickly ascertained that something in the immediate vicinity of the star could be causing the structures in the material - like a small binary companion or giant planet, too faint to be seen, but whose gravitational tugging could be affecting the material.

Sure enough, when they modelled the effect of a companion on these outflows, the team found that each type of structure they observed could be created by the presence of a secondary object. The mass of that object, its distance from the star, and the eccentricity of its orbit can all play a role in the variety of the structures produced in the stellar wind.

"Just like a spoon that you stir in a cup of coffee with some milk can create a spiral pattern, the companion sucks material towards it as it revolves around the star and shapes the stellar wind," Decin said.

"All of our observations can be explained by the fact that the stars have a companion."

All the shapes bore strong similarities to the complex structures and shapes seen in planetary nebulae, suggesting the structures in the two stages have the same formation mechanism. And there are wide-ranging implications for our understanding of stellar evolution.

"Our findings change a lot," Decin said. "Since the complexity of stellar winds was not accounted for in the past, any previous mass-loss rate estimate of old stars could be wrong by up to a factor of 10."

The discovery also strongly hints at what might happen when the Sun dies. Our Sun, of course, does not have a binary companion (which is also a bit of a mystery in its own right).

But the Solar System does have two planets massive enough to potentially influence its outflows. Those are Jupiter and Saturn, the gas giants, whose mass is already large enough to tug the Sun around in a tiny wobbly circle.

They'll be far beyond the Sun's reach when our star becomes a red giant, and recent discoveries suggest that giant planets can indeed survive their stars' deaths - maybe not for long, but long enough to make some waves (or arcs or shells).

The team's calculations predict that Jupiter, and maybe Saturn, will be able to carve some relatively weak spirals in the Sun's AGB wind.

The team is now conducting further research to find out what else their discovery might change for our understanding of the deaths of stars.

The research has been published in Science.