We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Help with Obstacle Avoidance and Boids

Poolcorp
Poolcorp asked
on
Medium Priority
717 Views
Last Modified: 2013-12-26
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

Comment
Watch Question

CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.