Distance Check within an angle/cone

Currently, I am attempting a raycasting technique with a line-circle interception (which is not working), and I was wondering if there is a way to limit a distance check within a cone in 2D. For reference, I am trying to make a sort of plane game, with radar detection, with my inspiration attached:

Twitter Post

If it is not possible, I can revert back to my raycasting, and I would ask if anybody had a line-circle intersection equation, of course taking into account the position of the circle, which I have not been able to find online. I can provide my project if needed.

Take a look at https://raytracing.github.io/books/RayTracingInOneWeekend.html chapters 5 and 6. A circle is just a flat sphere so the same code works for each.

1 Like

To get a “view cone” you can use the dot-product to calculate the angle between vectors.

// Notes: Vectors used in dot-product must be normalized to get the correct results
//        Calculations also work in higher dimensions
//        This example does not account for the radius of a circle/sphere
PVector center;

int DOT_COUNT = 500;
float HALF_VIEW_ANGLE = 15.0;
// our field of view is double since angle can be left or right
float threshold = cos( radians(HALF_VIEW_ANGLE) );

void setup()
{
  size(512, 512, P2D);
  center = new PVector(width/2, height/2);
}

void draw()
{
  background(0);
  PVector m = new PVector(mouseX, mouseY);
  PVector mdir = m.copy().sub(center).normalize();
  
  randomSeed(42); // use the same points
  
  for (int i=0; i<DOT_COUNT; i++)
  {
    PVector p = new PVector(random(width), random(height));
    PVector pdir = p.copy().sub(center).normalize();
    
    // Dot product
    // result is the cosine of angle between vectors...
    float ca = mdir.dot(pdir);
    
    // ...which matches our calculated threshold
    if (ca > threshold)
    {
      stroke(255,255,0);
    }
    else
    {
      stroke(0,0,255);
    }
    ellipse(p.x, p.y, 2, 2);
  }
  
}