1 Replies - 996 Views - Last Post: 29 March 2013 - 04:18 PM Rate Topic: -----

#1 mxa1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-March 13

Flocking/Swarming algorithm help

Posted 25 March 2013 - 08:57 AM

I'm trying to implement the flocking algorithm in C++.
All the tutorials I've visited are full of dead links or old information so aren't helping :/

I've tried to implement it myself by making all the particles 'home-in' on the player. When 2 particles then collide within their larger bounding boxes they home-in to each other. And when the 2 particles are actually touching they repel each other until they are outside of their bounding boxes and find another particle to home-into.

In theory, I think I understand it but when I run my application the particles all home into the player and come to a stand still along the Y-axis above the player.

I do apologise for the horrendous code.
All the particles in question are stored in a Vector, with a pos and velocity.

for(int i = 0; i < swarm.size(); i++)
		{
			for (int j = 0; j < swarm.size(); j++)
			{
				if (swarm.at(i)->getParticleModel()->getPosition().x < gameObjects.front()->getParticleModel()->getPosition().x)
				{
					if (swarm.at(i)->getParticleModel()->getTouching() == false)
					{
						if (swarm.at(i)->getParticleModel()->getVelocity().x < 0)
							swarm.at(i)->getParticleModel()->setVelocityX(swarm.at(i)->getParticleModel()->getVelocity().x *-1);

						swarm.at(i)->getParticleModel()->moveConstVel(timestep);
					}

					if (CollisionCheck(swarm.at(i)->getParticleModel()->getPosition().x-30, swarm.at(i)->getParticleModel()->getPosition().y-30, 65, 65,
                                      swarm.at(j)->getParticleModel()->getPosition().x-30, swarm.at(j)->getParticleModel()->getPosition().y-30, 65, 65))
					{
						if (swarm.at(i)->getParticleModel()->getFarAway() == true)
						{
							swarm.at(i)->getParticleModel()->setClose(true);
							swarm.at(i)->getParticleModel()->setFarAway(false);
						}
					}

					if (swarm.at(i)->getParticleModel()->getClose() == true)
					{
						if (swarm.at(i)->getParticleModel()->getPosition().y < swarm.at(j)->getParticleModel()->getPosition().y)
						{
							swarm.at(i)->getParticleModel()->setPosition(swarm.at(i)->getParticleModel()->getPosition().x,swarm.at(i)->getParticleModel()->getPosition().y+10);

						}

						if (swarm.at(i)->getParticleModel()->getPosition().y > swarm.at(j)->getParticleModel()->getPosition().y)
						{
							swarm.at(i)->getParticleModel()->setPosition(swarm.at(i)->getParticleModel()->getPosition().x,swarm.at(i)->getParticleModel()->getPosition().y-10);
						}

						if (CollisionCheck(swarm.at(i)->getParticleModel()->getPosition().x, swarm.at(i)->getParticleModel()->getPosition().y, 5, 5,
                                      swarm.at(j)->getParticleModel()->getPosition().x, swarm.at(j)->getParticleModel()->getPosition().y, 5, 5))
						{
							swarm.at(i)->getParticleModel()->setTouching(true);
							swarm.at(i)->getParticleModel()->setClose(false);
						}
					}
					if (swarm.at(i)->getParticleModel()->getTouching() == true)
					{
						swarm.at(i)->getParticleModel()->setVelocityX(swarm.at(i)->getParticleModel()->getVelocity().x *-1);

						if (!CollisionCheck(swarm.at(i)->getParticleModel()->getPosition().x, swarm.at(i)->getParticleModel()->getPosition().y, 5, 5,
                                      swarm.at(j)->getParticleModel()->getPosition().x, swarm.at(j)->getParticleModel()->getPosition().y, 5, 5))
						{
							swarm.at(i)->getParticleModel()->setFarAway(true);
							swarm.at(i)->getParticleModel()->setTouching(false);
						}
					}
				}

					
				if (swarm.at(i)->getParticleModel()->getPosition().x > gameObjects.front()->getParticleModel()->getPosition().x)
				{
					if (swarm.at(i)->getParticleModel()->getTouching() == false)
					{
						if (swarm.at(i)->getParticleModel()->getVelocity().x > 0)
						swarm.at(i)->getParticleModel()->setVelocityX(swarm.at(i)->getParticleModel()->getVelocity().x *-1);

					swarm.at(i)->getParticleModel()->moveConstVel(timestep);

					}

					if (CollisionCheck(swarm.at(i)->getParticleModel()->getPosition().x-30, swarm.at(i)->getParticleModel()->getPosition().y-30, 65, 65,
                                      swarm.at(j)->getParticleModel()->getPosition().x-30, swarm.at(j)->getParticleModel()->getPosition().y-30, 65, 65))
					{
						if (swarm.at(i)->getParticleModel()->getFarAway() == true)
						{
							swarm.at(i)->getParticleModel()->setClose(true);
							swarm.at(i)->getParticleModel()->setFarAway(false);
						}
					}

					if (swarm.at(i)->getParticleModel()->getClose() == true)
					{
						if (swarm.at(i)->getParticleModel()->getPosition().y < swarm.at(j)->getParticleModel()->getPosition().y)
						{
							swarm.at(i)->getParticleModel()->setPosition(swarm.at(i)->getParticleModel()->getPosition().x,swarm.at(i)->getParticleModel()->getPosition().y+1);
						}
						if (swarm.at(i)->getParticleModel()->getPosition().y > swarm.at(j)->getParticleModel()->getPosition().y)
						{
							swarm.at(i)->getParticleModel()->setPosition(swarm.at(i)->getParticleModel()->getPosition().x,swarm.at(i)->getParticleModel()->getPosition().y-1);
						}

						if (CollisionCheck(swarm.at(i)->getParticleModel()->getPosition().x, swarm.at(i)->getParticleModel()->getPosition().y, 5, 5,
                                      swarm.at(j)->getParticleModel()->getPosition().x, swarm.at(j)->getParticleModel()->getPosition().y, 5, 5))
						{
							swarm.at(i)->getParticleModel()->setTouching(true);
							swarm.at(i)->getParticleModel()->setClose(false);
						}
					}
					if (swarm.at(i)->getParticleModel()->getTouching() == true)
					{
						swarm.at(i)->getParticleModel()->setVelocityX(swarm.at(i)->getParticleModel()->getVelocity().x *-1);

						if (!CollisionCheck(swarm.at(i)->getParticleModel()->getPosition().x, swarm.at(i)->getParticleModel()->getPosition().y, 5, 5,
                                      swarm.at(j)->getParticleModel()->getPosition().x, swarm.at(j)->getParticleModel()->getPosition().y, 5, 5))
						{
							swarm.at(i)->getParticleModel()->setFarAway(true);
							swarm.at(i)->getParticleModel()->setTouching(false);
						}
					}


					
				
				}
			}



I also asked this question on cplusplus.com but they haven't replied yet :)

Thank you for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Flocking/Swarming algorithm help

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Flocking/Swarming algorithm help

Posted 29 March 2013 - 04:18 PM

Flocking and swarming, as I recall, are two slightly different algorithms. Flocking is when a number of agents converge along a given path, whereas swarming is converging on a specific target. You need to be clear which one you mean.

With problems involving multiple agents you need to normally reduce their number to just one or two so that the specifics of the problem you are having becomes more obvious.

Let me know how it goes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1