#### Archived

This topic is now archived and is closed to further replies.

# Realistic Enemy Vision Help...

This topic is 5211 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i am currently working on a 2d rpg game, and i am thinking about creating some enemy ai, where the hero has the option to kill anyone they want to in a town, but if a guard is within the line of sight of seeing the hero killing someone, that he will attack the hero. For example look at my diagram H represents the hero, V represents the victim, G represents the guard, and L represents the guards line of sight. V H LLLLLLL LLLLLLL LLLLLLL LLLGLLL LLLLLLL LLLLLLL LLLLLLL As you can see in the above example guard wouldnt see anything, but how would i go about programming the area of the guards line of sight? Any help is appreciated. [edited by - MasterDario on June 6, 2004 12:00:06 PM]

##### Share on other sites
If you save the coordinates of every object in the map you could use them. Have a rectangle and if anything is inside it then they just can see it.
x
123456789
LLLLLLLLL1 y
LLLLLLLLL2
LLLLLLLLL3
LLLLLLLLL4
LLLLGLLLL5
LLLLLLLLL6
LLLLLLLLL7
LLLLLLLLL8
LLLLLLLLL9

Imagine this... so if either V or H come into any L in the map... if ((x >= 1 && y >=1) && (x <=9 && y <=9)) then the guard can see their status.

##### Share on other sites

If the guard can see things a distance X away from it, then, if there is a guard within a distance X of the Hero, then he can possibly be seen while performing actions (like killing Victims).

So, the first thing to do is find a list of all guards within a distance X of the Hero (you''ll need to do a more detailed check if guards have different viewing distances... but let''s assume for the time being that all guards can see the same distance under perfect conditions).

Once you''ve worked out which guards are close enough to the Hero to possibly see her, then you need to check as to whether their line of sight (LOS) is blocked by intervening objects, walls, etc. You can do this by casting a ray (straight line) from either the Hero to the guard, or the guard to the Hero. If the ray intersects a game object, then you can say that the guard could not see the hero.

A more detailed check would consider the size of intervening objects... and an even more detailed check would consider the volume of space filled by the Victim + Hero and whether a sufficient part of it could be seen to infer that the Hero killed the Victim.

Make sense?

Cheers,

Timkin

##### Share on other sites
You could create a invisible line between the gaurd and the player, and then see if that line collided with any object. You''d probably want to do some research into parametric lines if you went that route.

Alternativly, you could use your pathfinding algorithm to figure out if the guard can see the player.

* First find the path to the player.

* Now, the guard can see the player if
1) He had to take steps in only 1 direction (i.e. all north, south). This on is pretty simple, if the gaurd could walk in a stright line to the player, then the guard could obviously see the player.

2) He had to take steps in only 2 directions(i.e. north, east). This one''s a bit harder, but you can think of it as a special case of #1. By only moving in 2 directions, the guard must be making a stright line as well. If you think about it, a line is drawn by pixels that move up OR down, left OR right. So if the guard moves in only 2 directions, then you can assum the guard is walking in a stright line.

--------------------------------------------------------------------------------
Good Links: C++ Reference, Java API

##### Share on other sites
simple pythag!
If your using grid references for each square, do something like
'in vb, abs turns all numbers to positive numbers'and sqr is the square roothx = abs(hero.x - guard.x)hy = abs(hero.y - guard.y)vx = abs(victom.x - guard.x)vy = abs(victom.y - guard.y)if sqr(hx + hy) <= guard.lenofsight then   if sqr(vx + vy) <= guard.lenofsight then      guard.attack hero, hero.x, hero.y   end ifend if

edit: Little mistake...

[edited by - Nice coder on June 7, 2004 4:10:12 AM]

##### Share on other sites
There are lots of ways to do as has already been stated.

One other way that comes to mind is reversing things, if all guards have the same vision range then you could instead, have the victim send out a ping. If a guard is in range they are alerted and run towards you. While this means the guard doesn''t acutally see anything it might work just as well for you.

-----------------------------------------------------
"Fate and Destiny only give you the opportunity the rest you have to do on your own."
Current Design project: Ambitions Slave

##### Share on other sites
If you go the route of the vic sending out a ping to guards, then you can also have different values for stealth and such. That way you could backstab someone, and no one would notice unless a guard is very close, or you could miss, and set the guards comming from the whole city.

##### Share on other sites
You may wish to consider if your gaurds can also be alerted by noise as well. It may be either complicatin things or simplifying them depending on how you want to handle it. If a victim or a victims friend is around surel someone is going to be screaming blue murder (if its not quick and painless). Hmmm.

If you want to complicate things maybe nearby citizens who are in line of sight may run off to find a guard if they witness a murder.

##### Share on other sites
What you really need to consider here (which was intimated by the previous poster) is how do you want to handle information flow between characters in your game. Should information only be attained via visual stimuli, or should other forms of communication occur? For example, sound, smell, conversation, writing. A complex environment with information flowing and changing between different forms is not overly hard to create and adds a level of emersion to your game that many players will enjoy. So, here''s a scenario. The player kills an NPC in an alley. This is witnessed by a street urchin nearby. With low probability the street urchin makes a noise alerting the player to his presence (because street urchins see all manner of things and know to stay quiet)!. Assuming that he does not alert the player (which brings about its own sequence of events) he flees the scene and heads straight to the guard/police/etc. He offers to sell information about a murder that occured. If the police officer pays him a small amount he then tells the police officer he saw someone getting killed and provides a rough description of the perpertrator.

Now, you don''t actually need lots of complicated mechanisms to bring this about. Just some basic behaviours and some information flow. You could use Line Of Sight tests within the local environment to determine if someone can see the crime. A separate test within a variable size radius as to whether someone can hear the crime (the radius being dependent on how stealthy the player is in their attempt to commit the murder and how efficient they are in their attack). A few days later you might even include a scent radius permitting NPCs to smell the body some distance away.

Given a character that detects the crime, define a small set of possible behaviours depending on the class of the crime. E.g., for murder, most people might flee and find a guard. Some might attack the player, while others might try and use the information (sell it to the police, the local thieves/assassins guild, blackmail the player), etc. If the character has several possible behaviours, select from them either randomly, or such that it helps your storyline. Assume that this behaviour is enacted and propogate the effects through your NPC population.

This type of information flow creates a very rich environment in which the players actions directly affect the state of many NPCs within the game. This is a good basis for a realistic reputation system as well!

Cheers,

Timkin

##### Share on other sites
Excellent post Timkin. This is a great example of how to build a system. It is clean, easy to maintain, scalable, and can lead to complex behavior. Information flow is definitely the key.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632593
• Total Posts
3007277

×