We are here by the skin of our teeth. Evolution could well have turned out differently, and the fact that it did not may well be down to freak, chance events. Life on Earth has faced a string of accidents, weird situations and outright catastrophes, from sudden ice ages to collisions with asteroids – and it is how life responded to these contingencies that ultimately led to us.

If that is so, we can only understand the story of life by taking the broadest possible view. Organisms are shaped by their environments, and those environments are shaped in turn by huge geological forces like volcanoes and ice sheets, and by the shifting climate.

But we should cast the net even wider. What if these great forces were influenced by even greater forces from the wider Universe? Might cosmic events in our Solar System and even our galaxy have also played roles? Do we literally have to thank our stars that we are here?

The most well-known example of an evolutionary shift caused by astronomical events is the hypothesis that the dinosaurs were driven to extinction by a gigantic meteorite impact 66 million years ago. This was proposed in 1980 by physicist Luis Alvarez, his geologist son Walter, and their coworkers.

The researchers discovered that sedimentary rocks, laid down all over the world at the time of the extinction, contain large amounts of a rare element called iridium. The team suggested that the iridium might have come from the dusty debris of a meteorite that smashed into the Earth. Iridium is more abundant in asteroids, the most likely source of such a meteorite, than it is on Earth.

Such a dramatic event would leave some mark on evolutionary history

Quite how such an impact might have killed off the dinosaurs remains a matter of debate, but there are many possibilities.

The energy released could have triggered global wildfires. The researchers estimated that, to deliver the required amount of iridium, the meteorite would have to be about 6 miles (10km) across. The impact of such a monster would have released millions of times more energy than a hydrogen bomb. What's more, the dust and debris thrown up into the air by the blast could have blocked sunlight and sent temperatures plunging for several years after.

In 1991 the impact hypothesis got a boost when scientists found an impact crater more than 100 miles wide at Chicxulub on Mexico's Yucatan peninsula. Its geological age coincided precisely with the extinction.

How much the impact drove the dinosaurs' demise is not clear; there is evidence that they were already on the wane. Still, there is good reason to expect that such a dramatic event would leave some mark on evolutionary history. The discovery helped to prompt concerns about potentially devastating meteorite impacts today.

Besides, a meteorite impact is not the only explanation for the extinctions 66 million years ago.

Tokuhiro Nimura is a researcher at the Japan Spaceguard Association, which was formed to monitor near-Earth objects that might strike the planet. In March 2016, Nimura and his coworkers suggested that the extinctions, global cooling and iridium layer might have been caused by the Solar System passing through a molecular cloud: one of the great clouds of gas and dust in space from which stars form. As dust accumulated in the atmosphere, it would have formed a haze that reflected sunlight and cooled the planet.

The event 66 million years ago is just one of several known "mass extinctions"

The basic idea goes back to a 1975 suggestion by British astronomer William McCrea. He thought that if Earth passed through an interstellar "dust lane" it could cause an ice age. At the time, astronomers Mitchell Begelman and Martin Rees pointed out that such dust might instead affect the way particles streaming from the Sun impinge on the Earth’s atmosphere and expose the planet to high doses of radiation, causing extinctions as well as climate changes.

Nimura has now resurrected McCrea's idea, arguing that the Chicxulub impact does not seem to have been catastrophic enough to account for the extinctions at the end of the Cretaceous.

However, for now this is mostly speculation. "The idea strikes me as highly interesting and plausible, but as yet undeveloped and without clear supporting evidence," says astronomer Martin Beech of Campion College at the University of Regina in Saskatchewan, Canada.

The event 66 million years ago is just one of several known "mass extinctions", in which many species worldwide seem to have died out suddenly.

At least some mass extinctions are caused by influences outside the living world

The biggest was at the end of the Permian period 252 million years ago, when no less than 96% of all life on Earth seems to have died out. All life today is descended from the surviving 4%, so it is easy to see that evolutionary history would have been very different if this extinction had not happened. When species die off, those that survive get opportunities to expand and diversify that they would not otherwise have had.

Palaeontologists have long debated what causes these mass extinctions.

It is possible that, just like smaller-scale population crashes, they could be an inherent part of the way ecosystems work. Because all life is interdependent, a small shift in one population might occasionally create a domino effect that sends shockwaves through the entire system.

But it is more likely that at least some mass extinctions are caused by influences outside the living world.

One such mass extinction occurred at the end of the Triassic period. About half of all species on Earth disappeared. The event might have been triggered by increases in volcanic activity, perhaps producing changes in climate, but it might also have been caused by a meteorite impact.

Mass extinctions have happened at regular intervals, about 26 million years apart

Such catastrophic collisions might not be the result of sheer chance, of stray asteroids or comets blundering into Earth. Instead, cosmic circumstances could systematically cause such objects to come close to our world.

The most well-known of these ideas is that our Sun has a dim companion star, which is so far away that it has never been observed directly. This star, dubbed "Nemesis" or the "Death Star", might periodically pull lumps of icy rock from the fringes of the Solar System and send them careening through our neighbourhood.

This idea was proposed in 1984 by two teams of astronomers: Daniel Whitmire and Albert Jackson, and Marc Davis, Richard Muller and Piet Hut. They were all prompted by the apparent discovery earlier that year that mass extinctions have happened at regular intervals, about 26 million years apart, over the past 500 million years.

The idea is that the gravitational pull of Nemesis, circling the Sun in an orbit about 1.5 light years away, would disturb the Oort cloud: a host of icy objects that lie beyond the orbit of Pluto at distances of about 0.8 to 3 light years, and which are only loosely bound by the Sun's gravity. The Oort cloud is the source of "long-period" comets, which return to the inner Solar System every few hundred years or more.

It is completely possibly that we have failed to spot another planet in the Solar System

Nemesis would be a tiny star: perhaps a red dwarf, or even a brown dwarf not much bigger than a giant planet like Jupiter. So it is not altogether surprising that it has never been spotted. It would be hard to see at such a distance, even with the most powerful telescopes.

But that is not the only problem with the Nemesis theory.

In a study published in 2010, astrophysicist Adrian Melott of the University of Kansas and palaeontologist Richard Bambach of the Smithsonian Institution in Washington DC re-examined the fossil record using the latest data. They confirmed that mass extinctions recur every 27 million years. However, they say this pattern is actually too regular to fit with the Nemesis idea. Such a distant dwarf star would inevitably be disturbed by other nearby stars, producing a more irregular influx of comets.

Instead, maybe the waves of mass extinction are caused, not by a companion star, but by another planet.

In 1985, Whitmire and his colleague John Matese suggested that there might be a relatively small rocky planet, around five times the mass of the Earth, orbiting in the Solar System far beyond Neptune. This planet might pull comets, not from the Oort cloud, but from the much nearer Kuiper belt. This is another disk of icy rocks on the edge of the solar system, of which Pluto and its moon Charon are now recognized as members. Whitmire and Matese called their hypothetical object "Planet X".

It is completely possibly that we have failed to spot another planet in the Solar System, even one bigger than the Earth. Before the New Horizons spacecraft reached Pluto and Charon in 2015, the best view we had of those objects was a vague blur, and we are only now starting to be able to make out some of the other large bodies in the Kuiper belt. If Planet X is dark and unreflective, it could well have eluded all our astronomical surveys.

The diversity of marine species goes up and down every 62 million years

What's more, in January 2016 astronomers proposed that there could be a ninth planet in the Solar System; orbiting beyond Neptune, with a mass about 10 times that of Earth. The suggestion was prompted by observations of visible Kuiper-belt objects, which seemed to be disturbed by an unseen influence.

If this planet exists, it probably would not do what Planet X is said to do. But the story illustrates that we do not know what is out there.

Whitmire, who is now at the University of Arkansas, has taken the Planet X hypothesis even further. In a 2015 study he showed that the idea fits with the regular 27-million-year extinction periodicity seen by Melott and Bambach. What's more, Whitmire says a second such object – Planet Y? – could explain yet another oscillation in the fossil record.

This pattern was reported in 2005 by Richard Muller and Robert Rohde. They found that the diversity of marine species goes up and down every 62 million years: a change that could be caused by changes either in extinction rates or the rate at which new species form.

Waves of comet impacts caused by "hidden" planets are a plausible explanation for these patterns, says Melott. But he says the patterns could also be caused by other, more distant cosmic events.

In 2007, Melott and his colleague Mikhail Medvedev argued that the 62-million-year pulse might be caused by a regular feature in the Solar System's journey through our Milky Way galaxy.

Cosmic-ray collisions could change the chemistry of the atmosphere

Our galaxy is shaped a bit like a plate. As it rotates, the Sun rises and falls in the galactic plane, rather like a merry-go-round horse. These shifts in position could change the amount of cosmic rays that stream through the Solar System and hit Earth.

Cosmic rays are high-energy subatomic particles, such as protons and electrons, shooting through space. They are thought to be produced in high-energy astronomical processes. Some seem to originate in supernovae: stars that explode when their fuel is exhausted. Others may come from black holes at the centres of other galaxies.

There are various ways that they could affect Earth's environment and thereby influence evolution.

Cosmic rays could themselves be harmful. When they collide with molecules in the air, they produce showers of particles that could induce mutations in DNA. That is typically bad for life. However, a low level of mutation could actually boost the variety upon which natural selection operates, making life more diverse

Cosmic-ray collisions could also change the chemistry of the atmosphere. They might produce electrically-charged particles that affect cloud formation and thus climate, or they could destroy the ozone layer that protects the Earth from the Sun's harmful ultraviolet rays.

Deadly rays from space do seem to be a good candidate cause for some evolutionary shifts

Because many cosmic rays are thought to be created in supernovae within our galaxy, the Solar System's bobbing up and down in the galaxy could alter the cosmic-ray flux, with knock-on effects for Earth's life.

However, it is strange that these effects only show up in marine fossils. If anything, you might expect sea-dwelling organisms to be better protected from hazardous particle showers than land-based ones.

Even Melott now thinks that this idea cannot account for the 62-million-year cycle in the fossil record after all. In 2011 he suggested that it might instead be an innate, geological "pulse of the Earth", perhaps related to changes in tectonic activity.

There is a similar pattern of changes in the makeup of marine sedimentary rocks, Melott and his coworkers say. This is what would be expected from changes in the rates of mountain-building and erosion, caused by shifts in the movements of tectonic plates.

All the same, deadly rays from space do seem to be a good candidate cause for some evolutionary shifts seen in the fossil record.

We are constantly exposed to low levels of cosmic rays. But a single supernova could unleash a lethal blast of such particles if it happened near enough to our Solar System.

A supernova would need to be within about 30 light years for it to have devastating consequences on Earth

Stars explode as supernovae all the time; when they do, they can temporarily outshine their entire host galaxies. Many are seen every year in other galaxies, but the most recent one known to have happened in our own galaxy became visible about 140 years ago. Another in our galaxy that appeared in 1572 was so bright, it was visible to the naked eye and was seen by the astronomer Tycho Brahe.

"Tycho's supernova" was safely distant: 7,500 light years away. If such an explosion happened much closer to us, we would be in serious trouble. The Earth would be raked not just with high-energy particles but with X-rays and gamma-rays, which could be fatal.

Has that ever happened?

It is estimated that a supernova would need to be within about 30 light years for it to have devastating consequences on Earth. There are not many stars that close.

However, in a 2002 study astronomers estimated that there may have been as many as 20 supernovae within about 420 light years of Earth over the past 11 million years, just from one group of stars, some of which are as close as 130 light years. Such events might well leave imprints in the fossil record.

The rays could create an indirect hazard by damaging the ozone layer

They certainly seem to have left traces in sedimentary rocks. Supernovae scatter the outer layers of the exploding star into space, including some atoms that are rare on Earth.

One of these tell-tale products of a supernova is a kind of iron called iron-60, which is not naturally formed on Earth. In 1999, physicists found high levels of iron-60 in geological structures from the deep ocean called ferromanganese crusts formed over the past 5 million years or so. Iron-60 has also been found in lunar "soil", and it seems to have come from two supernovae about 320 light years away, one about seven and the other two million years ago.

The latter explosions seems to have left traces in the fossil record.

In a study published in August 2016, astrophysicist Shawn Bishop of the Technical University of Munich and his colleagues reported finding iron-60 in fossil iron oxide crystals. The crystals were originally made by bacteria that use the magnetic oxide to align themselves with Earth's magnetic field. The iron-60 began to appear in such fossils in marine sediments that formed 2.6-2.8 million years ago.

Life could have been disturbed by these supernovae.

X-rays and gamma rays coming from so distant a source are not a direct problem in themselves. "They don't penetrate our atmosphere, and so can't directly cause sterilisation or mass extinctions," says Bishop.

But he says the rays could create an indirect hazard by damaging the ozone layer. "With a reduced ozone layer, as we know from the days of the Antarctic ozone layer hole, ultraviolet light from the Sun will penetrate to the Earth's surface and can then become a problem for organisms."

This will be extremely difficult to prove after millions of years

According to calculations by astronomer Narciso Benítez and his colleagues, supernovae at these distances could potentially deplete atmospheric ozone.

What's more, in a study published in July 2016 Melott and his colleagues estimated that cosmic rays from the supernovae could have boosted the numbers of high-energy neutrons and muons reaching ground level, tripling the overall radiation dose to ground-dwelling organisms. That could have induced cancer-causing mutations, as well as triggering changes in climate, the researchers say.

There does seem to have been a small mass extinction about 2.6 million years ago, at the boundary of the Pliocene and Pleistocene eras. But we cannot say for sure if a supernova played a role.

Indeed, there is no direct evidence of supernovae ever having a causal impact on the evolutionary history of life, says Bishop. "This will be extremely difficult to prove after millions of years." For example, there is no way to collect and examine fossil DNA for mutations after such long periods of time, let alone to make a comparison before and after the event.

However, there is another kind of cosmic outburst that is even more powerful.

The heavens are occasionally riven by blasts called gamma-ray bursts: extremely intense explosions that release gamma rays, lasting between a fraction of a second and a few hours. Gamma-ray bursts are among the most energetic events known in the Universe. They may be produced when particularly massive stars explode.

Earth has been around long enough to have been struck many times over

Fortunately, gamma-ray bursts have so far been seen only in very distant galaxies. But if one went off nearby, a supernova would be like a firecracker in comparison. Worse, "we probably couldn't see one coming, at least sooner than a few hours before," says Melott.

Fortunately, Melott says they are only likely to happen close enough to matter, meaning within 10,000 light years or so, about every 170 million years.

That is rare, but Earth has been around long enough to have been struck many times over. Indeed, in 2004 Melott suggested that a mass extinction around the end of the Ordovician period, 440 million years ago, might have been related to a gamma-ray burst.

The idea is that, again, X-rays and gamma rays from this event could have severely damaged the ozone layer, as well as triggering global cooling by inducing the formation of thick, smoggy nitrogen oxides in the atmosphere.

Melott argues that the pattern of extinctions in the late Ordovician fits this picture. For example, shallow-water marine organisms, which would have been more exposed to ultraviolet radiation than deep-water ones, seem to have been hit harder. Also the climate became markedly cooler.

They might head off the explosion by making the star lose mass

Could this happen again? Earth has about two billion years of life left to it, after which the Sun will expand and render it uninhabitable. In a 2011 analysis, Beech estimated that in that time there are likely to be about 20 supernovae and one gamma-ray burst near enough to do harm. Those are hardly alarming numbers.

Besides, Melott says we should be able to see nearby supernovae well in advance, since we can measure the ages of nearby stars. The nearest one that might detonate soon – meaning any time within the next few million years – is Betelgeuse in the constellation of Orion. It is too far away to do any damage.

Beech says it might even be possible to engineer stars to avoid a catastrophic supernova. "If a civilisation knew that a supernova was going to occur in their neighbourhood, then one survival option would be to try some super-astroengineering project," he says.

For example, they might head off the explosion by making the star lose mass or by mixing in some material that could delay its collapse. "How such engineering might be physically performed I don't know," says Beech. "But the physics of the situation and what one would need to do to prolong a star's life is reasonably well-understood".

Beech suggests that stars about to turn supernova might be good places to look for advanced aliens. If such a star started behaving oddly, it might be a sign of deliberate tampering.

The cosmic threats to life on Earth can get even more exotic.

In her 2015 book Dark Matter and the Dinosaurs, physicist Lisa Randall of Harvard University suggested that a mysterious cosmic substance called dark matter might have been the dinosaurs' ultimate killer.

The exotic dark matter could form a disk in the galactic plane

Dark matter does not interact with light, so we cannot see it directly. It only affects ordinary matter via gravity: it has mass, so it exerts a gravitational tug like any other matter.

We do not know what dark matter is. No one has ever detected a single particle of it. But most physicists and astronomers are sure it exists. If it were not there, galaxies could not rotate as fast as they do without falling apart.

Dark matter is estimated to outweigh ordinary matter in the Universe by about 5 to 1. It is thought to surround each galaxy in a roughly spherical halo.

Randall has suggested that some dark matter differs from the rest.

This "exotic dark matter" might feel another force as well as gravity, akin to the electromagnetic force that makes ordinary matter interact with light. The exotic dark matter could then form a disk in the galactic plane, and the passage of the Solar System through the disk could have perturbed the path of an Oort-cloud comet, causing the giant impact 66 million years ago.

Biologist Michael Rampino of New York University has extended this idea. In a study published in 2015, he proposed that some dark matter particles might be captured and destroyed in Earth's core. This could release enough energy to boost volcanic activity, creating the "pulse of the Earth" that Melott had previously linked to extinctions.

No planet-sterilising meteorite impact is anticipated in the foreseeable future

Well, maybe. But some scientists feel that the idea is way too speculative, and would not have got much attention if it had not been put forward by someone as famous as Randall, who achieved something close to scientific superstardom for her work on cosmology.

"You have to invent new physics in order to get the mechanism to work," says Melott.

"The argument seems rather contrived to me," agrees Beech.

But he adds that, while it is far from clear that our galaxy really has a disk of dark matter, "we know so little about the distribution and make-up of dark matter within the galactic disk and halo that the premise is certainly viable within our present uncertainty."

At this point that might be a rather too-familiar refrain: "interesting idea but it's all speculation". Should we believe any of these notions?

All the individual stories we have discussed are unproven, and many are speculative. But take a step back and there seems no doubt that, one way or another, life on Earth is connected to – and contingent on – cosmic forces. The difficulty is figuring out which cosmic phenomena played a role in a given incident.

These influences play out over such vast timescales that we need not fear any impending existential threat to the biosphere. No planet-sterilising meteorite impact is anticipated in the foreseeable future, though of course it is wise to keep looking.

However, that is not to say that human civilisation is completely safe from space threats.

Melott says our biggest concern should be solar flares: abrupt outbursts from the Sun that shower the planet with high-energy particles and radiation. The electromagnetic pulse they produce could cripple telecommunications.

Astronomical events might for once prove a boon to life on Earth rather than a burden

One such event in 1859 played havoc with the early telegraph network, giving some operators shocks and causing sparks and fires. Today, with our far greater dependence on telecommunications networks, the consequences could be devastating. We narrowly escaped this fate in 2012, when a solar superstorm narrowly missed us – but there was a big one in 1989 that disrupted the Canadian power grid.

If an event like this were indeed to bring civilisation to its knees, it might leave an imprint in the evolutionary record – because, ironically, it could actually stop the latest mass extinction, which is happening right now and is entirely of our own doing.

In that case, astronomical events might for once prove a boon to life on Earth rather than a burden. They would have put us firmly in our place and shown just how puny we are in the face of the cosmos.

Join over six million BBC Earth fans by liking us on Facebook, or follow us on Twitter and Instagram.

If you liked this story, sign up for the weekly bbc.com features newsletter called "If You Only Read 6 Things This Week". A handpicked selection of stories from BBC Future, Earth, Culture, Capital, Travel and Autos, delivered to your inbox every Friday.