To start off, a derivative represents a rate of change. If you have width, and you differentiate it with respect to u, you have dw/du or the rate of change of width with respect to u. The keyword here is rate. So you can have 3 width units per 1 u unit. In programming, specifically game programming, we are usually interested in the rate of change of a dimmension with respect to time. So dz/dt, where z is a dimmension.
One of the biggest mistakes I see in the forums is a notation mistake- users are adding dw to width. Like so:
//given int width
public int addWidth(int dw){
width += dw;
}
While this is syntactically correct, as dw and width are both ints, this is very misleading. It implies that you are adding a rate (like 3 meters/second) to a dimmension or quantity (3 meters). If you would instead like to add a quantity over a period of change, first multiply by that unit of change (exactly like dimmensional analysis for those who have had Chemistry), then add that product to the original quantity. So sticking with our width example, and we want to find the total change over a period of time, we can set up a linear equation:
width = dw*time + startingWidth;
So, the width is equivelant to the rate of change (or slope) over a period of time, plus the original width. Let's go over the applications of derivatives in programming. When you go to repaint() your window every x seconds, you now have a time to extrapolate the new dimmension or location of your Object. So if you are moving a characer 3 pixels per second, repainting every 100 seconds, you know that on ecah repaint, you want to move your character 300 pixels.
Now let's discuss about related rates. As the name suggests, related rates are used to relate the rate of change (so we need derivatives) of multiple dimmensions over a common dimmsion, usually time. So what are the applications of related rates in programming? Think about if you have a non-cubical (spherical, cone, cylindrical,triangular or rectangular pyramid, etc.) room flooding with water and a character trying to swim to the surface, how do you calculate the speed at which the character will need to swim in order to rise faster than the water? Let's work through this using a Sphere shaped room. The volume of a sphere is V = 4/3 * pi * r^3. When you differentiate Volume with respect to time, you get dV/dt = 4 * pi * r^2 * dr/dt. So the rate of change of the volume with respect to time is equivelant to 4pi * radius-squared * rate of change of radius with respect to time. Once you determine a constant rate of change for the radius, you can solve for the rate of change of the volume. Now it should be fairly simple to compare the position of the character and the rate at which it is swimming upwards, to the total volume and dv/dt.
For those of you who have played the N-Game, you'll find applications for this second example. So in certain levels of the N-Game, there is a laser-turret which targets the Ninja and fires a laser once it gets a lock. So how does this pertain to related rates? In the last example, we determined rate of change of volume. For this problem, we will be calculating dc/dt, or the rate of change of the hypotenuse for a right triangle. To begin, start the Ninja on a y = k line with the turret. This line is your b line in the pythagorean theorem a^2 + b^2 = c^2. Now, we will calculate dc/dt based on da/dt, or the rate of change of the hypotenuse based on the rate of change of line a. So since we've set up the Pythagorean Theorem, let's differentiate: 2a*da/dt + 2b*db/dt = 2c*dc/dt. Since line b isn't changing, db/dt = 0. So now our equation becomes 2a*da/dt = 2c*dc/dt or more simply, a*da/dt = c*dc/dt. It is up to you the programmer to determine da/dt, and c can be found from the Pythagorean theorem, so it should be fairly straight-forward to solve for dc/dt.
Now that we've talked about the math, let's talk about programming these concepts. As I talked about in my first derivative example, we can use the elapsed time to estimate positions from rate of change. In animation programming, we repaint() the window at set intervals so as to avoid flickering. So basically, you can position your objects according to their starting positions, modified by the number of repaints * rateOfChange. So dp/dt * t, or change in position with relation to time multiplied by time.
Edit: Thanks to Galois for catching my typo.
This post has been edited by macosxnerd101: 03 April 2010 - 11:29 PM








MultiQuote





|