Magnetism formula...

Started by
12 comments, last by Mussi 11 years, 8 months ago
I am looking for a formula that I can plug different parameters into to control how other objects react to a single object.

I want to be able to tweak the values to make the object either pull closer to the main object or push away based on some variables.

Anyone done this or have any info on it?

Would Bullet or Box2D have anything for this?

Thanks
Advertisement
If you consider point sources for the gravity, magnetic force etc. you would just apply a force on affected objects pointing towards/away from the source. The force should diminish quadratically with the distance to the source.
You were really vague, so this is a simple solution based on your minimal stated requirements...


bool attract = false;

Vector2f d = objectA.position() - objectB.position();
Vector2f force = d.normal() / d.magnitude();

if (!attract)
{
force = -force;
}

// ... now apply the force to whatever object...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
not sure if that will work for what I need. Hell I am not sure how to totally explain it.

Best I can say is this

I have the player as ObjA
and ObjB-ObjZ are enemies

Now I want ObjB-ObjZ to be randomly moving around the screen but I want to use the formula to always pull these enemies towards the player but they need to vary due to some variables which I figured something like a magnetic attraction some are stronger and weaker.... does this make more sense?

Thanks!
Is the strength of the attraction due to a property of the object or affected by the distance between the object and the player?
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
I would like it to be both....
So I'm assuming you don't really want to simulate actual magnetism, but want a formula for moving objects towards and away from each other based on some parameters.

I'm assuming you want the attractive force to be weaker with distance, so that means you'll want to divide by the distance. You can also divide by the squared distance to get more of a gravity effect. You'll probably want some constant factor for the strength of the forces, so multiply by that.

So now we have F = G / r^2, with F being the force, G the constant factor and r the distance between the objects. Adding in any other object property is as simple as multiplying/adding it with/to either the numerator or the denominator. If you're unsure if you should multiply/divide/add/subtract plot out some graphs or ask away.

Is that what you were looking for?
I'm assuming you want the attractive force to be weaker with distance, so that means you'll want to divide by the distance. You can also divide by the squared distance to get more of a gravity effect. You'll probably want some constant factor for the strength of the forces, so multiply by that.[/quote]
Remember that dividing by distance or distance squared is numerically unstable if the two objects are too close to each other, you want to introduce a minimal radius to prevent this (make the two objects collide with each other when they get too close, I guess).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

I will try this out soon and report back... Thanks
FYI, the actual equation I think you a re looking for is Coulomb's Law. It's like the gravity equation but for the attraction or repulsion of point charges. Basically, like the gravity equation but with polarity.

This topic is closed to new replies.

Advertisement