# gravity and magnetism as a function 2D

This topic is 3846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Not sure if this is an odd question, but I would like to allow users to define force functions in my 2D sim. The function would use the distance of a particle from a given point to apply a force at a certain level. A simple example would be f(x) = 1/x, or Force = 1/distance, where the greater the distance the less the force. What I'm wondering is if there are functions along these lines that come close to the real ones eg, gravity well and a magnet (polarity not considered). I know Im leaving out mass here, but was basically thinking of scaling the force by the inverse mass for things like magnetism and not for ones like gravity.

##### Share on other sites
If you want to do Newtonian gravitation, the simplest method is to add an acceleration to the body's current velocity:

a = GM/r^2
v' = v + F*a

Here r is the distance between the centres of the two bodies, and M is the mass of the gravitating body.

v is the velocity vector in 2D.

F is the unit vector in 2D pointing from the centre of the gravitated body to the centre of the gravitating body.

a is acceleration.

Since gravitation is two-way, this operation is applied to both the Sun and Mercury. However, it is seen that the tiny mass of Mercury does not accelerate the Sun an appreciable amount.

As for the electromagnetic field, Maxwell's equations allow for its modeling. Fluid simulation with Navier-Stokes comes next.

This means that vector calculus is required knowledge.

Wikipedia has a ton of information, and as far as I know, it's been accurate and full up to this point in time.

##### Share on other sites
I don't think telling him to learn vector calculus and go use the Maxwell equations is very a practical suggestion really...

to the OP: can you explain in a little more detail what you're trying to do here? You want to allow users to define forces, then you don't need to do anything right? Just hand them a book on vector calculus :) If you want to have some preprogrammed forces so that say you can put an object somewhere that creates a certain type of force field around it you could do this by looking up or working out the solutions to some different types of charge/current/mass/whatever distributions and then if you wanted to say create a gravitating sphere you could add an instance of CSphereicalGravField (or whatever) to your game world and then when you do your physics processing have it exert the appropriate forces on whatever objects you have in the world. Each say CPhysicalObject could have different parameters, properties that define how it would interact with the various types of CForceField objects.

So to have the SphericalGravField object apply a force on some CPhysicalObject SpaceShip you would just send SphericalGravField::GetForce( SpaceShip->position, SpaceShip->mass ) or something and have it return a force vector or something. I don't know if I'm being too clear but I can see how you might implement something like this, I think it could be kind of cool actually.

##### Share on other sites
Both gravitational and electromagnetic forces attenuate as an inverse-square, in vacuo. So unless you're working within a very dense medium or have some funky quantum effects to account for, a simple:

F = k/d2

will do the trick. Each force will have its own constant k, which will depend on your units. If you want to determine these constants to a realistic level, looks up Coulomb's law and Newton's universal law of gravitation, and their respective constants.

##### Share on other sites
also remember to apply the forces at a constant rate in your simulation because if you don't, as the frame-rate of your program varies, the accelerations will vary too.

##### Share on other sites
Quote:
 Original post by TheAdmiralBoth gravitational and electromagnetic forces attenuate as an inverse-square, in vacuo. So unless you're working within a very dense medium or have some funky quantum effects to account for, a simple:F = k/d2will do the trick. Each force will have its own constant k, which will depend on your units. If you want to determine these constants to a realistic level, looks up Coulomb's law and Newton's universal law of gravitation, and their respective constants.Admiral

Gravitational and electric forces for _point_ masses/charges at _rest_ have an inverse square form. For other geometries and charge/mass distributions the force field is not so trivial. And for a magnetism you can't even have a field if everything is at rest, and there isn't even a magnetic point charge anyway.

That's why I recommended coming up with a few that you want to model and then hard coding the correct force into different functions for each type of force field you want.

##### Share on other sites
Quote:
Original post by My_Mind_Is_Going
Quote:
 Original post by TheAdmiralBoth gravitational and electromagnetic forces attenuate as an inverse-square, in vacuo. So unless you're working within a very dense medium or have some funky quantum effects to account for, a simple:F = k/d2will do the trick. Each force will have its own constant k, which will depend on your units. If you want to determine these constants to a realistic level, looks up Coulomb's law and Newton's universal law of gravitation, and their respective constants.Admiral

Gravitational and electric forces for _point_ masses/charges at _rest_ have an inverse square form. For other geometries and charge/mass distributions the force field is not so trivial. And for a magnetism you can't even have a field if everything is at rest, and there isn't even a magnetic point charge anyway.

That's why I recommended coming up with a few that you want to model and then hard coding the correct force into different functions for each type of force field you want.

Hence... Maxwell's equations. Where movement is king.

##### Share on other sites
Yeah but it's easier to look up the form of say the electric field from uniformly charged rod and put that in your code than to say "here, div E = rho that's all you need." He doesn't need to be spending his time learning how to integrate charge distributions when he's trying to make a game. The Maxwell equations are great for derivations in text books, but extremely un-useful for getting stuff done directly with, and all sorts of common problems are solved already and their solutions are readily available so it doesn't make sense to reinvent them by starting from first principles.

##### Share on other sites

Apologies, I had posted this and wasn't expecting to get a response (thought it was a stupid question).

To elaborate a bit, if anyone's still listening, I have it set up so users can implement their own force classes, but, yes, I would like to have a few built in ones. My_Mind_Is_Going what you suggested is basically the same way I'm doing it, (right down to the getForce(...) method, and etc).

I guess my question is with the built in forces. Can I create a decent 'estimate' of the forces of say gravity and magnetism vis-a-vis a algebraic function (I say algebraic meaning "not too complicated")

It might be worth asking whether, in a physically based modeling sense, magentism and gravity would act that differently (assume particles of the same mass)? Imagine in a zero-g world, there's two large particles, one who has a gravity field, and the other that has a magnetism field. In what observable way would a small particle react differently to the two forces. Again, it just has to look realistic.

##### Share on other sites
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);

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

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

position += velocity;
velocity += acceleration;

Recompute acceleration for all objects that you want to create a force. In other words if you have three objects, o1, o2 and o3, you will do...

a = (G*mass(o2)) / distance(o2-o1) + (G*mass(o3)) / distance(o3-o1);

Just add all the accelerations together for a perfect result.

Magnetism should be similar.

##### Share on other sites
xerodsm, I'm not clear on what you're saying.

Quote:
 Gravitational force = (G * m1 * m2) / (distance)G = 6.67 x 10E-8m1 = object to get force form2 = object to get force fromso once you solve that then you'll have a forceNext we know f = maso 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?

##### Share on other sites
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;}