gravity and magnetism as a function 2D

Started by
10 comments, last by taby 16 years, 9 months ago
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.
Advertisement
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.
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.
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.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
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.
[size="2"]I like the Walrus best.
Quote:Original post by TheAdmiral
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.

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.
Quote:Original post by My_Mind_Is_Going
Quote:Original post by TheAdmiral
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.

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.
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.

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.
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.

This topic is closed to new replies.

Advertisement