Jump to content

Angular nightmare: field of view


Recommended Posts

Hi everyone


I hate math. I mean not in the broccoli sense but more like a visit to the dentist. It is required, so deal with it... Anyway, I can best describe my problem with an image:




The way I check whether an enemy is visible and falls within the field of view is as follows:

var maxViewDistance = 250;  var distBetw = getDistanceBetween(Player, Enemy);var fov = 60; if(distBetw > maxViewDistance) enemy.visible=false;else{ var playerAngle = player.angle; var angleBetween = angleBetween(Player, Enemy); var check = compare(playerAngle , angleBetween ) if(check > fov)  enemy.visible=false; else  enemy.visible=true;}

This works! However, first I actually have no idea why. I figured it out by console.logging the variables. Second, it sucks that I cannot figure out the vector way. I wanna bloody understand it :(


So returning to the image. Blue means known values, red unknown. I fail at getting the end vectors for the max view distance lines (question marks). I know that I have to do a scalar multiplication to alter the length of a vector. However, how do I account for the fov angles?


If I had those 3 points, I could check enemy vector against triangle (got that function working ironically). Plus I could also draw a fancy fov triangle as an overlay.



Link to comment
Share on other sites

Finding the coordinates of the question marks positions should be pretty easy...


q.x = camera.x + Math.cos(angle) * maxViewDist;

q.y = camera.y + Math.sin(angle) * maxViewDist;


You might need to flip the y sign if your y axis points down rather than up.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...