Orbit gravity around a point

Official forum for the Chipmunk2D Physics Library.
Javawizkid
Posts: 47
Joined: Sat Oct 23, 2010 2:25 pm
Contact:

Orbit gravity around a point

Post by Javawizkid »

How can I achieve this, I can't find any examples around. Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Orbit gravity around a point

Post by slembcke »

There is a demo that ships with Chipmunk called planet that does exactly that.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Javawizkid
Posts: 47
Joined: Sat Oct 23, 2010 2:25 pm
Contact:

Re: Orbit gravity around a point

Post by Javawizkid »

Ahhhh didn't find that until I downloaded chipmunk directly because there aren't any examples bundled with cocos2d. Thanks :D
Javawizkid
Posts: 47
Joined: Sat Oct 23, 2010 2:25 pm
Contact:

Re: Orbit gravity around a point

Post by Javawizkid »

I've had a look at the example but cant seem to change the position of the gravity. I can move the planets shape and body but it's gravity doesn't move with it :s
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Orbit gravity around a point

Post by slembcke »

The gravity actually doesn't have anything to do with the planet body. I just put the body in the same place for obvious reasons.

The gravity is calculated in this function which is assigned as the velocity update function for all of the bodies:

Code: Select all

static void
planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
        // Gravitational acceleration is proportional to the inverse square of
        // distance, and directed toward the origin. The central planet is assumed
        // to be massive enough that it affects the satellites but not vice versa.
        cpVect p = body->p;
        cpFloat sqdist = cpvlengthsq(p);
        cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));
        
        cpBodyUpdateVelocity(body, g, damping, dt);
}
(0,0) is the center of the screen, and also where the gravity emanates from. All you need to do is make p (the position of the body) be relative to where you want gravity to come from:

Code: Select all

cpVect p = cpvsub(body->p, gravityPosition);
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Javawizkid
Posts: 47
Joined: Sat Oct 23, 2010 2:25 pm
Contact:

Re: Orbit gravity around a point

Post by Javawizkid »

So how could I have multiple planets? Repeat the process?

Done it :D thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Orbit gravity around a point

Post by slembcke »

Javawizkid wrote:So how could I have multiple planets? Repeat the process?

Done it :D thanks
If you want more than one planet, you just need to add all the g vectors from all the planets together.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Javawizkid
Posts: 47
Joined: Sat Oct 23, 2010 2:25 pm
Contact:

Re: Orbit gravity around a point

Post by Javawizkid »

What does the -50000.0f in

Code: Select all

cpVect g = cpvmult(p, -50000.0f/cpvdot(p, p));
do? Shouldn't it be the equation of gravity?
Javawizkid
Posts: 47
Joined: Sat Oct 23, 2010 2:25 pm
Contact:

Re: Orbit gravity around a point

Post by Javawizkid »

Code: Select all

static void
planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
How do I access a global variable within this function because it says it's not declared?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Orbit gravity around a point

Post by slembcke »

If it's defined in another file, you need to add a declaration for it or include a header that does.

Somefile.c

Code: Select all

float someGlobalVariable = 5.0f;
SomeOtherFile.c

Code: Select all

extern float someGlobalVariable;
// you can now use someGlobalVariable later in this file
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests