Page 1 of 1

Putting Math Into the Context of Game Programming Part II- 2D Gravity

#1 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12680
  • View blog
  • Posts: 45,863
  • Joined: 27-December 08

Posted 03 April 2010 - 11:17 PM


So while helping out in the forums, I've seen a lot of misuse of mathematical formulas and notations in programs, specifically Precalculus and Calculus, that I hope to correct. Part II will cover particle motion, position and velocity functions, parametric equations and their applications in Game Programming.

Let's start off with parametric equations and their applications. Basically, parametric equations are used when coordinates along an axis are not related. So basically, we have an equation for the x-value and an equation for the y-value, which are usually related to time. In physics classes, they are commonly referred to as projectile equations. While we may occasionally use formal parametric equations (as we will cover later in this tutorial), it is important to understand how to modify x and y coordinates individually in a game. One application of this is having a smart missle track the target through an asteroid field or obstacle course of some sort. Obviously, the character will probably not be standing still, and the missle will still have to navigate the asteroid field, with multiple asteroids occupying the same x xor y coordinates. And without a human controller, we will have to modify the individual coordinates of the missle (or modify them parametrically) to allow it to continue to track the player while avoiding obstacles.

Next, let's move onto particle motion, position, and velocity functions. Basically, a position function is used to determine the height/vertical position of the Object, and particle motion functions are used to determine the horizontal position of the Object. Both are related to time. From a gravity perspective, position functions are usually negative quadratics, which means that the Object starts at some position, travels upwards to a maximum height, then returns downwards. Velocity functions are simply differentiated position or particle motion functions.

Now that we've covered position and particle motion functions, as well as parametric equations, let's get into gravity. From a game programming perspective, we care about gravity from the initial point at which a character begins to jump from the time it returns to the ground. So since the time it takes for the character to reach the maximum height is the time at which velocity = 0, we can take that twice that time as the maximum length we are interested for a particle motion equation. Let's go ahead and work through an example.

Given the position and particle motion functions as follows, where x is our particle motion function and y is our position function. Note that since we are mapping out a function for each coordinate, we are using parametric equations.
x = ln(t)^2; //t > 0
y = -4.9t^2 + 15t + 22;

We'll assume that time = 0 is the point at which the character initiates the jump. So as we discussed above, the time we care about for the jump is the time it takes the character to land back on the ground, or twice the time it takes it to reach the maximum height. So to find that max_height time, we'll need to differentiate y, and solve when y = 0. So:
y = -4.9t^2 + 15t + 22;
y' = -9.8t + 15; //y' is derivative of y

-9.8t + 15 = 0 //set y' = 0
-9.8t = -15 //solve for t
t = 1.531 //character reaches max at t = 1.531

So since we know that the character reaches the max height at t = 1.531, we can double that time to determine how long it will the character to return to the ground. So the overall duration of the jump takes 3.062 units of time.

So far, we have only been worrying about the height of the jump, but not how far away from the starting point the character will land. To determine this, we will need to go back to the particle motion function x = ln(t)^2;. Since we know t = 3.062, we can plug that in to the particle motion function, and we get 1.25 units along the x-axis. And when we go back and plug our t(max_height) into our y function, we get a maximum height of 37.463 units along the y-axis.

Now that we have our equations, it's just a matter of repainting the character at the appropriate (x,y) position, which we can easily determine from the parametric equations.

Is This A Good Question/Topic? 5
  • +

Replies To: Putting Math Into the Context of Game Programming Part II- 2D Gravity

#2 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon

Reputation: 4618
  • View blog
  • Posts: 19,384
  • Joined: 24-September 08

Posted 10 August 2010 - 09:08 AM

Parametric equations make me smile. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1