Jump to content
The Dark Mod Forums
MirceaKitsune

Idea for implementing AI shadow detection

Recommended Posts

I remember that a few years ago, someone pointed out a rather obvious limitation in the AI: A foe will only become alert if it sees the player directly, but not if it sees the shadow cast by the player. This means that if you're hiding behind a wall through which an enemy can't see you, however a light in front of you casts a shadow on the wall behind you which the foe would be able to see, that foe won't be alerted by your shadow. For the sake of having an intelligent and even more realistic AI, this always seemed a nice idea to consider.

It was mentioned that it's difficult to find a good implementation for this mechanic. Earlier today I was thinking about TDM and remembered this subject. I thought about how such a system could be implemented in a way that doesn't decrease performance, and at the same time is accurate enough to be considered usable. Although I don't code any TDM scripts, I wanted to contribute the solution I came up with; It's an estimation so it won't give the most accurate results, but should be cheap and provide just enough realism for this purpose. This is how it would work step by step:

  1. We begin by looping through all lights that cast shadows on the map.
  2. For each light, we scan whether there's any entity that is a player or AI within the radius / box of that light. If the light is touching a player or AI, we proceed with the following points for each one.
  3. If a valid entity was detected at point 2, we trace a line from the origin of the light toward the center of the player / AI bounding box.
  4. At this stage we do some boundary and collision checks: If the line reaches a solid surface before it reaches the player's center, that means a wall is likely in the way and we proceed no further. Also if the line passes the player's center but exceeds the radius of the light without hitting anything, the shadow is likely not going to reach the surface behind them so we stop.
  5. If the line does pass the player's center then touches a solid surface without existing the light's radius, we take note of the origin at which this line touched this surface. A solid surface should be considered any shader who's alpha >= 0.5, as shadows are typically not visible on materials like glass or water.
  6. Next we spawn a virtual box at the origin of this point, which will usually be a floor or wall or ceiling. The size of this box depends on the distance between the light, the player's center, and the point in which the box is spawned: The closer the player is to the light source the bigger it should be, and the further the player is from the traced point the bigger it should once more get... this is to better estimate how large the shadow should be thus how likely to attract attention.
  7. At this stage we have a problem: If the cube is passing through a solid wall, it will cause AI in other rooms to inexplicably become alert. So before proceeding, our invisible cube must be cropped (scaled and positioned) so that it aligns with the surface of all walls it touches from the direction in which the line was cast. This might be a little tricky for diagonal walls.
  8. Now the cube entity can act as a visual alert for AI: If an enemy is seeing it, it's as if they are seeing the player. The alert should have a watered down effect, so that the AI's alert level rises much slower compared to seeing a foe directly: The AI should be surprised at the shadow but not immediately figure out that it's a person who shouldn't be there.

That should be it from a technical standpoint! This method might fail to produce accurate results at times, namely if the path to the center of the player's box is obstructed by a solid although parts of the player are still visible to it... for a simple AI alert it should do however. It would be nice to see someone playing with this idea, maybe record a demo of how such a box projects to see if it's accurate enough. I attached a little schematic exemplifying how this would look like in 2D for easy visualization.

Trace.png.94b31f40424595053b65bb414ae1bbb5.png

  • Like 1
  • Haha 1

Share this post


Link to post
Share on other sites
Quote

I thought about how such a system could be implemented in a way that doesn't decrease performance,

 

Setting aside any questions about how difficult this would be to do (or whether the end result would even be desirable), how is it you think this system of additional traces, scans, and collision checks won't decrease performance?

Share this post


Link to post
Share on other sites

Note that there's an even better and more accurate way of doing this, though the alternative technique would be a bit more costly in terms of processing power: Instead of tracing just one line from the light source to the center of the player's collision box, we can divide the player's box and create a virtual volume of points at a given resolution (eg: 4 x 4 x 6) then trace a line from the light source to each of those points. Every line that hits a solid within the light's radius will spawn a single tiny cube at the point of impact, each of those entities acting as additional visual alerts to the AI for the player.

As the player is taller, a good default resolution would be 4 x 4 x 6 (slightly higher vertically). Still this means tracing 96 lines in total, for every AI / player located in every light, and doing it each frame; Depending on how cheap the trace procedure is, this could put quite a bit of load on the CPU. We could reduce the amount of useless points by instead creating a flat 2D grid of points in the center of the player's box which faces the light source... this would give a far more significant result (less pointless duplicates) with less overall points.

In fact we can do it even better! Just trace lines from the light source to specific bones on the player's armature. By default we can use the following body parts: L + R lower leg, L + R upper leg, L + R lower arm, L + R upper arm, pelvis, spine, torso, head. Only 12 traces and the shape of the model would also be represented as accurately as possible 😁

An additional benefit to using multiple traces is that each alert point can be adjusted based on how washed out the shadow is: If it's located in an area that's strongly lit, the shadow is harder to see thus the AI is less interested. And the more such triggers the AI is seeing at once, the faster their alert level will rise... this would make it so that smaller shadows are also of less interest, but if the AI is seeing a lot of shadowing at once it gets their attention even more.

Share this post


Link to post
Share on other sites
1 minute ago, Springheel said:

Setting aside any questions about how difficult this would be to do (or whether the end result would even be desirable), how is it you think this system of additional traces, scans, and collision checks won't decrease performance?

I don't know the exact performance cost of tracing a line from point A toward point B and checking if it hits a solid surface. Typically those operations are simple, a modern CPU should be able to handle them acceptably... but it depends on the projection algorithm used for surface detection.

If it proves too much we could add a delay to how often the projection is updated. 0.1 seconds would be ideal, though even 0.5 should be acceptable.

Share this post


Link to post
Share on other sites

Indirect player detection through AI seeing the player's shadow or the player obstructing a direct or indirect (lit surface) light source has been discussed before.

Such features where generally seen as too frustrating for the average player. There are probably only a few who would like the added realism even if it would come without visualization for the player.
In general, in-world visualizations (apart from the frob highlight) seems to be a taboo in the TDM community (looks like it instantly converts TDM to a modern Deus Ex cover shooter).
So hinting players about the possibility of AI seeing their shadow or that they cover a light source is a hard problem - if you want to have the slightest chance of the feature making it into the base mod.
And even if solved, it would probably still be a feature only enabled by  a minority of the TDM player base - wich itself is a minority of the stealth game player base already...

And as usual: Developer time is scarce. No member of the group that would like to have more indirect detection (wich includes me) has the skill and/or time to do it.

Share this post


Link to post
Share on other sites
2 minutes ago, Abusimplea said:

Indirect player detection through AI seeing the player's shadow or the player obstructing a direct or indirect (lit surface) light source has been discussed before.

Such features where generally seen as too frustrating for the average player. There are probably only a few who would like the added realism even if it would come without visualization for the player.
In general, in-world visualizations (apart from the frob highlight) seems to be a taboo in the TDM community (looks like it instantly converts TDM to a modern Deus Ex cover shooter).
So hinting players about the possibility of AI seeing their shadow or that they cover a light source is a hard problem - if you want to have the slightest chance of the feature making it into the base mod.
And even if solved, it would probably still be a feature only enabled by  a minority of the TDM player base - wich itself is a minority of the stealth game player base already...

And as usual: Developer time is scarce. No member of the group that would like to have more indirect detection (wich includes me) has the skill and/or time to do it.

I've seen discussion on the matter before. This time I wanted to provide an actual algorithm idea, since IIRC finding a good implementation approach was one problem.

It would make sense for this to be a separate option in the difficulty panel and disabled by default. Honestly even I might not use it if it proved too annoying! Still I think it would be a fun thing to have... even just to see how ingeniously it can be implemented, how well one could code accurate shadow detection with minimal performance impact.

Share this post


Link to post
Share on other sites

Everything that favors realism for realism's sake feels like a bad idea to me. It limits players (new, mostly unfair thing to manage), mappers (all lights should cast shadows, environment design has to support new gameplay feature at all times), and developers (AI's expensive already, new calculations should be as performance-efficient as possible). Gameplay is king, not realism. I don't need anything but imagination to predict that it will be a bad idea, gameplay-wise. But you can always implement and playtest it yourself, if you have to. Any proof of concept will be better than yet another "I have an idea, let's you people do it" thread, which seems to be a thing lately.

Edited by peter_spy
  • Like 2

Share this post


Link to post
Share on other sites

I remember this idea being discussed before and the conclusion (that i agree) is that it would cause unwanted frustration for the player. But i'm not saying that you shouldn't try to do it, if you know how that is, i'm sure some players will like the added difficulty. Make a mod and post it here, this is the beauty of totally open games like TDM, if you know how you can change it to your heart desire.  

Edited by HMart

Share this post


Link to post
Share on other sites
6 hours ago, HMart said:

I remember this idea being discussed before and the conclusion (that i agree) is that it would cause unwanted frustration for the player.

Very probably. Also it doesn't really fit with the cleverness of the guards! Most of the time you can open and close doors when they must know there shouldn't be anyone around and they don't care :). Granted, they sometimes comment on it, but overall they seem quite stupid to me...

Edited by wesp5
  • Like 1

Share this post


Link to post
Share on other sites

If I remember correctly apart from the possible frustration, it was not clear why the AI should recognise the player's shadow as hostile, while shadows of other guards would be viewed as friendly. If they (literally) jump at every shadow, it would increase the alert rating into infinity; if they don't, it would be unrealistic that they only react to the player's shadow, which would defeat the purpose of wanting to increase realism and rather only increases difficulty. Consequently, you would have to implement another feature, that lets the guard recognise a shadow (without really alerting it) and let it check who the shadow belongs to before increasing alert rating. This, in turn, increases the work you would have to put in drastically.

  • Like 3

Share this post


Link to post
Share on other sites

Isn’t there a menu toggle for playing with/without a player shadow? If playing with suddenly carries a penalty, why wouldn’t everyone turn it off?

There’s no point in implementing a feature if no one uses it, especially one as complex and performance impacting as this one.

  • Like 3

Share this post


Link to post
Share on other sites
47 minutes ago, grayman said:

Isn’t there a menu toggle for playing with/without a player shadow? If playing with suddenly carries a penalty, why wouldn’t everyone turn it off?

There’s no point in implementing a feature if no one uses it, especially one as complex and performance impacting as this one.

Not only that, the player shadow doesn't even work properly, due to the lack of animations.  So that would be another issue that would have to be dealt with.  It is an immense project that would require work in half a dozen different areas of the game, all for a result that might make the game less fun for 95% of players.

  • Like 3

Share this post


Link to post
Share on other sites
6 hours ago, wesp5 said:

Most of the time you can open and close doors when they must know there shouldn't be anyone around and they don't care :). Granted, they sometimes comment on it, but overall they seem quite stupid to me...

They actually do notice it and it matters a lot when increasing difficulty and AI awareness.

Edited by Anderson

"I really perceive that vanity about which most men merely prate — the vanity of the human or temporal life. I live continually in a reverie of the future. I have no faith in human perfectibility. I think that human exertion will have no appreciable effect upon humanity. Man is now only more active — not more happy — nor more wise, than he was 6000 years ago. The result will never vary — and to suppose that it will, is to suppose that the foregone man has lived in vain — that the foregone time is but the rudiment of the future — that the myriads who have perished have not been upon equal footing with ourselves — nor are we with our posterity. I cannot agree to lose sight of man the individual, in man the mass."...

- 2 July 1844 letter to James Russell Lowell from Edgar Allan Poe.

Share this post


Link to post
Share on other sites

Those are all good points I admit. I mainly thought of this from the perspective of "hey, I thought of an easy way that we could finally make this work... maybe it's something we could play around with, for the sake of making everything even more complete and realistic". When I work on a project I tend to want to implement cool things that might be useful, often times just to see that I could code them and get it working efficiently. Perhaps I got a bit carried away, though I'm still glad I shared this idea just in case it inspires something of use.

Share this post


Link to post
Share on other sites

Totally agree with Springheel, if the shadow doesn't "work" properly ( I have not played TDM for a while now) in that case this idea is really not viable, afaik there's no one working on animations for the character and it has been like this for years. 

Btw why don't you guys use mixamo?  Something in their licence prevents it? I'm asking because they give free animations and characters, I was able to download a bunch of animations right now in fbx format, totally free (you need to make a account but is free).  I've seen a animation pack there called "longbow locomotion pack" (and many others) that using the female archer character there, looks perfect for TDM.    

Edited by HMart

Share this post


Link to post
Share on other sites
57 minutes ago, MirceaKitsune said:

Those are all good points I admit. I mainly thought of this from the perspective of "hey, I thought of an easy way that we could finally make this work... maybe it's something we could play around with, for the sake of making everything even more complete and realistic". When I work on a project I tend to want to implement cool things that might be useful, often times just to see that I could code them and get it working efficiently. Perhaps I got a bit carried away, though I'm still glad I shared this idea just in case it inspires something of use.

Many many proposed ideas have made it into the game; it's a good reason to post anything that comes to any of us.

The nice thing about this forum is that ideas can get kicked around and tweaked (or outright set aside).

No such thing as a bad question or idea.

The tough part is finding someone these days who has the time to research and/or implement new stuff. (We have a ton of broken stuff that needs attention, and that list never seems to get any smaller, regardless of how many things get fixed.)

Share this post


Link to post
Share on other sites
7 hours ago, wesp5 said:

Most of the time you can open and close doors when they must know there shouldn't be anyone around and they don't care :). Granted, they sometimes comment on it, but overall they seem quite stupid to me...

 

1 hour ago, Anderson said:

They actually do notice it and it matters a lot when increasing difficulty and AI awareness.

That's up to the mapper, really. There's a shouldBeClosed 1 property that can be added to a door. If so, AI will notice that door is ajar, will investigate and then close it:

https://youtu.be/1xLXOlmAeHk?t=57

 

  • Like 1

Share this post


Link to post
Share on other sites
Quote

"hey, I thought of an easy way that we could finally make this work."

 

Most people tend to assume their ideas are easier to implement (by a lot) than they actually are.

  • Like 2

Share this post


Link to post
Share on other sites
3 hours ago, peter_spy said:

There's a shouldBeClosed 1 property that can be added to a door. If so, AI will notice that door is ajar, will investigate and then close it:

Even so, they never show an overall awareness that something weird is going on if they see doors being open that shouldn't be. In that regard I agree with Destined, how should they assume a shadow belongs to an intruder and not another guard?

Edited by wesp5
  • Like 1

Share this post


Link to post
Share on other sites
4 minutes ago, wesp5 said:

Even so, they never show an overall awareness that something weird is going on if they see doors being open that shouldn't be. In that regard I agree with Destined, how should they assume a shadow belongs to an intruder and not another guard?

I was actually thinking to suggest that as well; I always found it funny that if the player opens and closes a door, an AI looking at that door will never question "why does the door seem to be moving on its own"? But like others said, it needs to be thought out whether this would negatively impact the gameplay.

In this case though it doesn't necessarily need to trigger an alert: If all voice actors are still around, I'd rather just see a voice added instead! Something like the AI saying "is someone at the door" or "why is that moving on its own", similarly to how they complain about lights seemingly going out on their own 😄

Share this post


Link to post
Share on other sites

It's a lot of work (writing, recording, and setting up new dynamic bark for all current AI classes and voices) for one "cool, they thought of that!" player moment. A feature creep, in other words.

Share this post


Link to post
Share on other sites

Not to mention that if AI were to get suspicious by doors opening by default, players would never know whether it was safe to open a door or not, since they can't tell whether an AI can see it.  You'd be penalizing the player for something they have no control over, which is poor game design. 

 

AI do, in fact, turn to look at doors that open in their POV, even if it's not a suspicious door, so they don't completely ignore them.  I've always decided in my head that these old doors blow open or closed from time to time...same drafts that keep putting out the lights.

 

 

  • Like 3

Share this post


Link to post
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.

Guest
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.


×
×
  • Create New...