# Attracting forces

## Recommended Posts

fishleg003    122
Was hoping some one could clear this up for me im trying figure how i could displace a particle stream by using 3d points that would attract or repel the particles given there distance from the center. At the moment ive only got a gravity force which is just a constant -9.8 in the y im not entirely sure how you'd pull objects towards a single point... thx alot for any help fishy

##### Share on other sites
etothex    728
Simple, the direction of the force is the displacement vector from the source of the force to the particle being acted on, and has magnitude (for an inverse square force) proportional to 1/r^2 where r is the distance between the two.

So therefore, the acceleration of a particle in terms of the displacement vector v and a constant c is:

cv/(||v||^3)

##### Share on other sites
fishleg003    122
Im sorry you lost me is there a more err easier way of putting it.

How do you obtain the displacement vector ? Not totally sure i understand what c is.

My maths isnt exactly the best id love to know of a good book if anyone knows of one that explains the very basics of physics but doesnt forget to explain the jargon.

##### Share on other sites
Guest Anonymous Poster
displacment vector 'v' is the vector between the gravity source, and the object you want to move

'c' is an adjustable strength for the gravity

if you dont know what a vector is, wait a while for an experienced member to convert you to the 'church of vectors'...

##### Share on other sites
hmm, the solution suggested by the posters above will effectively put the particles in orbit around the points you will define.
Is this what you wanted?... Or being able to define a "stream curve" for all particles to follow, and be able to influence their trajectory with reference points you will assign in 3d space?

If you go for the latter, you'll need b-splines instead

##### Share on other sites
etothex    728
Quote:
 Original post by someusernamehmm, the solution suggested by the posters above will effectively put the particles in orbit around the points you will define.Is this what you wanted?... Or being able to define a "stream curve" for all particles to follow, and be able to influence their trajectory with reference points you will assign in 3d space?If you go for the latter, you'll need b-splines instead

No you don't. I assume this guy has some particle system where each particle has an acceleration added to its velocity every time interval - since he has a gravity acceleration which simply adds -9.8 to the velocity in the y-direction.

Deflecting a particle stream towards a point works exactly in that same fashion, except that the acceleration is an arbitrary constant c and not just -9.8. Plus, it's in the direction of the displacement vector, not just the y-direction. What makes the cool deflection pattern is that the force direction/magnitude changes as the particles get closer to the source of the force.

BTW, finding the displacement vector is easy. Just draw an arrow form one point to another. The vector's x-component is the length of the arrow in the x-direction and the y-component is the length in the y-direction. I gotta dash so someone else will have to explain vectors more...

##### Share on other sites
We're talking about different things.

You describe how one can simulate particles being attracted/repelled by "point-masses" positioned in space, using forces similar to those of the law of global attraction. (inv.square law)
What you speak of, is a physical simulation, whose final state may -in fact- become very difficult to control. (unless -of course- this is intended)

I was talking about explicitly moving particles on a known -predefined- trajectory, (the spline) which you can influence with as many control points as you want, just like with nurbs or something.
You know where all particles will start from, where they'll end up, how long that will take, which way they'll go, what their velocity/acceleration will be anytime...

Explicitly moving particles on a curve, and letting them move under the affect of gravitational acceleration is not the same thing. That's why to model something like what I was talking about, I suggested that one would probably need b-splines.

##### Share on other sites
fishleg003    122
Yeah he was right id like to have orbit points that i could shot around that would effect the particles slightly based on there distance from the center. The bspline thing would be really cool to have but at the moment i was just hoping to keep it very simple then eventually move onto something like bspline.

Quote:
 So therefore, the acceleration of a particle in terms of the displacement vector v and a constant c is:cv/(||v||^3)

With this maths notation its just simply,
c * v / ( len(v) to the power of 3 ) = new acceleration

Thx a tonne for the help so far hoping someone can clear a few things up just so i understand it better.

I normally calc velocity by doing,

Accel = Force/mass;

Vel = Vel + (Accel * dt);

So lets say i have my point C = { 0 2 0 } so my displacement vector would just be P = particle pos Dv = P - C.

But what i dont understand is how do i apply this new acceleration on top of whats already effecting the particles ? Would you simple just add it on top on the current acceleration ?

thx again
fishy

##### Share on other sites
The displacement vector v from point P1(x1,y1,z1) to P2(x2,y2,z2), is simply the coordinates of point P2 measured from P1 (as if P1 was the origin)
This gets down to: v = P2-P1 = (x2,y2,z2)-(x1,y1,z1) = (x2-x1, y2-y1, z2-z1)

You can do something like this:

1. Loop for all particles P(i),
2. TotalForce = 0;
3. Loop for all attractors A(j)
4. Calculate Displacement vector v from particle to attractor. v = A(j).Pos - P(i).Pos
5. TotalForce += (c/|v|2)*v
6. End loop (3)
7. P(i).Acceleration = TotalForce/P(i).Mass
8. P(i).Velocity += P(i).Acceleration*dt
9. P(i).Pos += P(i).Velocity*dt
10.End loop (1)

The magnitude of a vector is the square root of the sum of its squared components:
|v|=sqrt( x2+y2+z2 )
Since you need the squared magnitude above, you can lose the sqrt(), and implement this as the dot product of v with itsself.

##### Share on other sites
fishleg003    122
Cheers m8 i get it now :)

Ill give it ago later and tell ya how i get on.

##### Share on other sites
fishleg003    122
one more thing what if you wanted to add a fall off distance so that if the particle is beyond a distance it would ignore the force generated ?

##### Share on other sites
You could change step (5) into:
5. TotalForce += (|v| < A[j].MaxDistance)*(c/|v|2)*v

but I don't know whether certain compilers would issue warnings about this.
The one I use, doesn't.

I think that the best way to achieve this would be to add an extra "if()" statement between the steps (4) & (5) above...
Like:

if( |v| > A[j].MaxDistance )
continue;

##### Share on other sites
fishleg003    122
I gave it ago it appears to work great but i cant of got the formulea right because as you stated above,

TotalForce += (c/|v|2)*v

I tried it this way and it did appear to work but when the particle got really close to center and force was say 400 and dv (0.0 0.01 0.0) was a very small number id get (400 / (0.0^2 + 0.01^2 + 0.0^2)) * 400 which made the acceleration go balistic lol.

if i do sqrt(0.0^2 + 0.01^2 + 0.0^2) then it appear to work but what i did notice is that force doesnt seem to vary that much like say i place an attractor at 5 in the y and the particles spawn at the origion the force differs between -5 and 5 no values inbetween is this correct ?

Heres how i calculate the new force to be added to the other already existing forces which i recalc every update loop,

newforce.y = (A.force / Length(Dv)) * Dv.y; //for x y z Length = sqrt(x2 + y2 + z2)

totalforce += newforce / m;

Ive been playing around with it one thing that it looks extremely good for is an imploding explosion :D.

Thx
fishy

##### Share on other sites
Quote:
 Original post by fishleg003[...] which made the acceleration go balistic lol. [...]

Well, this was one of the things I had in mind, when I was talking about the simulation becoming difficult to control with these things...
Things get ugly too close to such attractors (Einstein says so :))

What you can do, is clip the force to a certain maximum. E.g.
TotalForce += min( (c/|v|2)*v, (some value that seems to work!) )
This value should also be multiplied with the particle's mass, if you want to allow for the same acceleration independently of how massive the particle is.

Quote:
 if i do sqrt(0.0^2 + 0.01^2 + 0.0^2) then it appear to work but what i did notice is that force doesnt seem to vary that much like say i place an attractor at 5 in the y and the particles spawn at the origion the force differs between -5 and 5 no values inbetween is this correct ?

How can this be? It's like the particle magically pops at places, without going there through a path from some other point.
Hmm, I see where this could come from... Are you using a maximum distance of effect==5 for that attractor? The problem with this, is that when a particle enters the effect area, the force isn't gradually applied starting from zero, but from any arbitrary value. (It could be +/-5 in your case)
I'll post a way to deal with this, a little later; I'm in a kind of a rush now :)

##### Share on other sites
1st. A mistake I've noticed.
In the formula TotalForce += (c/|v|2)*v, the vector v must be normalized! (it must be divided by its length) I missed that completely!
Also -now that I noticed- it has not been mentioned anywhere, that the "c" in the same formula, merely represents the mass of the specific particle, times some constant, which represents the mass which creates the gravity field.
I thought I'd mention that, just in case...

Now, as I said, the problem you were describing in your last post, should be caused by the fact that force isn't gradually applied -starting from zero- when a particle enters the affect area of an attractor. Instead, the initial value is the value you would get from the aforementioned formula, if you just plugged the correct distance. This may be troublesome...

You can use the following formula to fix that problem... What it does -basically- is map the range (0, +infinity) into the range (rmin, rmax), so that you can get the very same response/graph of force, within any arbitrary interval!

I strongly suggest that you keep rmin to zero! Else you'll get very weird behavior!

|f(r)| = c1*m/(c2 + (rmax-rmin)/(rmax-r) - 1)^2

Of course, this is only the proper magnitude of the force. To point it at the right direction, you should multiply it by the normalized vector of displacement from the particle to the attractor, just like the final "[...]*v" part in the previous formula. (Don't forget to normalize first)

m is the particles' mass, c1 should be the theoretical mass of the body that creates the gravity field, and c2 should be a value, such that "c1*m/c2^2" is the maximum force (at r==rmin). If you're not sure what it should be, use some very small -strictly positive- value, like 0.01...

Only calculate this formula for r in the range (rmin, rmax). Directly return zero instead.

I just want to remind you, though, that this is just a hack. I believe you'll get the best results using the simple formula you do, without any range checks or anything. Just clip the force to a certain maximum to prevent the particles going berserk.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account