Welcome! Log In Create A New Profile

Advanced

Gravity in c++

Posted by g_man 
Gravity in c++
March 10, 2010 12:39AM
I know I have posted other topics like this, but after thinking, i just need to know how to do this.
I am making a homebrew game where I need to make an object feel the force of gravity from a planet.
Think of the object as a ball in space, and it is shot from a cannon. When it get close enough to a planet, It will be attracted to it by gravity. If it is going too slow, gravity will pull it in until it collides with the planet, and gets destroyed. I have already created a class to handle the x and y positions of the object, the speed and the angle it is currently at, as well as the accleration vector of the object. What I want to know is how to implement gravity. If there is, what formula should I use. Should I keep adding accereation vectors while the object is close enough to feel the pull of gravity, or should I just change the vector every timer the game loop runs.
Any help is apprecated.
Re: Gravity in c++
March 10, 2010 02:46AM
Did you look at Box2D: [wiibrew.org] ?
Re: Gravity in c++
March 10, 2010 03:56AM
I thought I read somewhere that box2d wasn't very good for the type of gravity i'm tring to implement. Also, I'm tring to use this as a learning experience. Thanks though
Re: Gravity in c++
March 10, 2010 04:32AM
Well it should not be all that hard to code from scratch...

If I remember correctly, the force of gravity (between to objects) is proportional to the product of their masses (multiplied by a constant) and inversly proportional to the square of the distance. Please, anyone, feel free to correct the formula if it is not correct, I am recalling this from highschool physics and that was a long time ago.

Fg = G(m1)(m2)/(d^2)

For the interaction between the ship and the planet, G(m1)(m2) will be the same unless your ship or planet is losing mass. So this basically boils down to:

Fg = k/(d^2)

Force of gravity is just an acceleration towards the body causing the gravitation using the shorted path possible (stright line).

An acceleration is just a changing speed.

So...

1. Calculate the (absolute) distance between your object and the planet
2. Set your maximum acceleration (i.e. the acceleration if you were directly at the center of the planet). Call this G.
3. Determine your objects actual acceleration towards the planet by dividing G by the distance (calced in 1) squared plus 1.

Fg (ship) = G / ((d^2)+1)

This means that if the object is directly at the planet core the distance would be 0 plus the 1 and thus you would be
dividing by 1 (meaning you get the full G). If you are further away from the core then your gravity drops off in proportion to
the distance squared.

4. Now apply the calculated acceleration to the object. Usually this is done by keeping a speed variable and then adding
to that speed variable every frame (or some other interval).

You can offset the distance between the object and the planet by the planet's radius in order to get the initial G at the surface
of the planet as opposed to at the core.

Theoretically you could work out the value of G needed but using real values but I think that is way to complicated and unnecessary. Play with the code to figure out a G that "looks nice" and then you can scale other objects accordingly. For example, if you have a planet that is the same size but half the mass then you would need to you half the G.
Re: Gravity in c++
March 11, 2010 12:03AM
Thanks, I don't have time to implement it right now, but I will try later.
Re: Gravity in c++
March 11, 2010 01:30AM
Quote
g_man
I thought I read somewhere that box2d wasn't very good for the type of gravity i'm tring to implement. Also, I'm tring to use this as a learning experience. Thanks though

Yes Box2D will not help you with this.
Re: Gravity in c++
March 11, 2010 10:39AM
Quote
scanff
Quote
g_man
I thought I read somewhere that box2d wasn't very good for the type of gravity i'm tring to implement. Also, I'm tring to use this as a learning experience. Thanks though

Yes Box2D will not help you with this.
Not entirely true. In the source repository there is Contributions/Enhancements/Controllers/b2GravityController which can be used to do what he wants.
The latest sources contain a lot more features then the released 2.0.1 version.
Re: Gravity in c++
March 11, 2010 05:42PM
Quote
Daid
Quote
scanff
Quote
g_man
I thought I read somewhere that box2d wasn't very good for the type of gravity i'm tring to implement. Also, I'm tring to use this as a learning experience. Thanks though

Yes Box2D will not help you with this.
Not entirely true. In the source repository there is Contributions/Enhancements/Controllers/b2GravityController which can be used to do what he wants.
The latest sources contain a lot more features then the released 2.0.1 version.

Interesting, thanks Daid. Most people on Box2D forums actually use the SVN, I guess the stable release is way out of date.

g_man, Box2D is really easy to build for Wii if you want to look at this solution.
Re: Gravity in c++
March 12, 2010 12:10AM
Thanks for the advice, but Again I'm tring to learn as much as possible from this project. I think I'll start using box2d for my flash development though.
Re: Gravity in c++
March 16, 2010 05:38AM
Just to make sure I'm right, I will have a loop and on every iteration of that loop I will find the distance and the angle to the center of the point of gravity with these formulas
AccelAng = atan((240-bullet1.getX())/(bullet1.getY()-320)); If center is 240,320
Accel = k/(calcDistance(bullet1.getX(),bullet1.getY(),320,240)*calcDistance(bullet1.getX(),bullet1.getY(),320,240));
To calculate next X and Y distances a formula like this can be used:
float speedX;
	float speedY;
	float accelX;
	float accelY;
	
	accelX = accel*cos(accelAng);
	accelY = accel*sin(accelAng);
	
	speedX = speed*cos(angle);
	speedY = speed*sin(angle);
	speedX+= accelX;
	speedY+= accelY;
	
	speed = sqrt(speedX*speedX+speedY*speedY);
	angle = asin(speedY/speed);
	
	if(speedX<0)
	angle=(M_PI/2+(M_PI/2-angle));
	
	X+=speed*(cos(angle));
	Y+=speed*(sin(angle+M_PI));
Tell me if you have any suggestions.
Sorry, only registered users may post in this forum.

Click here to login