Bang or whimper?Ice or fire? Divine plan or cosmic accident? Alien invaders or genetically enhanced apes? The end of the world is painful to contemplate but also hard to resist thinking about, partly because there are so many wild and scary imaginative possibilities.

In “Melancholia,” an excursion from the sad to the sublime by way of the preposterous, the always controversial Danish director Lars von Trier offers his own, highly personal version of apocalypse: a celestial collision rendered in surprisingly lovely digital effects and accompanied by mighty blasts of Wagner. The film takes its title from a rogue planet that appears suddenly in the night sky and seems to be heading straight for Earth.

The word also, not coincidentally, names an emotional disorder described by Freud as “a profoundly painful dejection, cessation of interest in the outside world, loss of the capacity to love, inhibition of all activity, and a lowering of the self-regarding feelings to a degree that finds utterance in self-reproaches and self-revilings, and culminates in a delusional expectation of punishment.”

The expectation of punishment is, of course, one reason people go to a Lars von Trier movie in the first place. Suffering — predominantly, though not exclusively, the suffering of women — is both his favorite subject and his preferred method. He is a crafty sadist, but also, for all his tricks and provocations, a sincere one.

So “Melancholia” is emphatically not what anyone would call a feel-good movie, and yet it nonetheless leaves behind a glow of aesthetic satisfaction. Total obliteration happens on an intimate scale, and the all-encompassing, metaphysical nature of the drama leaves room for gentleness as well as operatic cruelty. The machinery of mass panic and media frenzy that juices up most films on this subject is notably absent. Instead, difficult emotions are registered in close-ups of individual human faces, and a perverse, persuasive idea rises to the surface. The end of the world as we know it might just turn out to be beautiful.

Freud’s diagnosis pretty much captures the mental state of Justine (Kirsten Dunst), a young woman whose history of crippling depression overshadows her lavish wedding party and threatens to blight her chances at future happiness. In the course of a long, hectic night she comes increasingly undone, to the bewilderment of her new husband, Michael (Alexander Skarsgard), and the exasperation of her sister, Claire (Charlotte Gainsbourg). Compared with the humorless, grimly responsible Claire, Justine is impulsive, self-indulgent and charming: the flighty grasshopper to her sister’s responsible, dutiful ant.

The arrival of Melancholia — the planet, that is — reverses the traditional moral of that fable. In the second half of the movie Justine’s fatalism will prove a more viable (or at least a more graceful) response to the prospect of global annihilation than Claire’s anxious practicality. During the wedding, though, the catastrophe, which has been foretold in a gorgeous, dreamlike overture, full of dark clouds and nightmarish images of doom, is not something the guests seem to be aware of. Rather, the imminence of an all-obliterating big bang is a piece of information the audience possesses in advance of the characters on screen, an open secret that makes their earnest, trivial doings all the more dreadful and absurd.

On its own, the spectacle of matrimony provides a rich, inexhaustible vein of comic and melodramatic potential — chance encounters, simmering grudges, sexual intrigue, dysfunctional outbursts — and Mr. von Trier is hardly the first filmmaker to use a wedding as a kind of controlled experiment in human waywardness. Robert Altman, Noah Baumbach and Jonathan Demme might come to mind during the first hour of “Melancholia,” to say nothing of the houses of Windsor and Kardashian.

The setting is a grand estate on the edge of the water, complete with stables, a golf course and manicured expanses of lawn. English is the language, and dollars are the currency, but this is less a specific America (a place Mr. von Trier has never visited and the theoretical location of most of his recent films) than an abstract space of moneyed entitlement. The aggressive opulence of 21st-century capital coexists, somewhat awkwardly, with an older, aristocratic elegance. You might have seen some of the wedding guests last year at Marienbad, while others, more recently, might have sneered at you from their seats in the first-class cabin as you pushed your way back to coach. Claire’s pompous husband, John (Kiefer Sutherland), owns the property, which seems to be both a high-end resort and his own private family retreat.

Unlike other von Trier victim-heroines — including those played by Emily Watson in “Breaking the Waves”; Nicole Kidman in “Dogville”; and Bjork in “Dancer in the Dark” — Justine is not assailed and humiliated by other people. The element of male aggression that was such a powerful force in those films, and an integral aspect of Mr. von Trier’s creative personality, has been neutralized here. The men who hover around the wedding, including the clueless Michael and the officious John, are not menacing, just useless.

Justine’s boss (and Michael’s best man) is an obnoxious advertising executive played by Stellan Skarsgard (Alexander’s father), who gives his prized employee a promotion and a deadline on what is supposed to be the happiest night of her life. Justine’s parents are the pathologically bitter Charlotte Rampling and the pathologically whimsical John Hurt, and the ensemble (also including Udo Kier as the imperious wedding planner, and Brady Corbet as a newly hired colleague of Justine’s) proceeds through the expected rituals. There are loud arguments, awkward toasts, bad sex, confrontations with the help and a few moments of serene and luminous bliss.

All of which, of course, amounts to nothing, since everything and everyone will be ashes soon enough. That is Justine’s state of mind, and Ms. Dunst is remarkably effective at conveying both the acute anguish and the paralyzing hollowness of depression. To the extent that the destructive potential of Melancholia is a metaphor for her private melancholia, it is perfectly apt. One of the chief torments of serious depression is how disproportionate and all-consuming the internal, personal sorrow can feel.

There is a grim vindication — and also an obvious, effective existential joke — in Justine’s discovery that her hyperbolic despair may turn out to be rooted in an accurate and objective assessment of the state of the universe. Mr. von Trier, inspired (if that’s the word) to make this movie by his own experience of depression, gleefully turns a psychological drama inside out. The world, Justine declares in her darkest moment of clarity, deserves its awful fate. The perverse achievement of “Melancholia” is how difficult it is to argue with her conclusion.