Flying physics is hard for developers and gamers.
Imagine you’re in your mid-30’s and you’ve got this plan to make a stupid game with your best friend from elementary school while you’re stuck at home during COVID19. The plan is: you’re a pigeon. Your job is to cover the world with poop. Your instrument of this destruction, a video game made in the Unity Engine.
In order to accomplish my dreams of world dookination, I needed to pilot a pigeon that could fly.
I’d like to think that my years of developing software has taught me to be more agile. I tell myself that my goal is to avoid building perfection, but to take small, incremental steps toward a “better” state. That’s the goal, but I always tend toward the perfect solutions in practice. Unity has a physics engine built-in which builds a physics map in the graphics card and runs calculations based on that. All this power, why not use it?
Unity’s physics engine is immensely helpful but, as I soon learned, it doesn’t really do flight physics. While it does things falling and bouncing off of other well, my pigeon just hit the dirt. He needs to soar across the open skies to perform his bombing runs! So I set to work on looking up how to model a glider in physics.
After some searching, I stumbled upon some 1990’s-looking websites from NASA detailing the Lift and Drag equations. Like all mathematical models of reality, these were just approximations. Close, but not perfect. I spent an entire weekend trying to adjust lift coefficients and figuring out how the velocity of the bird would generate lift.
My little bird would soar and then exponentially shoot off into the world of NaN, never to be seen from the physics engine again.
I eventually figured out that I needed to use the differential of the velocity when calculating lift (the change in velocity between the frames). This fixed the numerical instability and the bird stayed in the world, but things were still off. Rolling and banking weren’t working as expected. He would flip or roll and do loops when trying to turn.
I had been looking for posts about flight in Unity for some time now, but nothing was concrete. However, in my hours of frustration, I stumbled upon a gem in Stack Overflow. Apparently, there’s another equation that approximates flight called the Lifting Line Theory.
Anyway. I spent several hours trying to fix the flight model for this bird, trying to get the physics just right. Unfortunately, most birds fly far too quickly for what I’m looking for.
My friend and I have decided we’re better off with a bird that can only fly level and only in the direction it’s pointed. We’ll probably be switching to a top-down 2D shooter as well.
The lesson is: don’t make things too complicated at first. While I enjoyed dusting off quaternions and learning about lift approximation equations, it ended up not being useful. Start with simple and work your way up to the more complicated.