Page 1 of 1

Attraction Local Gravity

Posted: Sun Nov 23, 2008 8:17 pm
by debug
Is there a way to make a body have it's own gravity so that it attracts other bodies?

The behavior i am after is based on mass vs body gravity bodies are attracted to the body with high gravity in addition to normal space gravity.

adversely if it is an interaction with something of infinite moment then the attraction would be opposite the body would be pulled towards the static body

Re: Attraction Local Gravity

Posted: Sun Nov 23, 2008 11:06 pm
by slembcke
In the trunk code, I added a demo of planetary gravity. http://code.google.com/p/chipmunk-physi ... o/Planet.c

Take a look at planetGravityVelocityFunc(). This is set as the velocity update function for the bodies added to the space.

Code: Select all

body->velocity_func = planetGravityVelocityFunc;
The velocity update function feature exists in the latest 4.1.0 stable as well.

Re: Attraction Local Gravity

Posted: Sun Nov 23, 2008 11:17 pm
by debug
Thanks mate, awesome SDK you got here looking pretty neat and not bad feature wise

Is there some way to tell specific bodies to not obey spacial gravity?

Re: Attraction Local Gravity

Posted: Sun Nov 23, 2008 11:25 pm
by debug
Oh i see i guess i would just override the velocity function on those objects and set gravity to 0

Re: Attraction Local Gravity

Posted: Sun Nov 23, 2008 11:29 pm
by debug
I tested this function out and if i make a box around the screen and fire a small box or triangle anywhere on the screen it always ends up smack in the top left corner of the screen when i would expect it to slam into the nearest static segment to its originating position

i basically need a rigid body that i can control manually to a degree guiding its downward fall against normal gravity whilst this rigid body is attracting resting or falling rigid bodies around it

additionally when it gets close to a static body it should be pulled not irrecoverably towards it.

in other physics systems this can be achieved by making things attracted by force to each other and i would love to avoid setting up springs for things within a range every frame and removing springs as things fall out of a sphere of influence.

Re: Attraction Local Gravity

Posted: Mon Nov 24, 2008 1:27 am
by slembcke
The function that I pointed you at simply pulls things towards (0,0) nothing more. You can just apply forces to objects in Chipmunk if you find that easier.

It sounds like what you want to do is to have every collision shape in the space have gravity that is applied to every other object. Chipmunk's collision detection doesn't calculate closest features, so you will need to find some way to do that on your own. Using the distances between the centers of objects would be simpler, though not quite what you want. You also should realize that calculating forces that act between every pair of objects gets expensive very fast. Every time you double the number of objects, you quadruple the amount of processing that needs to be done.

Re: Attraction Local Gravity

Posted: Mon Nov 24, 2008 11:34 am
by debug
I was thinking of something like "vector fields" / "force fields" as you would see in Physx or Havok where you define a spatial representation of a force that is part of the force solving for the physics engine which you can move around and modify then when things are colliding with the force field space their velocity is modified based on a function of that space.

How would i go about making some kind of pseudo force field system in chipmunk or is it an option for something you would like to make available to the system allowing predefined and customizable spatial forces such as Brownian motion and vortices.

Re: Attraction Local Gravity

Posted: Sat Dec 26, 2009 12:40 pm
by ehudros
This is a very old thread, I know, but it seems to be exactly what I'm after as well.
Is there a way to achieve what debug is talking about? Let's say I have several planets, all having different gravities. What would be the best way to simulate that?

Re: Attraction Local Gravity

Posted: Sun Dec 27, 2009 11:51 am
by slembcke
One of the new demos shows how to set up planetary gravity. To have more than one planet, simply calculate the gravity for each planet separately and add them together.