When comic-book villains like Harley Quinn, Deadshot and Killer Croc join forces in “Suicide Squad” on Aug. 5, viewers will see plenty of bad behavior: physical punishment, betrayal, emotional torment, sick jokes.

As it turns out, all of that happened behind the camera, too.

“Suicide Squad,” based on the DC Comics series about bad guys who undertake dangerous missions for the government, may go down as one of the most colorful film productions in recent history. As shooting progressed in Toronto last summer, Jared Leto delved so far into his role as The Joker that he sent creepy gifts to his castmates: Will Smith, who plays the hit man Deadshot, received bullets, while Margot Robbie, who plays Harley Quinn, got a live rat. The Australian actor Jai Courtney grew a beard for the role of Captain Boomerang, only to have director David Ayer immediately destroy it with a pair of clippers. Joel Kinnaman, who plays the military man Rick Flag, was required to go through 55 hours of sleep deprivation and watch videos of beheadings.

“I have a little recipe book I follow when prepping a film,” says Ayer, sounding rather tickled by all the stories. “It’s kind of normal for me. It just wasn’t supposed to be out there so much.”

Advance interest in “Suicide Squad” began at least as far back as 2014, when Ayer’s involvement was announced. As a writer or director behind gritty cop-dramas (“Training Day,” “Street Kings”) and war films (“Fury”), Ayer might seem an odd choice to spearhead a comic-book movie, but he says the project leapt out at him during a discussion with Warner Bros. executive Greg Silverman.

“We just kind of went through what projects he had, and ‘Suicide Squad’ came up,” Ayer says. “I was like, ‘That’s it!’ They sent me a giant box of comics, and I started reading.”

advertisement | advertise on newsday

Ayer explains his attraction to the material this way: “The idea of bad guys trying to do good things, and people coming together to form a family, that’s one of my tropes in my films. It was exciting for me to explore the gray areas of the comic book movie. Normally, they’re about the good guys, and I think they’re boring because they’re always going to do the right thing. These guys, you never know what they’re going to do.”

Among the squad’s other members are El Diablo (Jay Hernandez), a Los Angeles gangbanger with the ability to rain fire upon his enemies; the reptile-human Killer Croc (Adewale Akinnuoye-Agbaje); and the vengeful swordswoman Katana (Karen Fukuhara, a model making her acting debut). Cara Delevingne plays June Moone, otherwise known as the Enchantress, who puts the team’s hard-earned loyalty to the test.

If the squad has a moral center, it’s Kinnaman’s Rick Flag. Although government agent Amanda Waller (Viola Davis) implants miniature bombs in the villains’ necks to keep them in line, Flag must hold them together as a unit.

“This character is one where David had a very specific idea of who he was,” says Kinnaman, who recalls discussing the role in depth with Ayer. “He’ll ask, ‘What’s your biggest fear?’ And he’ll keep digging, and he’ll tell you his,” says Kinnaman. “All of a sudden you’re admitting the things you’re most ashamed of, the worst things you’ve done.”

He adds: “And then, on a well-chosen day on set, David would completely betray that trust and pour it out on you. In front of everyone. And that gets a reaction that’s very real.”

Ayer, for his part, puts it a different way. “Betray — I wouldn’t call it that,” he says. “But if you have a road map of someone’s emotional life, you can point at that map and say, ‘Go there.’ ”

Despite Ayer’s tactics — or perhaps because of them — the cast of “Suicide Squad” quickly became close-knit during rehearsals, according to Courtney. “It didn’t matter who you were. Karen Fukuhara, it’s her first film, but then you’ve got Will Smith. And we were all just sitting around, letting go,” says Courtney. “I can’t wait to keep going with this role. My only hope is that I get to stay alive, at least through the next couple of episodes.”

Even before its release, “Suicide Squad” has spawned a spinoff involving Robbie’s Harley Quinn and other female characters. That project is now in the works. Ayer may or may not be involved — “You never know,” he says cagily — but he expresses deep pride in his finished product.

advertisement | advertise on newsday

“I make movies to work with actors. I love actors,” says Ayer. “They came together as a family, and I got to be part of that family.”