Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a 2D game trying to get a "cloud" of a few hundred tiny cubes or discs to follow my mouse cursor without overlapping each other.

However, using the physics engine introduces performance sluggishness as the number of discs grows and the collision detection is generally wonky.

Is there an easy way to optimise the engine for this kind of thing? If not, would I be better off just switching the engine off and writing something of my own? (Clutching at straws here, but:) Are there any good resources or examples on how to do this?

share|improve this question
up vote 13 down vote accepted

Yes, using a physics engine for that would be a little overkill. I suggest you implement a flocking algorithm. The basics of which are:

  • Separation: Steer to avoid crowding local flockmates

  • Alignment: Steer towards the average heading of local flockmates

  • Cohesion: Steer to move toward the average position of local flockmates

You can see it in action here and get some details on its implementation. You'll do some minor tweaking to include applying a force towards the mouse pointer.

share|improve this answer
You cab use a virtual dynamic grid or other spatial partitioning to make flocking efficient if you have a lot of boids you can't process naively, too. Plenty of ways to optimize. – Sean Middleditch Feb 21 '13 at 18:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.