Answer the question
In order to leave comments, you need to log in
How to build the field of view?
It is required to build the field of view of the player.
The player is a point with a radius of 10, with known coordinates. Also known are the coordinates where he looks at the canvas and his viewing angle. It is necessary to draw its field of view. The player is on the map, which is displayed as a picture and is represented in javascipt as an array of pixels (0 - no wall, 1 - yes). It is necessary that the field of view rests on the walls of the map.
Answer the question
In order to leave comments, you need to log in
School geometry.
1) First, calculate the angle where (X, Y) the player (x0, y0) is looking:
dx = X - x0; dy = Y = y0;
d = sqrt(dx*dx + dy*dy); - segment length
alpha = arccos(dx/d); - calculate the angle. Here it will be necessary to supplement, because the sign may be lost. Look at dx and dy.
2). Now we build the viewing angle (betaLeft, betaRight):
betaLeft = alpha - ugObz/2;
betaRight = alpha + ugObz/2;
3). Everything. Now we calculate the points through which the line of sight will pass:
xL = x0 + cos(betaLeft) * Radius;
yL = y0 + sin(betaLeft) * Radius;
xR = x0 + cos(betaRight) * Radius;
yR = y0 + sin(betaRight) * Radius;
And you build an endless line through these points so that it goes beyond the screen. You can choose Radius for this so that it is larger than the screen resolution
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question