Author Topic: Radial/planetary gravity?  (Read 2083 times)

Brady

  • Newbie
  • *
  • Posts: 3
    • View Profile
Radial/planetary gravity?
« on: June 16, 2015, 05:36:19 pm »
Is it possible to implement planetary gravity with Liquid Physics 2D?  For example, I'd like to have planetary bodies composed of liquid, and be able to blast bits of them off with impactors, and have the ejecta fly into the space surrounding the planet and be attracted to its center of gravity, possibly winding up in orbit, etc.  Also, as the planet loses mass to impact events, its gravity would decrease.

Does this sound like a use case that is feasible with this asset package?  Thanks!

Physical_Liquid_Conor

  • Administrator
  • Full Member
  • *****
  • Posts: 208
    • View Profile
Re: Radial/planetary gravity?
« Reply #1 on: June 17, 2015, 11:52:37 am »
Hi there is some discussion of this use case here:

Quote
http://gamedev.stackexchange.com/questions/53502/is-using-box2d-for-simulating-gravity-in-space-a-good-idea
http://mentalgrain.com/box2d/simulating-multiple-sources-of-gravity-in-box2d/


This is certainly possible depending on what results you are trying to achieve. Angry Birds Space uses box2d and planetary gravity. One thing to note though about using Particles instead of Box2d bodies is that there will be a lot more calculations. If you have a thousand particles in a scene and they are all attracted to each other there will be an overwhelming amount of distance/force calculations. You will likely have to make some optimizations, such as getting the number of particles in a group and representing it as a point mass. You will lose some accuracy this way and orbits may not be stable.

You can also look into integrating Kepler's equations if you are looking to achieve more stable orbits. There is some discussion of that here.

« Last Edit: June 17, 2015, 11:56:50 am by Physical_Liquid_Conor »

Brady

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Radial/planetary gravity?
« Reply #2 on: June 17, 2015, 10:16:42 pm »
Thanks, that's pretty much what I'd thought of doing: scanning the scene for "clumps" of particles, and wherever they're found in sufficient densities, treat them all as a single point of mass based on their average position.  The main thing was just making sure your liquid/particle system supported multiple custom forces/attractors, or if it always used the global gravity setting.

Thanks!

Physical_Liquid_Conor

  • Administrator
  • Full Member
  • *****
  • Posts: 208
    • View Profile
Re: Radial/planetary gravity?
« Reply #3 on: June 18, 2015, 10:50:58 am »
Well there is global gravity which will effect everything but you can set this to (0,0).

You should check out the ExampleScenes/splitParticleGroup to see how you update the groups after they have been split. Basically you want to go to the particle system and enable GetGroupData, then  use LPAPIGroups.GetParticlesInGroupCount(groupPtr) to find the size of the group.

Then calculate your force and apply it with: LPAPIParticleGroups.ApplyForceToParticleGroup(groupPtr, X, Y);

Brady

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Radial/planetary gravity?
« Reply #4 on: June 19, 2015, 12:06:38 am »
Excellent, that's exactly the info I was after!  Thanks!

RunSwimFly

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Radial/planetary gravity?
« Reply #5 on: September 09, 2015, 04:20:07 pm »
Correct me if I'm mistaken but wouldn't that just force all the particles in the group in the same direction? What if you wanted to attract them towards a point? Would the best way be to just get all the particles in the group and then apply the calculation in C#? Is that even possible?

I'm interested in creating a cross platform variation of my iOS game Germs which has fluid particles which interact globally with each other but are also subjected to attraction and damping relative to the centre of the germ to which they currently belong. I imagine I could discover the answer by watching all of the videos and reading through the sample code but I'm just a little impatient to know if this is going to be possible :)

RunSwimFly

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Radial/planetary gravity?
« Reply #6 on: September 13, 2015, 09:40:27 am »
I've had a look through the documentation and watched the videos and it seems the two options I have to achieve what I want are:

1. Get all particles within a circle shape for each of my attractors OR
2. Loop through all particles checking which attractor they are currently assigned to (stored in userdata)

and then use C# to apply my attraction and damping forces. Particles swap attractors based upon their interaction with particles from other attractors rather than proximity to other attractors so they need only consider one attractor in any given frame. Since only one attractor affects a particle at a time I'd wondered whether it might be more efficient to dynamically update particle groups to correspond to their current attractor and then use select based upon groups. However it doesn't seem to me that you can really alter groups in the fine grained fashion necessary.
In any case I've come up with another game idea which is perhaps more organic to the library and I'll implement that first as a learning exercise before I try shoehorning my other ideas into the library.
BTW the library looks great and quite extensible. Planning on bringing across some of my shaders to complement the existing metaball code to implement a 2.5D emboss type effect.
« Last Edit: September 13, 2015, 10:21:55 am by RunSwimFly »

RunSwimFly

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Radial/planetary gravity?
« Reply #7 on: September 13, 2015, 10:59:20 am »
Actually that still won't work. It appears I can get all the information I need by querying but there's nothing I can see in the API calls that allows you to apply individual forces to individual particles in a particle system or to directly modify the velocities of individual particles. You can only set a single force vector for a selected set of particles.
So the question remains (and boiling it down to the simplest case), if you wanted a group of particles to be attracted to a single point and orbit it how would you go about it and can it even be achieved? Ie how would you attract particles towards a point rather than forcing them all in the same direction?
If you can't do this presently is there any chance you could expose the SetVelocityBuffer method from liquidFun to allow this?
« Last Edit: September 13, 2015, 11:06:46 am by RunSwimFly »