Getting to grips with VERY basic gravity.

Hey I'm a fairly new to this so take it easy please.

I've written a very simple program to simple print the gravitational force between two objects but I am not getting a value returned (or i am getting an immeasurably small value).

#include <iostream>
#include <conio.h>
#include <math.h>


using namespace std;

const double GRAVITY = 0.000000000066742;
long int FG;
int m1=1;
int m2 =2;
int distance;

int main () {
int distance=4;
FG=GRAVITY*(m1*m2)/distance*distance;
cout << FG;
getch();
return 0;
}


Do I need to change the GRAVITY to something larger? Thanks if anybody has a minute to point me in the right direction,
Cheers
The usual gravitational constant is 9.81 m/s2.
Last edited on
Have I missed out any sort of motion/acceleration?
FG is an int. The fractional part of the result is truncated away in the assignment.
Make FG a double.

You need to add parentheses around the calculation of the squared distance.
Actually, come to think of it, I think 9.81 might be the Earth's gravitational constant, which wouldn't be used for the gravitational force between two objects.

Use this instead, the standard gravitational constant. The number is a little crazy, so just define the calc as the constant...

 
const double GRAVITY = 6.67428 * pow( 10.0, -11 );


You'll need to #include <cmath> for that to work.

This assumes your mass is measured in kg and your distance in metres. Your gravitational force will be measured in Newtons.
Last edited on
Just a thought ....

const double GRAVITY = 6.67428e-11;
Wow, I never actually knew you could declare doubles with the exponent like that.

Every day's a school day. :-)
Thanks guys.
Applied the suggestions.

Getting a force of 1.25143e-011

0.0000000000125143 Newtons?
I get 8.34285e-012.
Ah thanks, I had altered the mass of M2 to 3. Getting the same with mass as 2,

Thanks a lot!
Would like to take this a little further to learn a bit more. Any suggestions without jumping too far into the deep end.
You could try putting the calculation for the gravity force in a function.
Or asking the user for mass and distance?

Maybe giving an option to choose units of measurement for both mass, distance and gravitational force?
Had another play to tidy it up abit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <conio.h>
#include <math.h>

void calc (void);

using namespace std;

const double GRAVITY = 6.67428 * pow( 10.0, -11 );
double FG;
int m1= 1;
int m2= 1;
int distance;

int main () 
	{
	cout << "Please enter the mass of object one (KG)\n";
	cin >> m1;
	cout << "Please enter the mass of object two (KG)\n";
	cin >> m2;
    calc();
	}

void calc (void)
	{
    double FG;
	int distance=4;
	FG=GRAVITY*(m1*m2)/(distance*distance);
	cout << "The gravotinal force in netwons is  "<< FG;
	getch();
    }


I'm eager to make something I can visualize. If I draw an x,y axis how hard would it be to draw some dots to represent different objects and see the effects of them pulling together. I'm not sure where one would start.
You didn't like this way?

const double GRAVITY = 6.67428e-11;

This is better than calling functions to achieve the same result.
Last edited on
You best bet is to use some sort of multimedia library. Drawing this in a console app would be painful.

Take a look at SFML( http://www.sfml-dev.org/ ) or SDL ( http://www.libsdl.org/ ).
Last edited on
Topic archived. No new replies allowed.