0

What I need to achieve is a program which will simulate gravity in 2D plane (only x and y axis), using Euler equation, based on class of an astronomical object and a list of said objects.

As for the object, I need to give it both positions, a mass and a velocity (if it exists from the beginning). Radius is ignored, since I'm not going into collisions.

Then, I was told to recalculate every object's position and velocity accordingly, when I add a new object to the list, based on this new object's mass.

I have no idea how to do this, since all of this should be determined by time, right? It's a constantly changing situation, especially without taking collision into account. This means that all the objects will be inifnitely spinning and flying around, so I can't simply say "well, upon adding a new object, the previous one moved to XY and stayed there".

I tried searching for that before, but most gravity projects use Box2D engine or similar libraries, while everything I need to do is to use a simple class, a list and a formula.

I know the maths behind it, Gravitational Constant, force equations and whatnot, but I have absolutely no clue how to implement this. I'm having a big problem, since the assignment is for Monday, and having a lot of work on my own (I have a freshly started-up company) I forgot a lot from C++ lectures.

I will be really grateful if anyone could help me with that, thanks in advance!

EDIT: I really have not a slightest clue how to approach this problem, everyone I ask tells me to make a class with vectors, throw it in a list and create a method which will update all the parameters in time, but while I do know this, because I understand the subject, I do not know how to present it in code... I know I should do this earlier, but I'm really desperate right now. I will need to revise the code and understand it anyways, I'm no computer dummy and I will need to explain it to my professor, so I'll get the grasp of it - I just don't know C++ at this point and I'm probably done with my studies if I don't make it in time...

  • Every iteration step, calculate all the resultant forces and, based on that, change the velocity of the objects. So you probably need a mass (scalar) + velocity (vector) for each particle. – Aleph Dec 07 '13 at 11:07
  • You will also need to have a time step for your simulation, so that you can convert the sum of gravitation forces to a correct delta velocity for each time step. – RichardPlunkett Dec 07 '13 at 11:10
  • Great, thanks! But how to implement time steps? Any pointers? – KubaPrzetakiewicz Dec 07 '13 at 11:13
  • Depends on the accuracy you want. Basically you're using the time step to go from acceleration to change in velocity, by doing: delta v = a * delta t. Where delta t is your time step. What you're doing is a numeric approximation of integration, so the smaller your timestep, the more accurate. – Aleph Dec 07 '13 at 11:21
  • Yes, I have all the necessary math right here, since I have already written it down and I do understand it, however, I have no idea how to implement time steps in code... I have found this: [http://www.cplusplus.com/forum/beginner/60975/](http://www.cplusplus.com/forum/beginner/60975/), but there's nothing about velocity and every object is referring to the already existing origin, not other objects. It doesn't even include lists, I'm completely stuck... – KubaPrzetakiewicz Dec 07 '13 at 11:28
  • Is your basis a fixed *very large* mass with lots of small objects flying around, or do all masses need interacting with *every* other one? Say, Earth + artificial satellites vs. the solar system. – Jongware Dec 07 '13 at 11:33
  • They need to interact with _every_ other one unfortunately... I already have a solution in mind, when it comes to a fixed, large mass, but it's not what I'm looking for :( I need to have a class for objects and a class for list and every new item needs to adjust the behavior of other ones... I'm devastated. – KubaPrzetakiewicz Dec 07 '13 at 11:39
  • look here http://stackoverflow.com/a/20017455/2521214 and remove frictions and global gravity from it ... and add local gravity interactions – Spektre Jan 14 '14 at 10:34
  • btw what you mean with use of Euler equation ??? Isn't it for fluid dynamics? use instead Newton's gravity formula... or Kepler's equation but that one makes no sense because of absence of local interactions ... – Spektre Jan 14 '14 at 10:39

0 Answers0