# creating forces such as attraction or repulsion

This topic is 5128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

any ideas on how to "create a force"? my example is in a crowd, each person needs their space, so when another person gets nearer , who is also trying to keep their own space, a repulsive force between the two is created. also, to have a charachter be " attracted" to a point that they strive to travell towards. its for flocking. i dont really know how to quantify a force

##### Share on other sites
You could probably use a single scalar. Positive for attraction, negative for repulsion and have the strength dissipate over distance. So each person might emit a -2 force, but a bench might emit a 20 force.

##### Share on other sites
Get yourself an introductory physics book. It'll spend a lot of time talking about this sort of thing.

The basic idea is to find the distance between two objects. From this you compute how much they attract or repulse each other. They objects may also have a strength inherent to them somehow (planets have mass, electrons have charge, etc, etc). Then you use that force to accellerate the objects away/toward each other.

##### Share on other sites
http://www.red3d.com/cwr/steer/gdc99/

##### Share on other sites
For things like crowds, you might also want to try putting together an imitation of fluid dynamics simulators. This is probably overkill, and I haven't implemented it myself (I've just talked with people who have), but anyway. Create a grid of cells that covers the area you want to simulate. Each cell has an acceleration vector that describes how it affects particles that are currently within the cell. Particles have a velocity that carries them through cells, and they impart some of their velocity to the cell that they're currently contained in (so lots of particles flowing in from the right side of a cell will give the cell an acceleration vector to the left) as well as the cells surrounding that cell. The "forces" of attraction and repulsion you were talking about are created by making the fluid uncompressable, so when a particle wants to flow into an already-crowded cell, it has to push all of those other particles out of the way. This slows it down.

This is almost certainly not useful (or at least, not practical) for what you're working on, but I hope you at least found it interesting. :)

##### Share on other sites
thanks but what im attempting is already way over my head, so your suggestion, although cool, is just in outer space to me. thanks anyway

##### Share on other sites
To create forces between objects, start with the position of the object. Look at the relative position of each other object to it by subtracting the co-ords. Use pythagoras to work out the range to the other object. Divide the lengths by distance to get a vector. Then just adjust the position coords according to the vector and distance as you desire.

Eg. If each object was x1,x2,x3 etc.

do

dx=x1-x2
dy=y1-y2
dist=sqrt(dx*dx+dy*dy)
vx=dx/dist
vy=dy/dist
x1+=vx*0.1*dist
y1+=vy*0.1*dist

Iterate that so every object looks at every other one and that should do soemthing interesting! Play around with the end 2 calculations. Have fun.

Mark
Cornutopia Games
http://www.cornutopia.net

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 11
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634150
• Total Posts
3015800
×