# I need help with a gravity simulator!

Printable View

• 12-10-2011, 05:18 PM
Karly_Kute_X
I need help with a gravity simulator!
Hey Guys,

I'm trying to write a game-like program which simulates an object falling to the ground, while I can do all the physics and maths behind this I really really
suck at java at this level :( if any of you guys could help me out with this I'd be forever grateful ^_^

Thanks,

Karly Xx
• 12-10-2011, 05:23 PM
JosAH
Re: I need help with a gravity simulator!
Per unit of time the velocity v increases with a value a (due to gravity) so at a time t+1 the velocity becomes v+a; the distance to the ground decreases by a distancve v, so the distance at t+1 becomes d-v. Repeat until d <= 0 and start your simulation at a height d0 and an initial velocity v0

kind regards,

Jos
• 12-10-2011, 06:10 PM
Karly_Kute_X
Re: I need help with a gravity simulator!
That's cool I just have no idea how to code that :( are there any examples of such a code here or if not do you know where I can find code examples?
• 12-10-2011, 06:21 PM
JosAH
Re: I need help with a gravity simulator!
Quote:

Originally Posted by Karly_Kute_X
That's cool I just have no idea how to code that :( are there any examples of such a code here or if not do you know where I can find code examples?

There is not much coding involved; as a bare bones minimum your simulation looks like this:

Code:

```double d= d0; // initial height double v= v0; // initial velocity while (d > 0) { // while no impact yet   v+= a; // adjust velocity   d-= v; // adjust height }```
kind regards,

Jos
• 12-10-2011, 06:45 PM
Karly_Kute_X
Re: I need help with a gravity simulator!
Ahh that makes more sense - although I need to represent the initial and final positions as vectors - I understand I need to use arrays to do this - I just don't know how to make the program do this, also I need to allow the user to alter the mass of whats being dropped and the mass of the planet :(:(

Grrr confuzzled! :\

K x
• 12-10-2011, 07:40 PM
JosAH
Re: I need help with a gravity simulator!
Vectors only make sense in the calculations if there is a horizontal component in the speed (vx, vy), otherwise the first component of these vectors will always be zero. The force applied to the falling body only infuences the vertical speed component. The mass of the planet only influences component 'a' in the calculations (i.e. the gravity factor)

kind regards,

Jos