Stealth game visibility check system

If you’re making a stealth game, where the aim is to sneak around without being seen, it might be a good idea to use rays to check if a player or enemy is visible.

Even better would be to use ray casting checks against each bone of the player’s skeleton.


This system (which is in development for my own game) uses a set of rays cast to key bones: The hands, the feet, the pelvis and the head and checks if they are visible from the point of origin. It then gives a visibility rating as a percentage (own[‘total’])

If your visibility rating is over a certain percentage for a certain amount of time, you should get spotted.

This is a very accurate system, good for an in depth sneaking mission. Much better than just casting a ray to the character and getting True/False visibility.

As usual, it requires a little python knowledge to integrate it in to your own games, but not as much as usual. :slight_smile:

Here’s the demo with script attached:
stealth_visibility.blend (733 KB)

You can change the pulse interval of the always sensor if you want to reduce the drain on logic. Also don’t forget to comment out the draw line calls, that’s just for illustration. :slight_smile:

1 Like

That looks awesome! :3 Probably gonna use it somehow, sometime, somewhere.

Thanks for sharing!

Don’t want to clutter the other thread.

How would you deal with textures with transparency?
Grass, bushes, smoke…
Maybe check UV hitposition?
From api: " if poly is 2, returns a 5-tuple with in addition a 2D vector with the UV mapping of the hit point as 5th element."

There’s no point in seeking so much information since the rays don’t cover the whole body but just the medians of bones.
You might want to add simple ray casting from ray hit position if the hit object has a transparent property or something.

Ontopic: great idea, though I had another idea some time ago about sphere projection and approximating all the scene to spheres to check for occlusion and an accurate percentage of visibility, unfortunately python’s speed couldn’t really keep up with the projection of a bunch of spheres.

I just can’t imagine a scenario in which the pelvis is the only visible bone…

Pretty cool, and definitely useful.

You could do a double check if you wanted, so that if it’s blocked by [‘partial_blocking’] it adds 0.5 to “visibility” (should have made that “invisibility” for clarity of code). But it’d double the load.

I think if you’ve got a whole load of enemies and you use this method unfiltered, then it could slow down a bit.
It’s best to add this as a final check once you’ve checked for distance, visibility arc and other things. You could also add a general “darkness” modifier to the end result to approximate hiding in shadows. That’d require some kind of light map of your level though. Another modifier might be speed of movement. The eye is drawn to fast moving things…

The pelvis is included to weight the average calculation towards the center of body mass. :slight_smile:

Once I did a visibility test with video texture and a duplicate scene (black scene, red character),
but I thought that this approach would be more sane than that.

Thanks for the insight.

I recall doing something in the past which would modify the scene (translate objects etc), updating a render to texture and resetting them all in 1 frame which worked nicely. With an object color set up, you can generate a render to texture with everything black except the desired object white (or any other color combination) in a single scene.

Edit: similar technique used Here, where it hides refractive objects before updating render to texture and then setting them back visible.

@Smoking_mirror
Looks good. Pure genius.
Would you be able to use Enemy Detection Bars (EDB)? I’m thinking if one ray sees a bone, an EDB HUD is partially displayed, if two rays detect bones, then more of the EDB is shown, and so on. Like “Far Cry3 & 4”. “Dishonored” etc.
If all the rays sees the player, then the EDB turns red and the battle is on. Something like that?

Well, this is set up to give an overall level of visibility of the player. So it doesn’t focus on individual enemies, but gives an overall percentage of how visible the player is, assuming that if one enemy can see the player they will raise the alarm. You could easily hook up a bar to show that in the HUD.

Another way of doing it would be to calculate for each enemy individually, and attach a percentage to each. When an enemy can see the player they would change to a different AI state, either attacking or running to trigger the nearest alarm.

Well, this is set up to give an overall level of visibility of the player. So it doesn’t focus on individual enemies,

In “Far Cry”, the Enemy Detection Bar points to the enemy, I don’t need to worry about that yet, though. Baby steps.

I’m thinking. . . where the enemy, might catch a glimpse of the Player, and might get suspicious but not attack. Maybe walk over to where the player is to have a look. If he don’t see the player, walk back to his post, If the enemy actually sees the player then attack, sounds an alarm, shoots, or whatever.

I always wondered how they did that in games. You gave me an idea of how it might work.

Hmmm, I think I’ll try to build something similar to yours. I want to see if I can recreate the Enemy Detection Bars like the ones in AAA games based on your idea.

Thanks. :slight_smile: