0

Hey guys, I was looking into a simple flash game, and have a few enemies patrolling the area. I made each of them have a view angle of about 35 degrees, and a distance of 150 px. The problem is that they can see through walls. How would I check to see if a wall was in between the guard's view of the player?

I thought of ray tracing, but don't know a way to implement it. I also thought of maybe drawing a rectangle between the player and the guard, and collision check it against the walls, but again don't know how to implement it..

Any ideas?
Thanks, Max

Jess
  • 8,628
  • 6
  • 49
  • 67
  • This might help you: http://stackoverflow.com/questions/5943531/an-efficient-way-to-check-for-objects-intercepting-a-line-of-sight-in-as3 – Marty May 20 '11 at 00:22
  • 1
    Did you mean "find player in viewfield"? – alxx May 20 '11 at 05:55

2 Answers2

0

I think that you are not that far off.

Since your walls will probably be sprites or some other display object, you know their positions ect.

Then you could draw an opaque sprite with alpha 0 that embodies your enemy viewing range and then do a collision test on all walls and all viewing ranges. This will also enable you to do a collision detection between the player and all enemy viewing ranges.

After a quick search this might be what you need

Cheers

Community
  • 1
  • 1
Dennis Jaamann
  • 3,547
  • 2
  • 23
  • 42
  • This was my idea, but what happens if the guard is staring at you, and the wall is behind you? – Jess May 19 '11 at 21:52
  • If you know the position of each wall, the player and the view direction of your guard, you should be able to calculate whether there is a wall between your guard and your player – Dennis Jaamann May 19 '11 at 21:57
  • Could you give a simple example of how to do this? I know it is possible, just not how to do it. – Jess May 19 '11 at 21:59
  • It has been a while since i did some flash dev, so i'm afraid i can't help you with that. It is possible though and you are not far off :) – Dennis Jaamann May 19 '11 at 22:08
0

Just a thought.
Keep your code just as it is, but do a second test.
If it finds a target with your current code run another test to see if there is a wall between them with a ray type test.
This would keep your working code as it is and minimize the field for ray testingto the width of the target.

The_asMan
  • 6,364
  • 4
  • 23
  • 34