gravity and magnetism as a function 2D

Started by
10 comments, last by taby 16 years, 10 months ago
xerodsm, I'm not clear on what you're saying.

Quote:Gravitational force = (G * m1 * m2) / (distance)
G = 6.67 x 10E-8
m1 = object to get force for
m2 = object to get force from

so once you solve that then you'll have a force

Next we know f = ma

so instead of that gravitational force, to get the acceleration you could just do...

a = (G * m2) / (distance);


1) m1 and m2 are the masses of the objects?

2) You're saying that instead of using the very first equation you posted, one should just use the simplified equation at the very end of the above quote to get acceleration? The mass of the moving object isn't important?

Quote:That will give you the magnitude of a, then just multiply the magnitude of a with the vector, m2.position - m1.position;


I'm not understanding what you're talking about here. you're saying to multiply the acceleration we calculated by the distance between the two objects? The equation you gave ended with dividing by the distance, so if we're multiplying by the distance now, then that doesn't make sense.

I think what you're saying is that the accel is applied to the X and Y coords of the position differently, but I don't understand the actual implementation, could you explain in more detail?


Quote:Now you have an acceleration. Then you just need to do:

position += velocity;
velocity += acceleration;


I guess what I'm not clear on is how to handle speed+direction. I can see conceptually that the new position would be the old position + velocity, but how do you actually store the "direction" information, so that the X and Y of the position are updated appropriately?
Advertisement
Using meters/second as velocity, and meters/second/second for acceleration, G = 6.67e-11.

The formula for acceleration is a = GM/r2, where M is the mass of the gravitating body.

Here is some sample C++ code to start you off with:
#include <iostream>using std::cout;using std::endl;#include <cmath>int main(void){	double G = 6.674e-11;	// Parameters of body 1.	double m1 = 4;	double pos_x1 = 234;	double pos_y1 = 122;	double vel_x1 = 0;	double vel_y1 = 0;	// Parameters of body 2.	double m2 = 6;	double pos_x2 = 100;	double pos_y2 = 122;	double vel_x2 = 0;	double vel_y2 = 0;	// Find the distance between the bodies (Pythagorean theorem).	double dist_x = fabs(pos_x1 - pos_x2);	double dist_y = fabs(pos_y1 - pos_y2);	double r = sqrt(dist_x*dist_x + dist_y*dist_y);	// Calculate the acceleration scalar.	double a1 = (G*m2)/(r*r);	double a2 = (G*m1)/(r*r);	// Find unit-length vectors pointing from one to the other.	double dir_x1 = (pos_x2 - pos_x1) / r;	double dir_y1 = (pos_y2 - pos_y1) / r;	double dir_x2 = (pos_x1 - pos_x2) / r;	double dir_y2 = (pos_y1 - pos_y2) / r;	// Translate first.	pos_x1 += vel_x1;	pos_y1 += vel_y1;	pos_x2 += vel_x2;	pos_y2 += vel_y2;	// Accelerate second.	vel_x1 += a1*dir_x1;	vel_y1 += a1*dir_y1;	vel_x2 += a2*dir_x2;	vel_y2 += a2*dir_y2;	return 0;}


This topic is closed to new replies.

Advertisement