Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flocking boids behaviour problem

Yesterday I came across Craig Reynolds' Boids, and subsequently figured that I'd give implementing a simple 2D version in Java a go. I've put together a fairly basic setup based closely on Conrad Parker's notes.

However, I'm getting some rather bizarre (in my opinion) behaviour. Currently, my boids move reasonably quickly into a rough grid or lattice, and proceed to twitch on the spot. By that I mean they move around a little and rotate very frequently.

Currently, I have implemented:

  1. Alignment
  2. Cohesion
  3. Separation
  4. Velocity limiting

Initially, my boids are randomly distributed across the screen area (slightly different to Parker's method), and their velocities are all directed towards the centre of the screen area (note that randomly initialised velocities give the same result). Changing the velocity limit value only changes how quickly the boids move into this pattern, not formation of the pattern.

As I see it, this could be:

  1. A consequence of the parameters I'm using (right now my code is as described in Parker's pseudocode; I have not yet tried areas of influence defined by an angle and a radius as described by Reynolds.)
  2. Something I need to implement but am not aware of.
  3. Something I am doing wrong.

The expected behaviour would be something more along the lines of a two dimensional version of what happens in the applet on Reynolds' boids page, although right now I haven't implemented any way to keep the boids on screen.

Has anyone encountered this before? Any ideas about the cause and/or how to fix it? I can post a .gif of the behaviour in question if it helps.

like image 766
Iskar Jarak Avatar asked Jun 02 '11 02:06

Iskar Jarak


3 Answers

Perhaps your weighting for the separation rule is too strong, causing all the boids to move as far away from all neighboring boids as they can. There are various constants in my pseudocode which act as weights: /100 in rule 1 and /8 in rule 3 (and an implicit *1 in rule 2); these can be tweaked, which is often useful for modelling different behaviors such as closely-swarming insects or gliding birds.

Also the arbitrary |distance| < 100 in the separation rule should be modified to match the units of your simulation; this rule should only apply to boids within close proximity, basically to avoid collisions.

Have fun!

like image 183
Conrad Parker Avatar answered Sep 25 '22 12:09

Conrad Parker


If they see everyone, they will all try to move with average velocity. If they see only some there can be some separated groups.

And if they are randomly distributed, it will be close to zero.

If you limit them by rectangle and either repulse them from walls or teleport them to other side when they got close) and have too high separation, they will be pushed from walls (from walls itself or from other who just were teleported, who will then be pushed to other side (and push and be pushed again)).

So try tighter cohesion, limited sight, more space and distribute them clustered (pick random point and place multiple of them small random distance from there), not uniformly or normaly.

like image 23
Alpedar Avatar answered Sep 21 '22 12:09

Alpedar


I encountered this problem as well. I solved it by making sure that the method for updating each boid's velocity added the new velocity onto the old, instead of resetting it. Essentially, what's happening is this: The boids are trying to move away from each other but can't accelerate (because their velocities are being reset instead of increasing, as they should), thus the "twitching". Your method for updating velocities should look like

def set_velocity(self, dxdy):
    self.velocity = (self.velocity[0] + dxdy[0], self.velocity[1] + dxdy[1])

where velocity and dxdy are 2-tuples.

like image 38
CSCSH Avatar answered Sep 24 '22 12:09

CSCSH