I have a bunch of randomly generated boids flying around on my screen, but I want a function that can give them a field-of-view where they can "see" other boids or obstacles.

The code I wrote gets all the pixels around the boid in a circle of some size. Then I try to filter through them to find only the ones in front of the boid within a 30 degree cone-shaped field of view.

I am making this project in Unity using compute shaders written in HLSL.

For each boid, I gave a float2 position and a float angle. In my kernel, I run a function that tries to set the color of all the pixels inside my cone to red. Here's the code:

for(int x = -viewDistance; x <= viewDistance; x++){
    for(int y = -viewDistance; y <= viewDistance; y++){
        float2 sample = float2(x, y);
        float d = length(sample);

        if(d <= viewDistance && d > 0){
            // sensorAngle is an angle in radians defining the "view margins" of my cone.
            float minAngle = boid.angle - sensorAngle; 
            float maxAngle = boid.angle + sensorAngle;

            float theta = acos(sample.x / d);
            if(theta >= minAngle && theta <= maxAngle){
                float2 p = sample + boid.position;
                Result[int2(p.x, p.y)] = float4(1, 0, 0, 1);

As you can see I get all the pixels within a certain boundary and filter out ones that are too far away. This works perfectly in retrieving all pixels within a circle around my boid. My problem is with checking if the pixel is within my boid's view-cone.

I tried following this explanation for that code. This works perfectly, but it seems to "mirror" my cone over the x-axis like this:field of view cone mirror

Also, my boids get generated with a random number between 0 and 360 as their angle. I multiply this angle with PI / 180 to convert degrees to radians (This all happens on my C# side and then I send a ComputeBuffer through to my Compute Shader). However, I noticed that when the angle is generated larger than 180 degrees, the cone doesn't even render at all.

What might be a better method to go about calculating this FOV cone for each of my boids?

  • \$\begingroup\$ I'm absolutely terrible with trigonometry but I recall that there are different functions to get the interior angle vs the exterior angle - you might just be using the wrong function (I assume acos?) Alternately, a quick fix looks like it might just be to flip the greater than and less than in your comparison? \$\endgroup\$ – Appleguysnake Apr 1 at 18:29
  • \$\begingroup\$ If you haven't already seen it, Sebastian Lague has a very good video implementing boids, including a compute shader version complete with obstacle avoidance and everything. youtube.com/watch?v=bqtqltqcQhw The main takeaway I think is that you shouldn't be scanning every pixel in the view radius as that's a ton of work for a lot of empty space, compute shader or not. Instead, separate your boid logic from your visual rendering. \$\endgroup\$ – Appleguysnake Apr 1 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.