Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 670
  • Last Modified:

Help with Obstacle Avoidance and Boids

I am working on a Boids project for class and I am having some difficulty implementing the obstacle avoidance portion of the model and was hoping you guys could give me a hand. Below is my model code. Basically I want them to steer around the obstacles (Which are random circles on a 2d surface), What is the math or whatever I am missing, I can't seem to find a good source for this on the google.




private void Flock(List<Boid> boids)
        {
            //Obstacle Avoidance
            for (int i = 0; i < obstacles.Count; i++)
            {
                float obstacleDistance = Distance(Position, new PointF(obstacles[i].X, obstacles[i].Y));

                //if (obstacleDistance  < space)
                //{
                //    deltaX += Position.X - intCollection[i];
                //    deltaY += Position.Y - intCollection[i];
                //}

                // Align along with other nearby boids
                if (obstacleDistance  < sight)
                {

                    deltaX += obstacles[i].X * 0.5f;
                    deltaY += obstacles[i].Y * 0.5f;
                }
            }
            //Flocking
            foreach (Boid boid in boids)
            {
                float distance = Distance(Position, boid.Position);

                if (boid != this && !boid.Predator)
                {
                    // Keep distance between other boids
                    if (distance < space)
                    {
                        deltaX += Position.X - boid.Position.X;
                        deltaY += Position.Y - boid.Position.Y;
                    }

                    // Flock together with other boids to form cohesion.
                    else if (distance < sight)
                    {
                        deltaX += (boid.Position.X - Position.X) * 0.05f;
                        deltaY += (boid.Position.Y - Position.Y) * 0.05f;
                    }

                    // Align along with other nearby boids
                    if (distance < sight)
                    {
                        
                        deltaX += boid.deltaX * 0.5f;
                        deltaY += boid.deltaY * 0.5f;
                    }
                }

                // If a predator is within sight get away
                if (boid.Predator && distance < sight)
                {
                    deltaX += Position.X - boid.Position.X;
                    deltaY += Position.Y - boid.Position.Y;
                }

               
                
            }
        }

Open in new window

0
Poolcorp
Asked:
Poolcorp
2 Solutions
 
TommySzalapskiCommented:
Well, how do you want to aviod them? You could hug the wall and go around or make right angle turns to go around. The important thing is to choose which way to go I suppose.
You could, as soon as you hit the circle, look left and right. If one of those directions is also blocked, then the shorter way around is the other direction. If neither direction is blocked, then either way is as good (since you must have hit the circle head on).
0
 
satsumoSoftware DeveloperCommented:
I have a suggestion for another method, however I am waiting for a response to TommySzalapski's question before I spend the time explaining it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now