Physics are a very hard thing to pull off in a calculator program, but if you can get realistic physics to work, you will have a very impressive program. This is not to out-perform BuilderBoy's wonderful post on Omnimaga, but to clarify some points he may have made.
Inertia, the key behind how realistic movement is done, is represented as the previous motion. For example, if the object moved 5 pixels in the last frame, the inertia is 5. Inertia is what makes objects keep going. Programatically speaking, you need a single variable to store inertia for each axis (X and Y).
In my opinion, the best way to implement physics is by storing the position in one place, the inertia in one place, and the requested change in one place. Let me explain how this works. The requested change is the amount of force to apply to the object each frame. This means that if the user presses the right arrow, you would supply a reqested change of 1, 0, to move it right. The physics engine, or whatever is updating the location of objects, takes the requested change and adds the inertia (the previous motion) to it. It stores the resulting value back into the inertia. Then, it adds this value to the position, applying the motion.
Now, with this system, an object will go on forever in one direction, which is not good. This is where friction comes into play. Friction is the force that slows objects down. When two objects rub against each other, kinetic energy (movement) is changed to heat energy and the objects slow down. However, if you throw a paper airplane in the air, it eventually slows down as well. This is because of liquid friction, or the friction between objects and a liquid, such as water or the air. The easiest way to implement this programatically is to gradually lose inertia.
The last thing to implementing realistic motion is gravity. Once you have everything else in place, gravity is easy to add. You simply need to subtract one from the Y portion of the requested change.
So all of this may be a little confusing, so here is some psuedo-code:
Code:
This can be improved to support more of Newton's laws of motion, which I will post at a later time.
Inertia, the key behind how realistic movement is done, is represented as the previous motion. For example, if the object moved 5 pixels in the last frame, the inertia is 5. Inertia is what makes objects keep going. Programatically speaking, you need a single variable to store inertia for each axis (X and Y).
In my opinion, the best way to implement physics is by storing the position in one place, the inertia in one place, and the requested change in one place. Let me explain how this works. The requested change is the amount of force to apply to the object each frame. This means that if the user presses the right arrow, you would supply a reqested change of 1, 0, to move it right. The physics engine, or whatever is updating the location of objects, takes the requested change and adds the inertia (the previous motion) to it. It stores the resulting value back into the inertia. Then, it adds this value to the position, applying the motion.
Now, with this system, an object will go on forever in one direction, which is not good. This is where friction comes into play. Friction is the force that slows objects down. When two objects rub against each other, kinetic energy (movement) is changed to heat energy and the objects slow down. However, if you throw a paper airplane in the air, it eventually slows down as well. This is because of liquid friction, or the friction between objects and a liquid, such as water or the air. The easiest way to implement this programatically is to gradually lose inertia.
The last thing to implementing realistic motion is gravity. Once you have everything else in place, gravity is easy to add. You simply need to subtract one from the Y portion of the requested change.
So all of this may be a little confusing, so here is some psuedo-code:
Code:
Start:
0->x
0-Y
0->I ; X inertia
0->J ; Y inertia
0->V ; X requested change
0->W ; Y requested change
Loop:
Draw object
if left is pressed, store -1 to V
if right is pressed, store 1 to V
if up is pressed, store -1 to W
if down is pressed, store 1 to W
if object is not colliding with other object
V+I->V
X+I->X
if object is on ground
I*0.5->I ; if it is on the ground, more friction
else
I*0.75->I ; If it is in the air, less friction
else
0->I ; stop it if it hit another object
if object is not on the ground
J+W-1->J ; Requested change plus gravity
Y+J->Y
J *0.75->Y
else
0->J
goto Loop
This can be improved to support more of Newton's laws of motion, which I will post at a later time.