Attraction between objects

How would I make it so one thing is attracted to another? Not being attracted from far away, but very attracted up close. I can't really find anything on google for it.
Last edited on
Don't really understand what you're asking.

Edit: magnets?
Last edited on
Inverse of distance squared or something similar.
Ya I was thinking just get the distance between them, but I don't know how to make a small distance be a big attraction.
attraction = 1/distance
Smaller distances would have a greater attraction.
Oh I see what you're saying now, I misunderstood.

You could do something like this.

Let's say you have Object 1 and Object 2.
Object 1's initial coordinates (If we're doing 2d) (x,y) are 100,100
Object 2's initial coordinates are 200,100

The distance between the two at this point is sqrt( (100-100)^2 + (200-100)^2 ) which = sqrt ( 100^2 ) = 100 pixels apart.

We could check if the distance is below a certain threshhold before the magnetization(think I made that word up not sure) takes affect.

For this example, let's use a threshhold of 30. Let's say Object 1 has a MagnetizationLevel of 1, and Object 2 has a MagnetizationLevel of 2. (This means that Object 2 will be pulled towards object 1 at double the speed that Object 1 pulls object 2 if both threshholds are met. In this example, they both will have the same threshhold of 30 pixels.)

So we'd check if the distance is less than 30 between the two objects. If the distance is less than 30, then we will decide the MagnetizationEffect by taking our (MagnetizationLevel)(threshhold)/(distance+constant).

Keep in mind, we are adding a constant to the distance due to the fact that we want to avoid dividing by a number near 0 (preferably avoid dividing by less than 1) due to the exponential effect of the equation.

So for example. Here's some pseudocode.

I could have Object1's MagnetizationLevel be 1.
Object2's MagnetizationLevel be 2.
Object 1 Initial Location: 100,100
Object 2 Initial Location: 200,100

If distance between Object 1 & Object 2 is less than either of their threshholds (in this case, they both have a threshhold of 30 which is when magnetization effect begins) then
we first must figure out the direction each object will be moving.

For this example, we will say Object 1 has moved to X position 170 since 200-170=30 and 30 is our threshold in this example.

Object 1's X is less than Object 2's X (170<200), so we know that Object1's X must increase while Object2's X decreases.
Object 1's Y is equal to Object 2's Y, (100=100), so we know that the Y value will stay the same.

For the constant in this example, I choose to use 1, but that is up to you from trial and deciding what works best.

I will calculate Object 1's dx (change in x) by using the equation described above (MagnetizationLevel)(threshhold)/(distance+constant).
Therefore, Object 1's dx is at this location would be (1)*(30) / (30+1) 30=distance, constant=1
Object 1's magnitude for dx will be 30/31 at this location. We know that Object 1's X must be increasing since Object 1's X<Object 2's X, so we will leave this positive.

Now we do the same for object 2. (Position X=200)(Threshold=30)(MagnetizationLevel=2)(Distance=30)(Constant=1)

Object 2's dx magnitude will be (2)*(30) / (30+1) so object 2's dx magnitude will be 60/31 at this point. However, keep in mind that dx will need to be negative, since Object 2's X is greater than Object 1's X, so object 2's X must decrease to approach object 1's x.


Did this help or does this not make any sense?

Last edited on
Topic archived. No new replies allowed.