1. Attraction

Hi
Mark said:
>I have an object with a position {x,y} and velocity {x,y}

>i also have other objects with position {x,y} these other objects attract
or
>repell the first object, how do i caluclate a value to add to the first
>objects velocity so that it will be pushed directly away from a repelling
>object, or pulled toward an attracting object? The force should be less
>further away from the object.


I wrote a program last year called "linofo" that deals with attraction.
Unfortunately, it got lost in a disk crash a few months ago, so I don't have
the code anymore. I think I may have uploaded a demo to the list, so if
anyone happens to have a copy I'd sure like it if they could send one my
way.

Anyway - the way it worked was like this.

The formulae are:
1--f=m*a
2--s=1/2*a*t^2
3--s=v*t
4--f=G*(m1*m2)/r^2

(4) gives the force of attraction between 2 masses, where the gravitational
constant is G and the distance between the objects is r.

From (1) we get f=m*a -> a=f/m
Substituting that a in (2) we get s=1/2*(f/m)*t^2 --- (5)
since f=G*(m1*m2)/r^2 (from (4)) (5) becomes:
     s1=1/2*((G*(m1*m2)/r^2)/m1)*t^2 for the distance that m1 moves in time
t and
     s2=1/2*((G*(m1*m2)/r^2)/m2)*t^2 for the distance that m2 moves in time
t.

The only real mystery here is what to do with t? What is the nature of time
in a computer simulation? What I do is to divide the continuous stream of
time into discrete "ticks". A "tick" is a minumum unit of time. So, when we
move our objects, we want to know how far they move in a "tick". Movement
then becomes a series of steps, each step being the distance moved in a
tick. Since we are dealing with ticks, we can set t in the equations above
to be 1 which gives:
      s1=1/2*((G*(m1*m2)/r^2)/m1) for the distance that m1 moves in a tick
and
     s2=1/2*((G*(m1*m2)/r^2)/m2) for the distance that m2 moves in a tick.

So now we have a distance of movement. What is the direction? The direction
is the direction of the line joining the two points. So now we have a
magitude and a direction for our step. This makes the step into a vector.

If you have many masses, then the step of any one mass is just the vector
sum of the step caused by all of the other masses. This is done with a
nested loop:
ie
For each mass
    step = 0
    for each other mass
       step = step + step caused by mass and other mass
    end for
   move by step amount
end for

This method gets very slow if there are a lot of masses, because the
calculation time rises with the square of the number of masses. I'm not sure
how useful it would be in a game situation. But maybe we have a math whiz
among us who could make it run faster by eliminating the need for the nested
loop.

Bye
Martin

new topic     » topic index » view message » categorize

2. Re: Attraction

Thanks for those who suggested some ideas for my problem. I think I've got
something working now. What I'm doing is making a small action-puzzle game
to get in the mood to make my RPG. Hopefuly I'll have something playable
soon.

-Mark.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu