I’m trying to create an AI that will study the territory on its own (not on the Waypoints).
For this I created the following scripts (scheme in the picture):
Character
Field of View
AI
Memory
Character is a base class with stats. It knows about all other scritps.
AI controll character.
Field of view is a 3d space view of character. It Raycast from character position with a radius of character view and with angle of character view angle. It knows about layers (obstacle, enemy etc). (code below)
Memory is a grid of nodes. Every node have a state (Unknown, Empty, Enemy, Obstacle etc). The idea is to fill the grid by character vision. Like if he see an obstacle - write in the nods under it state “obstacle”.
My problem: i dont know how to translate data from 3d space of Field of View to Memory.
In Field of View i know only points, where raycast hit thomething. How to feel nodes with Empty then?
When you raycast you get a 3d world position. You want to flatten that into a 2d grid - if you are looking down onto the game world then you can use the X and Z position of your raycast as the X and Y into your 2d memory grid.
You need to decide what resolution your grid would be. For example, if each grid block is 2 metres then you need to divide your X and Z raycast position by 2 to work out which grid entry it goes into.
Your memory grid would be a 2d array going from [0,0] to [maxX,maxY] but your 3d world could have a different range. You need to move the 3d world to fit into your 2d grid.
For example, if your world went from -50,-100 (XZ) to 50,100 (XZ) then the X size of the world would be 100 metres and the Z size would be 200 metres. If each memory grid square was 2 metres then you would need a grid 50x100. To find your memory grid position you would do this:
Take the 3d position (for example, -40,10,-70)
Subtract the minimum world position to it (which was -50,0,-100)
You now have 10,10,30
Take the X and Z (10,30) and divide by your grid size (2 metres)
You now have 5,15
Convert to integers to get your memory grid position (5,15)
Thanks)
I think about it.
I have some functions in memory class to translate coords from 3d space to 2d grid.
Also i have a class, that describe zone, where bots move (lets call it BattleZone). In it declared zone size, zone possitin in world etc. So 2d grid is tied to the terrain.
I dont know how to find all cells in grid which are inside character field of view.
I need to analize them and say (for example):
cell [5,2], [5,3], [5,4] - is wall
cell [4,2], [4,3], [4,4] - is empty
etc.
I can get a wall position by raycast hit. But how i can get an empty cell position and separate it from that cells, that not in field of view?
Maybe my level of English does not allow me to fully explain the problem.
But the point is that I’m trying to figure out how to transfer a 3d map into a 2d character memory grid. By putting there the zones that fall into the field of view.
For start we have a grid where all cell is undefined.
U U U U U U U
U U U U U U U
U U U U U U U
U U U U U U U
Character have field of view - 3f and angle 110*
So, where he respaun at bottom right corner of the map he must write to his memory zone, that he can see. And it will be something like:
U U U U U U U
E E U U U U U
E E E O O O O
E E E E E E C
I think about to make same field of view for 2d memory grid… but isnt too much?)
I use Undefind cells to create “Points of Interest”, so ai can decide where to go next and what zones it didnt see yet.