PDA

Click to See Complete Forum and Search --> : vb6 Gravity Simulation


MeTTa@
Mar 2nd, 2006, 09:47 AM
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

MeTTa@
Mar 2nd, 2006, 01:34 PM
Well?, And all i would want to use is a shape square for ground, and a shape circle for ball....

zaza
Mar 2nd, 2006, 02:00 PM
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.

Try it, and see how far you get.


zaza

Jacob Roman
Mar 2nd, 2006, 02:29 PM
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:



+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

Jacob Roman
Mar 6th, 2006, 06:28 PM
Here's the source code to a nice gravity simulation using Forward Euler integration, with a scalefactor.

MeTTa@
Mar 8th, 2006, 10:18 AM
Thats a nice app, could you apply it to "bounce" off of an object?

Jacob Roman
Mar 8th, 2006, 11:28 AM
You want elasticity you mean? Yeah I can do that for ya. Give me some time to put that in. I was also gonna improve the gravity simulation.

In the mean time, try these other integration methods. 4th order Runge Kutta is the most accurate. ;)

http://www.vbforums.com/showpost.php?p=2373508&postcount=56

MeTTa@
Mar 8th, 2006, 07:55 PM
Hows it coming?

Jacob Roman
Mar 9th, 2006, 08:36 AM
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.

Jacob Roman
Mar 10th, 2006, 09:18 AM
Here it is. Try playing around with the elasticity values. 0.7 looks pretty realistic.

MeTTa@
Mar 10th, 2006, 10:51 AM
!AWESOME, great stuff... That is way kool.

Jacob Roman
Mar 10th, 2006, 11:18 AM
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.

Jacob Roman
Mar 10th, 2006, 11:32 AM
I fixed the problem. The timestep wasn't small enough. Make the timestep 1/1000, and make the scalefactor 5000

Jacob Roman
Mar 10th, 2006, 01:26 PM
Updated big time. ;)

|2eM!x
Mar 10th, 2006, 11:33 PM
Its missing force.bas JR :P

Jacob Roman
Mar 13th, 2006, 11:18 AM
No it's not. It's right there in the zip file. Look again.

midhat
Mar 15th, 2006, 12:36 PM
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)

Jacob Roman
Mar 31st, 2006, 11:49 AM
Timers should never be used, especially in a physics engine. ;)

midhat
Apr 1st, 2006, 06:49 AM
why not?

Jacob Roman
Apr 1st, 2006, 04:15 PM
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. ;)