Defining local-gravity areas

Byfron
Posts: 3
Joined: Sat May 14, 2011 7:46 am

Defining local-gravity areas

Post by Byfron »

Hi everyone...

Is there a way to define several space volumes affected by different gravity vector? I guess that won't be supported out of the box, so I thought I can do it by hand by calling setGravity on the objects localized in these areas, in each update step.

Can anyone suggest a more elegant way of solving the problem? Maybe inheriting a rigidBody and overriding some method?
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Defining local-gravity areas

Post by dphil »

One way would be to use ghost bodies encompassing those volumes, and have them iterate over their contained bodies and apply the appropriate gravity at each step. Not sure if this is more "elegant" or not, but it's one way, and would automatically process the spatial localization for you (ie you don't manually calculate where bodies are, the different gravity zones would just tell you what's in them).
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: Defining local-gravity areas

Post by Mako_energy02 »

Do you mean having pockets of anti-gravity/reverse gravity in the world? This is actually somewhat simple to setup if you use a btPairCachingGhostObject to detect all objects within it's shape, get pointers to them by iterating over all of it's cached contact pairs after each step, and then call "btRigidBody::setGravity(const btVector3& gravity)" on every rigid body found inside.

I've actually made my own Gravity Well/Gravity Field classes using this concept, and they are surprisingly performant, at least until the massive gravity well the size of the world has sucked everything into it's center and there are 20+ collisions going on constantly...it's less performant then...but still fun to watch.

edit: Doh, dphil beat me to it.
Byfron
Posts: 3
Joined: Sat May 14, 2011 7:46 am

Re: Defining local-gravity areas

Post by Byfron »

Thank you both.

I think that approach can solve my problem. Let's say that I'm simulating a small "planet" with its own gravity, so depending on the position of my solids, the gravity will push them towards the centre of the planet. I guess I can quantize the space on the planet surface and define a ghost body located in each of these locations, with its gravity pointing to the planet centre. So automatically any solid going through the ghosts, will be affected by the "local gravity vector".
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Defining local-gravity areas

Post by dphil »

Sounds good. And if you really wanted to you could apply gravity based on F = (G*m1*m2)/r^2 for more realism :)
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Defining local-gravity areas

Post by Flix »

Byfron wrote:I guess I can quantize the space on the planet surface and define a ghost body located in each of these locations, with its gravity pointing to the planet centre.
Maybe you can try using a single sphere shaped btPairCachingGhostObject (with a radius bigger than your planet), and at regular intervals ( a few frames should work fine if you don't want to do it on each physic timestep ), find all the rigid bodies inside it and apply to them the appropriate gravity based on the difference between their position and the center of the planet.

[Edit:] a simple btGhostObject would probably be much faster to use, if you can afford to retrieve rigid bodies from a "aabb squared" space around the planet...
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: Defining local-gravity areas

Post by Mako_energy02 »

I have profiled my approach using a btPairCachingGhostObject and to get all the bodies inside and apply the appropriate gravity for each one is only ~30 microseconds per field(I've had up to 3 fields going at once) when there were ~10 objects in the fields. Again this was until most of the objects bunched up in the center which caused stepSimulation() to take much longer due to all the collisions going on at once. That is really all you have to worry about...how many simultaneous collisions do you expect to have as a result of this gravity well?