Answer the question
In order to leave comments, you need to log in
How to implement NOT x-ray vision of "enemies" in the game?
Good day.
I am writing a small game (for myself) in python. 2d game, RPG type (but with stealth elements), top view. Started describing "enemies," came to sight and was blown away, can't figure out how to make "enemies" not see through walls. They have a direction of view (vector) and an area in which they see (a triangle), but how to cut out the desired piece from a triangle.
Perhaps the approach with the area itself is not correct. Tell me please.
The wall has 2 center coordinates.
Answer the question
In order to leave comments, you need to log in
You don't need to cut anything.
Take all the key points in the field of view (enemies, friends, loot, something else) and check if there are obstacles on the line between them and the bot. Those. check if the line crosses any obstacle. If it crosses, then the bot does not see the point.
It seems to me that you need to learn how to cut one polygon out of another. First you have a triangle. We take the first wall, which is in the triangle. Let this be a segment. The segment closes a certain area for the enemy (it is clear which one, right?) This area must be subtracted from the triangle. Then we take the next wall inside the visible area and again subtract etc. from it.
Essentially, two steps are needed. Determine if a line segment and a polygon intersect, and learn how to subtract one polygon from another. Here, maybe this library will help: https://pypi.python.org/pypi/Shapely
In any case, I recommend looking for a ready-made solution.
Can look in the direction of the rays?
1. check the distance to the player, if in the visibility zone - continue.
2. cast a beam from the enemy to the player.
3. check if the beam is in the field of view (something like 0 >= abs(beam angle - enemy rotation angle) <= (enemy's viewing angle / 2)
4. check if the beam intersects with a segment (wall) (google algorithms)
And what figure do you describe the player?
You can take the point of view of the guard (head, eyes) and "shoot" with straight lines, which will be limited by the viewing angle. If you work in a polar coordinate system, then we move the origin of coordinates to the viewpoint, take one of the sides of the "view triangle" as the axis. Then we draw ten lines that come out of the origin and differ only in the angle of rotation. Moreover, all line angles will be less than the viewing angle.
Now everything is simple: we work with the geometry of intersections. The only difficulty is to line up the elements one after the other the way they go in the real world.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question