In grappling with the threat of doomsday rocks from outer space, Hollywood has always been far ahead of the federal government, cranking out thrillers full of swashbuckling heroes, rockets and nuclear blasts that save the planet.

Now Washington is catching up.

On Wednesday, the nation’s agencies that build civilian rockets and nuclear arms sealed an agreement to start working together on planetary defense. The goal is to learn how to better deflect comets and asteroids that might endanger cities and, in the case of very large intruders, the planet as a whole.

“Often, these agencies focus on their own pieces of the puzzle, so anything that brings them together is a good thing,” said Bruce Betts, director of science and technology at the Planetary Society, a nonprofit group that promotes space exploration.

Comets and asteroids are part of the cosmic rubble left over from the birth of the solar system. Comets, made of dirty ice, visit Earth’s neighborhood only when knocked loose from their home orbits beyond Pluto. That makes their movement somewhat unpredictable. Asteroids, made of rock, fly mostly in a belt between Mars and Jupiter. Their orbits can be calculated with great precision if astronomers can spot the dim objects.