Im a grade 11 high school student, today in physics, we were digging deep into gravity. The whole equation with the mass of earth and distance from the center of earth, blah blah blah. Its no good to me if its not in code! So im wondering, how could i turn that into an accurate snippet of code. Most likely with a timer. Thanks,
___
Dan
Have you any idea what it is that you want to do? Do you want to plot something? Do you want to calculate something? If you know the equation, you can calculate the force on the object. You know that F=ma (you do know that, right?). And you know that v = u+at, so given the starting velocity, you can work out the force and hence the acceleration and hence in a given time interval you can work out the new velocity. You can also use s= ut + 0.5at^2 to get from a given initial velocity and calculated acceleration to a distance moved in a given time step.
When it comes to physics in programming, especially in games, Timers are REALLY not the way to go. You are gonna literally need to use real time to pull off accurate physics.
Yes, F = MA, but to break it down into 2D vectors, it's actually this:
F_Net.x = m * a.x
F_Net.y = m * a.y
Where F_Net is the net force, meaning the total amount of forces applied to the object, m is mass, and a is acceleration. But it's gonna need to be twisted around since acceleration is the variable needed:
a.x = F_Net.x / m
a.y = F_Net.y / m
Since this is a gravity simulation, F_Net.y should equal -9.8, or more precisely -9.80665, and the coordinate system in your form should be Cartesian:
Code:
+y
^
|
+ -->+x
If you are confused, I'm simply saying up is in the positive y direction, and right is the positive x direction.
Next, you are gonna need an integration method. There are many out there, such as Forward Euler, Backwards Euler, 2nd order Euler, Velocity-less Verlet, Velocity Verlet, Leapfrog, 2nd order Runge Kutta, 3rd order Runge Kutta, 4th order Runge Kutta, etc. Forward Euler is the easiest, but only 100% accurate linearly. 4th order Runge Kutta is the most accurate linearly as well as angularly. But since you are a beginner at physics, well stick with Forward Euler:
p.x = p.x + v.x * dt
v.x = v.x + a.x * dt
p.y = p.y + v.y * dt
v.y = v.y + a.y * dt
Where dt is the time step size. Be sure to use small step sizes, like somewhere between .01 or .001. I'll provide source code later on. ;0
Last edited by Jacob Roman; Mar 2nd, 2006 at 06:01 PM.
It's turning into a real physics engine apparently! I'm still having issues with elasticity because for some reason, it gets screwed up after a few seconds. Gimme until tommorow and it should be all good.
I'll improve it in the meantime and fix that little issue. I still need to add Air Resistance because otherwise it will continue to accelerate. Here's what I mean. If you dropped an elephant and a ball off a tower, without air resistance, they both will hit the ground at the same time.
well i havent gone thru the rest of the topic n detail, but i think one soln could be to use a timer and change the coordinates of an object so its "attracted" to another object. to achieve the effect of acceleration you can decrease the timer's interval by some amount, directly proportional to accelaration. it will cause the moving object to mve faster as time passes (and it gets closer to the other object)
Because timers are inaccurate, inconsistant, are NOT real time, get worse the more timers you use and run simutaniously, are very limited, etc. They are just awful, and are never used in commercial games. Found these facts out the hard way throughout my 9 years of VB experience. Managed loops locked at a certain framerate are the way to go. And if you are using DirectX, it handles the framerate for you based on your monitors refresh rate. The only thing good about timers are the fact that it doesn't use much CPU. If you are after making a realtime game engine and physics engine, stay away from timers.