I'm Makin' A Game: Accounting for Gravity
by fobwashed
This is a short vid, with a long post about how I dealt with the gravity situation. I love physics. I love math. But they can be a real pain in the ass when you don't understand them -_-;; The Math Behind Gravity So, I've had a pretty basic way to do targeting before with the balls that show the trajectory and whatnot, but it was janky and a sort of slamshod put together business and was pretty...
This is a short vid, with a long post about how I dealt with the gravity situation. I love physics. I love math. But they can be a real pain in the ass when you don't understand them -_-;;
The Math Behind Gravity
So, I've had a pretty basic way to do targeting before with the balls that show the trajectory and whatnot, but it was janky and a sort of slamshod put together business and was pretty restrictive in the types of aiming I could use. The hardest restriction being that the height of the throw couldn't be altered on the fly =\ This had to be changed so I delved into the math behind gravity and how I could use that to determine arcs and such. The following is basically a math class for anyone interested. To some it'll be sort of hard math, to most it's basic physics with some intermediate algebra. All I know is, I remember when I was learning this stuff in school thinking "when the hell am I ever gonna need this crap? Quadratic formula? psshhhhhhh" but lo and behold, this shit's everywhere.
Basic Physics
Physics wise, you can pretty much completely separate directional movement and the force of gravity. At least, in this game you can as I'm not going to calculate anything like wind resistance and whatnot. . . at least not yet. Getting the direction that an object should travel is easy. You just normalize the difference between the target and the object tossing the item. The problem is determining how fast this object should travel at a given launch velocity. This gets even more complicated (at least for me) when you start to factor in varying heights for the target and the tosser.
The Formulas
The base of applying gravity is simple. The speed an object is moving vertically is just increased by the amount of gravity over and over. This is simplifying things greatly if you're looking at the real world, but again, I'm working without any frictional drag. And also as of yet, haven't found the need to apply any sort of cap on fall speed. So, if I want to figure out the Y value of an object given a launch speed and an elapsed amount of time,
Y = launch speed * elapsed time - gravity * elapsed time ^ 2 * 0.5.
That right there is how I determine how high the targeting balls are to represent the arc the thrown object will take.
Now, if I'm talking just plain old flat ground, the speed that the object will travel is easy to find out. Basically, using the previous formulas, you can determine how long an object will be in the air assuming it was thrown from the ground, and will land on the ground. You can use an easy
time of flight = 2 * launch speed / gravity
Knowing how long something is in the air, you simply divide the distance between the two points and you know how fast you need to travel to reach the destination as the object is hitting the ground.
The real difficulty I ran into was figuring out the arc to hit the target when the target was above or below the tossing point. To get this bit of information, I ended up using the quadratic formula. First, I had to figure out if the target is above or below me using both their heights and getting the difference. If I always place the tosser at (0,0), I could use the QF, plugging in the difference in height as the Y value to figure out how much time, with a set launch velocity, would have to expire before the tossed object hit that height.
Time In Air Till Impact = launch velocity / gravity + sqrRoot ( launch velocity ^ 2 / gravity ^ 2 - 2 * target height / gravity)
With the knowledge of when in time, an object launched at a specific velocity would hit the ground at the specified height, I can now divide the distance that I need to cross by that amount of time and get the speed that the object needs to be throw in. And BAM.
Conclusion
I've got the core of all my gravity based targeting in place. It's set up in a way in which I can vary the height and if I wanted to, the speed of thrown objects. This'll go a long way in terms of enemy attacks in the future as well as giving the player more options. I'll still have to figure out a nice way to give the player control over their item throws, while balancing the complexity of the action. Progress.