1
\$\begingroup\$

I tried to implement from this site linear-algebra on how do I check the field of view using dot product. But when I check the angle to the given fov angle, I am not getting the desired result. Instead of getting the field of view of 45 degree, I am getting 90 degree. What am I doing wrong?

Vector2 start = Mapper.transform.get(owner).position;
Vector2 end = Mapper.transform.get(target).position;
float facingAngleRad = 0;
Vector2 ownerOrientation = 
   new Vector2((float)Math.cos(facingAngleRad), (float) Math.sin(ficingAngleRad));
Vector2 targetOffset = tmp1.set(end).sub(start);

float product = ownerOrientation.nor().dot(targetOffset.nor());
float checkAngle = (float) Math.acos(product);
float FIELD_OF_VIEW_ANGLE = 45 * MathUtils.degeeToRadians;
if(checkAngle < FIELD_OF_VIEW_ANGLE) {
    // in the field of view
}

In the below image, green is the desired field of view (45 degree), but I'm getting 90 degree (red)

enter image description here

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

If you want the field of view to be 45 degrees, then you need to check that the angle is less than 45/2. This is because you are checking if the angle is between the "center" and one of the outer bounds, and for a center angle of 0, the outer bounds are -22.5 and 22.5, not -45 and 45.

\$\endgroup\$
1
  • \$\begingroup\$ it works, I just divide the FIELD_OF_VIEW_ANGLE to half. \$\endgroup\$
    – ronscript
    Sep 24, 2016 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .