Problem with steering in my Boids Algorithm
Hi All
I'm trying to program a Simple{very simple) Program in C that displays 20 triangles flocking using the Boids Algorithm. I've managed to implement the avoiding rule whereby all the boids avoid each other. However I'm having difficulty steering my boids effectivly. To begin with I just want all my Boids to rotate from their random starting positions and face the centre of the flock then move towards the centre.
All they do however is continually rotate on the spot or all face directly East(which happens to be the way they face when the angle is 0) Anyone have any ideas or know of simple source code I could use as an example.
Thanks
Below is my code
for (k=0; k<(MAX_PRIMS/*+ addboids*/); k++) {
boidcentre.x =+xnn[k];
boidcentre.y =+ynn[k];
}
centre.x =boidcentre.x/MAX_PRIMS;
centre.y =boidcentre.y/MAX_PRIMS;
for (i=0; i<(MAX_PRIMS/*+ addboids*/); i++) {
float x = centre.x - xnn;
float y = centre.y - ynn;
float desiredAngle = (float)atan2(y, x);
float currentAngle = angle;
float temptemp = desiredAngle - currentAngle;
hile (temptemp < -pi)
{
temptemp =+ pi*2;
}
while (temptemp > pi)
{
temptemp =- pi*2;
}
float difference = temptemp;
if (difference < -speed.x)
difference = -speed.x;
if (difference > speed.x)
difference = speed.x;
temptemp = currentAngle + difference;
while (temptemp < -pi)
{
temptemp =+ pi*2;
}
while (temptemp > pi)
{
temptemp =- pi*2;
}
angle =angle + temptemp;
temptemp = 0;
}
You are initializing boidcentre to zeros before you calculate your center/centroid of the flock ????
You need to if you arent
--------------------------------------------[size="1"]Ratings are Opinion, not Fact
Hi guys this is James.I totally for this forums.So guys please clarify this forums to me.
Thanks
James
Oregon Treatment Centers
Thanks
James
Oregon Treatment Centers
I wrote up my own boids program a while back and used a direction vector for each boid. It was the normalized summation of all influencing vectors multiplied by a speed scalar. The "influence" vectors each had an associated weight.
After you calculate the direction vector, rotate the boid model/sprite to align with the direction vector and translate it in in that direction per the time step.
If you want to be really clever, you could add a genetic algorithm that associates bit values to the weights of the influence vectors and get super boids.
Your influence vectors should include: avoidance, cohesion, predator avoidance, and alignment(direction of fellow flockmates)
My program was 3D but it whould translate the same to a 2D environment. Worked out quite well.
After you calculate the direction vector, rotate the boid model/sprite to align with the direction vector and translate it in in that direction per the time step.
If you want to be really clever, you could add a genetic algorithm that associates bit values to the weights of the influence vectors and get super boids.
Your influence vectors should include: avoidance, cohesion, predator avoidance, and alignment(direction of fellow flockmates)
My program was 3D but it whould translate the same to a 2D environment. Worked out quite well.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement