XNA Problem with boids!

Started by
1 comment, last by Noggs 12 years, 12 months ago
Hey guys, Im making a boid flocking algo, and it is pretty much functional

You can see the basic of the flocking here (:


if (flock.proximity < boidDistance && boidDistance < flock.vision)
{
flock.acceleration = flock.position.Translation;
flock.acceleration -= flock[j].position.Translation;
flock.acceleration *= (boidDistance / -0.1f);
//If the acceleration is greater than the maxAcceleration allowed we normalize the vector in order to control the speed
if (flock.acceleration.Length() >= flock.maxAcceleration)
{
flock.acceleration.Normalize();
flock.acceleration *= flock.maxAcceleration;
}
//Adding the acceleration to the velocity.. well actually a Hyrda!, we multiply it!
flock.velocity *= Matrix.CreateTranslation(flock.acceleration);
}


and then I want them to attack some different targets, for directing them in the right direction I say


else if (!boundingSphere.Intersects(player.buildingList[1].boundingSphere))
{
behavior = Behavior.Flock;
vision = 50;
proximity = 15;
maxAcceleration = 0.008f;
acceleration = position.Translation;
acceleration -= Target;
acceleration *= (-0.0005f);
if (acceleration.Length() >= 0.003f)
{
acceleration.Normalize();
acceleration *= 0.003f;
}
velocity *= Matrix.CreateTranslation(acceleration);
}


Which is basically the same as before.. Now my real problem is when they reach their target their velocity is pretty high and they will pass fast through it and juggle back and forth like idiots :P..

What I want is them to go to the buildings boundingsphere/box and do the stupid-bee thing ^^ you know hit its head against the window repeately :) .. any suggestions? Its really bugging me >_<
Advertisement
bonk
Basically you want them to collide with the building. If you want them to act like bee's then think about what is really happening. A bee has a high forward velocity which updates it's position forward every frame. When it hits a window it will be reflected back - basically the velocity vector will have been reflected from the window. Then its wings will flutter like mad (applying a forward acceleration) and the bee will eventually stop moving away from the window and start going towards it again.

So if you detect a collision, reflect the velocity vector and you should get the behavior you want.

This topic is closed to new replies.

Advertisement