• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

# Magnetism formula...

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1MARS_999  GDNet+   -  Reputation: 819

Like
0Likes
Like

Posted 02 August 2012 - 05:58 PM

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

### #2Inferiarum  Members   -  Reputation: 623

Like
0Likes
Like

Posted 02 August 2012 - 06:04 PM

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.

### #3Cornstalks  Moderator*   -  Reputation: 5343

Like
0Likes
Like

Posted 02 August 2012 - 07:48 PM

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

[ 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 ]

### #4MARS_999  GDNet+   -  Reputation: 819

Like
0Likes
Like

Posted 02 August 2012 - 10:15 PM

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!

### #5Postie  Members   -  Reputation: 568

Like
0Likes
Like

Posted 03 August 2012 - 12:59 AM

Is the strength of the attraction due to a property of the object or affected by the distance between the object and the player?
Currently working on an open world survival RPG - For info check out my Development blog:

### #6MARS_999  GDNet+   -  Reputation: 819

Like
0Likes
Like

Posted 03 August 2012 - 06:30 AM

I would like it to be both....

### #7Mussi  Crossbones+   -  Reputation: 959

Like
0Likes
Like

Posted 03 August 2012 - 08:15 AM

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?

Edited by Mussi, 03 August 2012 - 08:15 AM.

### #8Bacterius  Crossbones+   -  Reputation: 3512

Like
0Likes
Like

Posted 03 August 2012 - 07:43 PM

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.

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

"The best comment is a deleted comment."

### #9MARS_999  GDNet+   -  Reputation: 819

Like
0Likes
Like

Posted 04 August 2012 - 12:13 AM

I will try this out soon and report back... Thanks

### #10jwezorek  Crossbones+   -  Reputation: 1328

Like
0Likes
Like

Posted 06 August 2012 - 11:38 AM

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.

Edited by jwezorek, 06 August 2012 - 11:39 AM.

### #11MARS_999  GDNet+   -  Reputation: 819

Like
0Likes
Like

Posted 06 August 2012 - 05:31 PM

Ok, I see these equations

1C = 1A*1s
1C= 1F*1V

so what do I do with the 1C when it's done? Not sure how to apply this to each object....

As of now I am guessing,
Obj1 = A = 10
Obj1 = s = 1

Obj2 = A = 20
Obj2 = s = 1

do I subtract the two 1C's? and when at zero they are attracted to each other? Guess I need a usage example.... if you could!

Thanks!

### #12jwezorek  Crossbones+   -  Reputation: 1328

Like
1Likes
Like

Posted 06 August 2012 - 07:36 PM

Ok, I see these equations

1C = 1A*1s
1C= 1F*1V

...

You're looking at the wrong equations because for some reason my link was broken. Search for "Coulomb's Law" on wikipedia. Let me try again, just posting the naked url right into this post

The Url is this:
http://en.wikipedia....ulomb&#39;s_law

Edited by jwezorek, 06 August 2012 - 07:36 PM.

### #13MARS_999  GDNet+   -  Reputation: 819

Like
0Likes
Like

Posted 06 August 2012 - 08:42 PM

so I am thinking something like this....

pseudo code....

```//change obj1charge or obj2charge + or - will attract or repel each other...
float obj1Charge = 1000.0f, obj2Charge = 200.0f;
float x = 1000.0f, y = 1000.0f;
float k = 299000.0f;
float f = k * ((obj1Charge * obj2Charge) / pow(float(x * y), 2));
for(int i = 0; i < 30; ++i)
{
x += x*f;
y += y*f;;
std::cout << f << ", " << x << " , " << y << std::endl;
}
```

that is what I came up with....

BTW obj1 and obj2 should be a vector correct for easy scalar * vector multiplication?

Edited by MARS_999, 06 August 2012 - 09:11 PM.

### #14Mussi  Crossbones+   -  Reputation: 959

Like
1Likes
Like

Posted 07 August 2012 - 08:19 AM

You need to divide by the squared distance between those objects, that's not what it looks like. You want objects to move towards or away from each other, the direction is the normalized vector from one objects position to the other. The magnitude of that vector can be some form of Coulomb's law, Newton's law of universal gravitation or anything else you like for that matter.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS