How to detect cover places at 3D scenes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robotex

I want to add covering for my AI Bots at 3D FPS. How to detect cover places in which bot will be invisible for player?

I want that bot detected that cover places (for standing, sitting or lying) depends on players position and FOV and then run to it and attack the player from it.

How can I do this in Godot?

:bust_in_silhouette: Reply From: Siandfrance

First of all, you need 2 points, one is the head of the player and one is the head of the bot (in the simplest case).
In the code, I will call thoses 2 points respectively player_pos and bot_pos, you also need the direction the enemy is looking (bot_rotation). All three are Vector3. The rotation will be in the same “format” as godot’s Spatial (doc) (YXZ-Euler angles)

Then, to have the FOV correct, you have differents solutions, those are two of them:

  1. Calculate a vertical and horisontal angles (code here). With this you can do the next step only if the angles are in certain ranges (and thus have full control over the FOV).
  2. Create a Area(3D) and do the next step only if the player is in this area (it allows for more complex vision shapes)

Once this step is complete, you need to do a ray-cast from the bot to the player using a RayCast node (doc) or the space_state interface (doc). If the raycast collides with a wall or a terrain element, the bot does not see the player, otherwise it sees him.

If you want a place for the player to hide in like bushes in Assasin’s Creed:

you can put an Area(3D) around the bushs that collides with the RayCast and thus stop it like a wall.

There is also a great video from GMTK explaining how it might work in stealth games which is similar to fps game detection: link.

I hope this helps.

If the raycast collides with a wall or a terrain element, the bot does not see the player, otherwise it sees him.

You didn’t understand. The task is hiding the bot from player, not player from bot.

I need to find positions where bot can hide from player’s shooting. If bot sees player, it will run to cover and start to attack player from it (moving from cover and back if needed). The biggest problem is changing cover position when player moves. For. i.e. bot can find cover, find the path to it and start running but player is also moving during bot’s moving. So, when bot arrives to the end of path the cover position was moved to the other position.

So, I can detect covers but I can’t guarantee that cover will be valid when bot arrives to it.

Robotex | 2020-07-16 14:24

Oups sorry.
A solution would be to take choose a cover that is reasonably far from the player, so wherever he moves it is still valid.
You could also hard code cover places depending on the player’s position (I think that this is the most used solution).

Siandfrance | 2020-07-16 15:15