Saturday, October 31, 2009

Making Stuff Move: Euler's Method

Before we jump into Euler's method, we need to understand two basic things: acceleration and Newton's Laws of Motion, especially the second law.

(If you don't already understand how position, velocity, and acceleration all fit together, you may need to study pictures and animations. Try the flash animations under "Classical Physics" on this page.)

Imagine a rocket ship that doesn't have enough fuel to get into space. After it burns all its fuel, it will be falling with nothing but gravity affecting it. Gravity will push anything straight down with a constant acceleration. (Unless the object is stopped from moving, say by being on the ground.) The constant acceleration due to gravity will make a free-falling object have more and more velocity as time goes on. A rocket that's been falling for a longer time will be moving faster than an rocket that was only falling for a short time.

You know what happens when you accelerate a car (with the accelerator pedal, of course): the speed of the car will increase. The magnitude of the acceleration tells you how quickly the speed is changing. You also know what happens to the car when it has speed (or velocity): the position changes. The position might change so quickly that it interferes with a policeman's radar gun. You can find the speed from the acceleration, and the position from the speed. Acceleration is the "derivative" of velocity, which is the "derivative" of position. You can think of derivative as a difference, or the amount of change. So if the car is accelerating, the velocity is becoming larger. The car is moving faster every second.

Once you understand acceleration, Newton's Laws are very easy. Newton's second law is:

F = m*a

Force equals mass times acceleration. This equation is describing a specific situation in which a force is pushing against an object and making it move. If the object has mass m and it accelerates with a magnitude a, you know the force pushing against it is m*a. And in a form that's more useful to us:

a = F/m (divide both sides of the equation by m)

If the object has mass m and you know the force against it is F, the object will accelerate at F/m.
The rocket ship burns fuel to create a force that pushes upwards. That force causes an acceleration on the mass of the ship. The same engine on a lighter ship will cause more acceleration.

Now we can move on to Euler's method. The acceleration in most simulations is changing all the time, so finding the position of the objects becomes extremely difficult. Euler's method is just a dumb assumption: assume the acceleration never changes for a short amount of time. Then we can use this constant acceleration to calculate the position and velocity of the objects a short time later.

Here are the actual calculations you can use (which you can derive yourself with a little calculus or a heaping helping of algebra):

starting position is x
starting velocity is v
if acceleration a is constant for a short period of time t,
the new velocity is v + t*a
the new position is x + 1/2*t*t*a + t*v (1/2*t*t*a is one half t squared times a)

You can probably understand the meaning of the expression for velocity even if the expression for position looks too weird. You can also approximate the new position by x + t*v since 1/2*t*t*a is usually very small.

And here's a little explanation of the way you'd use that in a program:

Use our same rocket ship with mass m, but this time give it plenty of fuel and put it in space. You can calculate the forces against the ship at any time. The forces involve the gravity of planets, the ship's engines, and other things, such as little asteroids hitting the hull. You have lots of little functions that calculate the forces, so just sum them up. (You do know how forces can be added up, right? OK, just trust me. You can add them up into one big force.)

Force = gravity(depends on ship's position) + rocket + asteroidA + asteroidB
(You can see how the force might be very complicated and change dramatically over time)
Accel = Force/Mass

We draw the ship at its current position, then update and draw at its new position after a short time.

NewPosition = Position + Accel*0.5*t*t + Velocity*t
NewVelocity = Accel*t


Nothing could be easier! draw the frame and calculate Accel again, then update the postion for the next frame.

in C++/Java-ish code:
while(simulationIsRunning) {
accel = calculateCurrentAccelleration();
ship.pos += (ship.vel*t + accel*t*t*0.5);
ship.vel += accel*t;
ship.draw();
}

Next time, "Making Stuff Move: The Two Most Interesting Forces". Don't miss it!

No comments:

Post a Comment