Orbit gravity around a point
-
- Posts: 47
- Joined: Sat Oct 23, 2010 2:25 pm
- Contact:
Orbit gravity around a point
How can I achieve this, I can't find any examples around. Thanks
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Orbit gravity around a point
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 47
- Joined: Sat Oct 23, 2010 2:25 pm
- Contact:
Re: Orbit gravity around a point
Ahhhh didn't find that until I downloaded chipmunk directly because there aren't any examples bundled with cocos2d. Thanks
-
- Posts: 47
- Joined: Sat Oct 23, 2010 2:25 pm
- Contact:
Re: Orbit gravity around a point
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
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Orbit gravity around a point
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:
(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:
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);
}
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 47
- Joined: Sat Oct 23, 2010 2:25 pm
- Contact:
Re: Orbit gravity around a point
So how could I have multiple planets? Repeat the process?
Done it thanks
Done it thanks
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Orbit gravity around a point
If you want more than one planet, you just need to add all the g vectors from all the planets together.Javawizkid wrote:So how could I have multiple planets? Repeat the process?
Done it thanks
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 47
- Joined: Sat Oct 23, 2010 2:25 pm
- Contact:
Re: Orbit gravity around a point
What does the -50000.0f in do? Shouldn't it be the equation of gravity?
Code: Select all
cpVect g = cpvmult(p, -50000.0f/cpvdot(p, p));
-
- Posts: 47
- Joined: Sat Oct 23, 2010 2:25 pm
- Contact:
Re: Orbit gravity around a point
Code: Select all
static void
planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: Orbit gravity around a point
If it's defined in another file, you need to add a declaration for it or include a header that does.
Somefile.c
SomeOtherFile.c
Somefile.c
Code: Select all
float someGlobalVariable = 5.0f;
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/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Who is online
Users browsing this forum: No registered users and 5 guests