# The swarm of insect: how to in opengl

I need the algorithm of doing such simulation. Where to find the reference for it. How to program the movement of particular insect and the swarm in the whole. What laws rules such things. I found on igeo but there is additional library that do not work on JOGL. I also want to know did the swarm of insect is different from swarm of points that is refered on IGEO web site. Should the insects to be with dimension but not points? I need the swarm of insects with diffferent colours that defines its velocity.

I was told such algorithm in the words: there is central insect and the swarm should comprise some kind of polygon (with the center of this insect) and other insects should not be out of this polygon. So I do not understand should be the central insect moves along the center of the polygon or it may be moving in the central area (small polygon) of the main swarm polygon (or maybe it could be circle)? But the main question is that the velocities of insects should be according to its colour: so it should be constant in modulus. But teacher told that I should use Random functio? How and for what? For the direction of velocities? Or the random fucntion should be applied when the certain insect tries to go out of boundary of swarm? Anyway the first task is the programming of main insect how its move then we can program the other insects with the restrictions relating to the sawrm boundary? Who can briefly refer to the right algoright with some kind of code?

So how to program the movement of certain insect. How to use “random” c++ function? For its position, for the velocity (but its direction as they shoud be constant by modulus).

How to use “random” c++ function? For its position, for the velocity (but its direction as they shoud be constant by modulus).

These aspects are quite independent of OpenGL and you will have a better chance of getting useful answers by asking in a general programming forum.

I don’t know how swarms are simulated, but I would recommend you try to ask a more specific question than what is essentially: “how do I simulate an insect swarm?” Such generic questions require somebody knowledgeable to write half a book chapter to answer, so that makes it unlikely to happen in practice. Please see also [thread=176139]Forum Posting Guidelines[/thread] for suggestions how to ask good questions.

http://stackoverflow.com/questions/24098901/swarm-of-inswects-with-different-velocities-according-to-its-colaur--here is my code. Can anyone to link the code there. I have additional questions about how to use the field of particle structure vec4 pos in array like manner particle[i].position[j]…except it there is colour field in integer value in particle structure. So how to use in for rgb colors. // it is particle system. But as for swarm I was said by teacher to have some central insect. So should it be in constant position in the centre of cube. Or this cube shoved move along this insect so it can not have collisions. Despite in this way its movement and the whole cube should be in one direction.

Can anybody to help to recode next code? As there is division according colours thta would define their velocities&

#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <iostream>
#include “windows.h”
#include <gl\glut.h>

/Ekran/
#define WIDTH 400
#define HEIGHT 400

/Zagalni/
#define GLOBAL 0.3
#define PERSONAL 0.3
#define INERT 0.3
#define DELAY 100

/Riy/
#define N 60 //NUmber of agents
#define K 200 //Number of moves

/* Obmezhennya */
#define Xmin -4
#define Xmax 6
#define Ymin -4
#define Ymax 6

double velocity[N];
double swarm[N];
double BestPers[N];
double BestGlob;

float Lrand(float min, float max)
{
return (min + ((rand() % 10000) / 1e4) * (max - min));
}

void init() //Generate initial positions and directions
{
srand(time(NULL));
glClearColor(1, 1, 1, 0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(Xmin, Xmax, Ymin, Ymax);
for(int i = 0; i < N; i++)
{
swarm[i] = Lrand(Xmin, Xmax);
swarm[i] = Lrand(Ymin, Ymax);
velocity[i] = Lrand(-1, 1);
velocity[i] = Lrand(-1, 1);
BestPers[i] = 100000;
}
BestGlob = 100000;
}

void MoveWasp() //Moves the whole wasp
{
for(int i = 0; i < N; i++)
{
for(int a = 0; a < 2; a++)
{
velocity[i][a] = INERT * velocity[i][a] +
Lrand(-1, 1) * GLOBAL * (BestGlob[a] - swarm[i][a]) +
Lrand(-1, 1) * PERSONAL * (BestPers[i][a] - swarm[i][a]);
swarm[i][a] = swarm[i][a] + velocity[i][a];
}
}
}

int chkBrd(int i) //Verifies agent’s position
{
if((Xmin <= swarm[i]) && (swarm[i] <= Xmax) && (Ymin <= swarm[i]) && (swarm[i] <= Ymax))
return 1;
else
return 0;
}

double calculate(int i)
{
double f;
double a = swarm[i], b = swarm[i];
f = - 0.1 * fabs(1 - b) - 0.1 * fabs(1 - a) - j0(20 * a * a + b * b);
// f = - 0.1 * fabs(1 - b) - 0.1 * fabs(1 - a) - j0(a * a + b * b);
// f = a * sin(4 * a) + 1.1 * b * sin(2 * b);
return f;
}

void checkBP(int i, double a)
{
if(a < BestPers[i])
{
BestPers[i] = a;
BestPers[i] = swarm[i];
BestPers[i] = swarm[i];
}
}

void checkBG(int i, double a)
{
if(a < BestGlob)
{
BestGlob = a;
BestGlob = swarm[i];
BestGlob = swarm[i];
}
}

void draw(void)
{
glColor3f(1, 1, 0);
glBegin(GL_POINTS);
for(int i = 0; i < N; i++)
glVertex2dv(swarm[i]);
glVertex2d(BestGlob, BestGlob);
glEnd();
glFlush();
glutSwapBuffers();
}

void display(int i)
{
std::cout << i << " " << BestGlob << " " << BestGlob << " " << BestGlob << "
";
}

void go()
{
double a;
for(int k = 0; k < K; k++)
{
for(int i = 0; i < N; i++)
{
if(chkBrd(i))
{
a = calculate(i);
checkBP(i, a);
checkBG(i, a);
}
}
display(k);
draw();
MoveWasp();
glClear(GL_COLOR_BUFFER_BIT);
Sleep(DELAY);
}
return;
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(600,220);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(“Рій Комах”);
glPointSize(2.0);
glEnable(GL_POINT_SMOOTH);
init();
glutDisplayFunc(go);
glutIdleFunc(go);
glutMainLoop();
}

Or can anybody complete and revise this code on particles system in some cube^2.0: also with different velocities according to the colour.

#include <GL/gl.h>
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h>
#include <math.h>
#define MAX_NUM_PARTICLES 100
typedef struct particle
{
int colorr;
int colorg;
int colorb;
float positionx;
float positiony;
float positionz;
float velocityx;
float velocityy;
float velocityz;
float mass;
} particle;

particle particles[MAX_NUM_PARTICLES];

int speed=2.0;
int num_particles=60;

void init () {
for(int i=0; i<num_particles; i++)
{
particles[i].mass = 1.0;
particles[i].positionx = 2.0*((float) rand()/RAND_MAX)-1.0;
particles[i].positiony = 2.0*((float) rand()/RAND_MAX)-1.0;
particles[i].positionz = 2.0*((float) rand()/RAND_MAX)-1.0;
if (i<num_particles/3) {
particles[i].velocityx = speed1.0((float)
rand()/RAND_MAX)-1.0;
particles[i].velocityy = speed1.0((float)
rand()/RAND_MAX)-1.0;
particles[i].velocityz = speed1.0((float)
rand()/RAND_MAX)-1.0;
particles[i].colorr =255;
particles[i].colorr =255;
particles[i].colorr =0;
}

else if ((num_particles/3=<i) && (i<2num_particles/3)) {
particles[i].velocityx = speed
2.0*((float)
rand()/RAND_MAX)-1.0;
particles[i].velocityy = speed2.0((float)
rand()/RAND_MAX)-1.0;
particles[i].velocityz = speed2.0((float)
rand()/RAND_MAX)-1.0;
particles[i].colorr =255;
particles[i].colorr =0;
particles[i].colorr =0;
}

else {
particles[i].velocityx = speed2.0((float)
rand()/RAND_MAX)-1.0;
particles[i].velocityy = speed2.0((float)
rand()/RAND_MAX)-1.0;
particles[i].velocityz = speed2.0((float)
rand()/RAND_MAX)-1.0;
particles[i].colorr =255;
particles[i].colorr =0;
particles[i].colorr =255;
};
}
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
for(int i=0; i<num_particles; i++)
{
point_colors[i+24]  = colors[particles[i].color];
points[i+24]  = particles[i].positionx;
}
glBindBuffer(GL_ARRAY_BUFFER, buffers);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), +sizeof(colors)NULL,
GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points),points);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors),colors);
glDrawArrays(GL_POINTS, 24, num_particles);
glutSwapBuffers();
}

float last_time, present_time;

void idle(void)
{
int i, j;
float dt;
present_time = glutGet(GLUT_ELAPSED_TIME); /* in milliseconds /
dt = 0.001
(present_time - last_time); /* in seconds /
for(i=0; i<num_particles; i++)
{
particles[i].positionx+=dt
particles[i].velocityx;
particles[i].velocityx+=dtforces(i,1)/particles[i].mass;
particles[i].positiony+=dt
particles[i].velocityy;
particles[i].velocityy+=dtforces(i,2)/particles[i].mass;
particles[i].positionz+=dt
particles[i].velocityz;
particles[i].velocityz+=dt*forces(i,3)/particles[i].mass;
}
collision(i);
}
last_time = present_time;
glutPostRedisplay();
}

float coef=1; /* coefficient of restitution /
void collision(int n)
{
if(particles[n].positionx>=1.0)
{
particles[n].velocityx = -coef
particles[n].velocityx;
particles[n].positionx = 1.0-coef*
(particles[n].positionx-1.0);
}
if(particles[n].positionx<=-1.0)
{
particles[n].velocityx = -coefparticles[n].velocityx;
particles[n].positionx= -1.0-coef

(particles[n].positionx+1.0);
}

if(particles[n].positiony>=1.0)
{
particles[n].velocityy = -coefparticles[n].velocityy;
particles[n].positiony = 1.0-coef

(particles[n].positiony-1.0);
}
if(particles[n].positiony<=-1.0)
{
particles[n].velocityy = -coefparticles[n].velocityy;
particles[n].positiony= -1.0-coef

(particles[n].positiony+1.0);
}

if(particles[n].positionz>=1.0)
{
particles[n].velocityz = -coefparticles[n].velocityz;
particles[n].positionz= 1.0-coef

(particles[n].positionz-1.0);
}
if(particles[n].positionz<=-1.0)
{
particles[n].velocityz = -coefparticles[n].velocityz;
particles[n].positionz= -1.0-coef

(particles[n].positionz+1.0);
}

}

bool gravity = TRUE;
float forces(int i, int j)
{
if(!gravity) return(0.0);
else if(j==1) return(-1.0);
else return(0.0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(600,220);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(“Рій Комах”);
glPointSize(2.0);
glEnable(GL_POINT_SMOOTH);
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
}