Jump to content
  • Advertisement
Sign in to follow this  
fguihen

creating forces such as attraction or repulsion

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!