I want to implement the simple, age-old boid algorithm in 2D but as compute shader in HLSL (host programm ist VVVV). Now my problem: In all 2D sample-applications I`ve found, boids are updated one at a time. This results in some jittering that causes the typical "random" results. Direction changes, etc.
I am not sure if my implementation is correct, but if I test each rule individually, my results look like other references. If I apply the rules together however (in pretty much any combination), very soon my boids enter a stable state where they form a fixed formation and just fly in some particular direction. Changing the view-radius influences the size and number of formations but doesn`t introduce anything "chaotic" or flock like, just static bunches after a couple of seconds.
Is there an implementation problem or is this just a property of parallel compute you have to compensate (somehow?
Relevant code:
void CS(uint3 tid : SV_DispatchThreadID){
if (tid.x >= elementCount)
return;
if(reset){
OutputBuffer[tid.x].x = random(float2(rand.x + 12,tid.x-4)); // PosXY
OutputBuffer[tid.x].y = random(float2(tid.x + 16,rand.y*6));
OutputBuffer[tid.x].z = random(float2(tid.x,tid.x * 2)) / 100; //VelXY
OutputBuffer[tid.x].w = random(float2(tid.x * 16, tid.x / 4))/ 100;
}else{
float maxSpeed = 0.01;
float2 myPos = OutputBuffer[tid.x].xy;
float2 myVel = OutputBuffer[tid.x].zw;
float2 myAcc = 0;
float2 steerAlign = 0;
float2 steerCohesion = 0;
float2 steerAvoid = 0;
int alignCount = 0;
int cohesionCount = 0;
int avoidCount = 0;
for(uint i = 0; i < elementCount; i++){
if(i != tid.x){
float2 iPos = OutputBuffer[i].xy;
float2 iVel = OutputBuffer[i].wz;
float dist = distance(iPos,myPos);
if(dist < range / 2){
steerAlign += iVel;
alignCount++;
}
if(dist < range * 3){
steerCohesion += iPos - myPos;
cohesionCount++;
}
if(dist < range){
float2 diff = myPos - iPos;
diff /= dist * dist;
steerAvoid += diff;
avoidCount++;
}
}
}
if(alignCount > 0 && steerAlign.x != 0){
steerAlign /= alignCount;
steerAlign = normalize(steerAlign) * maxForce;
}
if(cohesionCount > 0){
steerCohesion /= cohesionCount;
steerCohesion = normalize(steerCohesion) * maxForce;
}
if(avoidCount > 0){
steerAvoid /= avoidCount;
steerAvoid = normalize(steerAvoid) * maxForce;
}
if(myPos.x < -1){
myPos.x = 1;
}
if(myPos.x > 1){
myPos.x = -1;
}
if(myPos.y < -1){
myPos.y = 1;
}
if(myPos.y > 1){
myPos.y = -1;
}
myAcc = (steerAlign * alignFx) + (steerCohesion * cohesionFx) + (steerAvoid * seperationFx);
myAcc = clamp(myAcc, -maxForce, maxForce);
myVel += myAcc;
myVel = clamp(myVel,-maxSpeed,maxSpeed);
myPos += myVel;
OutputBuffer[tid.x].xy = myPos;
OutputBuffer[tid.x].zw = myVel;
}
}