DES311 #4: Goal Seeking

This week I’ve made some improvements to the cohesion behaviour and implemented goal seeking.

Cohesion Improvements

Last week I spoke about how the cohesion behaviour wasn’t causing the swirling patterns I was looking for. I hypothesized this was because BOIDs were packed together too tightly and not able to jostle for central position. I attempted to improve this by having the separation behaviour use a collider to find nearby BOIDs rather than a single ray directly in front of it. This didn’t do the trick. Because separation looks for clear paths at increasing angles from the forward direction. This meant if a BOID entered the collider from the side the behaviour would try and find a clear path directly in front, which it would since the nearby BOID is at its side. This meant the separation was constantly maxing out the pilot module with a vector of straight ahead, meaning lower priority behaviours wouldn’t have any control.

I reverted the build back since this method seems like a dead end for now. Instead I made a change to the cohesion behaviour so it doesn’t take into account BOIDs that are too close:

                //objects too close don't get included, leads to natural separation
                if (Vector2.Distance(transform.position, nearbyBodies[i].transform.position) > targetSeperation)
                    sumX += nearbyBodies[i].transform.position.x;
                    sumY += nearbyBodies[i].transform.position.y;

With this emergent separation between BOIDs they now have room to jostle for central position, leading to the patterns I was looking for:

Goal Seeking

Next on my list of goals for this sprint was to add a goal seeking behaviour. This is the first behaviour that lets the user interact with the the flock.

How it works is when a static bool is read as true by the seeking behaviour in each BOID the position of the mouse (stored as a static vector) is used to calculate a new direction vector which is sent to the pilot module. The static bool is made true when the left mouse button is clicked. When the left mouse button is released a circle is instantiated at the mouse position and the BOIDs will move to that circle. The circle is destroyed when a BOID touches it.

When sprites are added this action can be made to look like the user is holding a piece of food above a fish tank, which they drop in when the mouse is released.

What’s Next

This concludes the first week of sprint two. For the second week I am aiming to implement predators and predator avoidance. These will be triangles who will move across the screen and the BOID flock will scatter to avoid.