One of the projects I’m working on at the moment is a game in which you play as a bird, and do normal bird things. One of those normal bird things is flying. Flight is hard.
When I started I figured that I could just go to wikipedia and there’d be two or three equations that I could semi copy-paste into unity that would just kind of work. Wikipedia certainly had some equations for me, but they weren’t the nice n’ simple ones I was hoping for.
This is an equation to calculate aerodynamic force. It’s a surface integral involving tangential stresses and pressure differences. While I’d normally be all about figuring out how to solve surface integrals and learning about pressure and what not, time isn’t exactly on our side. So I kept looking. Alas, I found a more inviting equation.
It looked more like something that I could easily drop into unity, so I used it. Found here and on wikipedia. This did require a gas density, but that was easy enough to google. Somewhere on google mentioned 1.229 as the air density at sea level, so I just used that. Also provided was an approximation for the lift coefficient for thin aerofoils, which thankfully was just
Cl = 2πα where α was angle of attack. Despite how simple it was, I still managed to get it wrong and for at least two days I was flying with a negative angle of attack, which meant that when I pulled back I’d fall faster and when I leant forward I’d float away. But I WAS flying and that was the main thing. I also had an issue with NaN’s whereby if something was negative (I think forward velocity) you would be flung so far that floats would stop working. I managed to fix that when I figured out the angle of attack though. At that point (and after multiplying air density by 50) I had this.
Kinda dodgy, but also kinda working.
After about a day of tweaking (and playing with particle effects, materials, and adding controller support) I had this.
This felt much better. The gliding worked (pretty sure air density is still ~50x that at sea level) and leaning forward didn’t send you skyward. Admittedly there are some hacks afoot here (see last blog). One hack is a small correction that takes the difference between the forward vector and the velocity and applies that back to the bird. This (broke physics and) stopped the bird from maintaining momentum perpendicular to the bird, in other words, if you made a sharp turn you wouldn’t keep going sideways until you hit something. Another is the fact that I’m multiplying air density somewhere between 50 and 100. This made lift a lot easier to generate with small wings and stopped the bird from just falling out of the air at 9.8 m/s². Considering that these hacks are totally necessary to make the game not feel like a bad bird simulator, I’m completely ok with having suffocatingly thick air.
To finish, here’s something else I worked on other than making bird flight not bad.