This project has moved and is read-only. For the latest updates, please go here.

Planetary gravitation

Feb 8, 2009 at 2:50 PM
This discussion is a continuation of the discussion here.

@RCIX: What I would like to see is a controller where you give it a radius, strength and a list of bodies.
This way we have a many body to one gravitational field relationship and that provide easier to read code with a lot of flexibility.

After you wrote about the "list of bodies" implementation I was laying all night thinking about it and what you came up with was actually very close. Your current patch does it very well, but I would like one controller to function with one gravitationfield instead of one.
The reason for me to write this to you and not change it myself is that you wrote that you were still testing it and it might contain errors.

Great work so far. I look forward to providing a simple and powerful planetary gravitation implementation to the masses.
Feb 8, 2009 at 4:53 PM
Oh, and another idea. What about the reverse? Just like the sample with explosions but a constant force? I guess something like this is called reflection field.
Feb 12, 2009 at 3:21 AM
Ok ive been doing some testing and work and i think ive fixed my formula for applying gravity but i gotta tweak it and apply it to all of the gravity code. As for what you're asking, i think mine does that: it just can apply gravity based on points in space or other bodies (it does it by looping over each body in the simulator and then performing gravity calculations to all of the bodies that it thinks generates gravity). About the reverse gravity: wouldn't just setting the gravity strength to a negative value work?
Feb 12, 2009 at 3:22 AM
I'm trying to cram as much functionality as i can into as few classes as possible to minimize implementation work for you (and me). If you want me to split some of this up though i guess i could do that.
Feb 12, 2009 at 3:26 AM
Edited Feb 12, 2009 at 3:27 AM
Haha, That is what you get when working without sleep for 20 hours. And to call it a reflection field, I was probably thinking about repulsion field.
Anyway, good to hear you have been working on it. Looking forward to see what you have come up with.

Edit: That reminds me... It's 4:27 at night here, should go to bed.
Feb 12, 2009 at 5:28 AM
Edited Feb 12, 2009 at 5:29 AM
Well i've finished all of the work/testing and it should be just fine now. One problem though: if you are using the DistanceSquared type for your gravity, because of the way i had to do it it acts really weird when zooming by a gravity point: right after it passes through the center of gravity, it acts like someone kicked it in a random direction and flies off. This is because you need enormus forces to get good results with that type of gravity and since after passing through the center spot its right next to the point and gets huge forces applied to it.
Feb 12, 2009 at 5:38 AM
BTW the amount of force range to get planetary type gravity is 1 to 5 thousand. For a more subtle effect try say a few hundred (for like objects that slowly follow the player in a overhead 2d shooter)
Feb 13, 2009 at 2:52 AM
In case you didnt notice, i put up the patch for the gravitation fields.
Feb 13, 2009 at 4:41 PM
I have RSS updates on everything that happens in this forum. Thanks for uploading it, I will take a look at it later today.
Feb 13, 2009 at 11:34 PM
Edited Feb 13, 2009 at 11:34 PM
I've had a look at your patch. It looks good.

I'm going to change some things. I guess you wanted the PlanetaryGravityController to work both on specified points, but also between different bodies, right?

I think that the gravity controller should work only on specified points and that the two joints should become controllers instead (Joints have constrains and because gravity does not have that, they should be controllers).

The 3 controllers would be like this:
1. PlanetaryGravityController (renamed to GravityController) applies gravity to all bodies with a specified number of points.
2. GravitateJoint (renamed to GravityField) applies gravity to a subset of bodies to a specified point.
3. FixedGravitateJoint (renamed to GravityBody) applies gravity to one body. Gravity that comes from another body. The mass of the body would be taken into account. The higher mass, the larger magnitude of gravity.

What do you think about that?
Feb 14, 2009 at 12:42 AM
If i understand you correctly, the (currently PlanetaryGravityController) would apply gravity to all bodies based on a number of points specified by the user, where the (currently GravitateJoint) would apply gravity from several bodies to all of the bodies in a simulation (or a subset of bodies if the developer so chooses), and the (currently FixedGravitateJoint) would be single body to single body gravitation. If that's what you're gonna do then thats fine. i say this because of that very annoying bug with the DistanceSquared gravity. and yeah i realized that gravity should be based on mass like right after i uploaded the patch.
Feb 14, 2009 at 12:47 AM
Saw you mentioned a bug in the patch too, forgot to ask about it.
Anyway, as for the computation of gravity, you have several choices, BorisTheBrave from Box2D has a set of controllers where he also have a gravity controller, the Physics2D project also have 2-3 controllers that applies gravity. They all work differently, you could take a look at the other controllers to determine what you think would be the best and then make it fit to your needs.
Feb 14, 2009 at 3:29 AM
Well i can't find the Box2D controllers but i took a look at the method that physics2D uses to apply gravity, and it's virtually the same as mine. However i multiply the gravity by 100 if the user is using the DistanceSquared gravity type because that is what provides a decent simulation of gravity under that method (i stepped through the code and the gravity force was like 0.03 otherwise). I'm just concerned that the user will have problems (the way i understood your plans for my code the GravitateJoint would be converted into something like the PlanetaryGravityController but you had to tell it which bodies you wanted to be affected by the gravity) and go "huh? why can't i just create this controller and give it a list of bodies that i want to generate gravity and be done with it? why do i have to tell it which bodies that i want affected by my gravity too?".
Feb 14, 2009 at 1:19 PM
I've uploaded a checkin containing your patch. (checkin #46818)

You can try it out by downloading the snapshot, open SimpleSamplesadding and add the following line of code to Demo5 LoadContent():
ComplexFactory.Instance.CreateGravityController(PhysicsSimulator, new List<Vector2> { ScreenManager.ScreenCenter }, 1000, 500);

Right now it does not take body mass into account, but I kept it as you made it with the list of bodies and list of points. If you have any additions, feel free to contact me.
Feb 15, 2009 at 4:13 AM
Well the code looks fine, and aside from the factory missing a couple of versions of the creation code and the aforementioned mass calculation missing, it looks done. Two things though: why did you make the gravity type changeable after construction (it doesn't make sense that gravity should be able to switch between the various types while it is being used), and why don't you use the ControllerFactory to create these? it seems perfect for the job.
Feb 15, 2009 at 2:47 PM
As for the gravity type, I can't take every situation into account and it's not really that important if it's writeable or not after construction.
We don't have anything called ControllerFactory anymore. It was used for springs, but that was renamed to be SpringFacotry. I've inserted the GravityController into the ComplexFactory.
Feb 16, 2009 at 6:46 AM
hmm... All right thats fine. I can't think of anything else but maybe the ability to more easily add gravity types. That can be for a future version tho :)