2D Line of Sight?

It’s becoming pretty obvious that I need some sort of “line of sight” system to figure out if the ninjas get spotted. (My current “compare direction and coordinate X” won’t go a long way).

I did check some BlitzMax archives, and was hoping to find some libraries but couldn’t find suitable yet. (It’s too late now anyway, so I’m better off by checking things tomorrow).

Here’s some articles I googled regarding 2D line of sight – seemed to have some decent info:

Any relevant links are most welcome.

12 Responses to “2D Line of Sight?”

  1. Andreas says:

    Use 3d math. Get the right vector of the guard and the direction to the ninja (normalized vectors) use the dot product on these vecotors, if the result is 0 the guard is looking exactly at the ninja. You can check for a cone like this for instance if( dot -0.5) ninjaInSight = true

  2. Andreas says:

    some code got lost because of bad signs I imagine… if dot less than 0.5 AND dot more than -0.5 THEN ninjaInSight

  3. Juuso says:

    yeh, but besides direction there could be wall/objects between them which makes ninjaInSight = false :)

  4. Andreas says:

    He he ok, well have a line for the guards line of sight, check that line against all gameobjects bounding box. If the ninja is the closest one it´s visible.

  5. Juuso says:

    That is bloody genius. How come I didn’t think of that.

    Maybe I should do (1) less 2d math research when tired and (2) spend more time listening to people who know. :)

  6. Andreas says:

    :) glad I could help. Sometimes you get blind to the simple solution.

  7. shagwana says:

    http://www.blitzbasic.com/codearcs/codearcs.php?code=148

    This is a code snippet I posted for blitz 3d a long time ago, using a Bresenham line algo. Should’t be to much work to get it working in max, it lets you trace the line in controlled steps rather then doing it all at once.

  8. tonic says:

    Do yourself a favor and buy “Real-Time Collision Detection” by Christer Ericson, you’ll find lot of usable stuff there, including relevant stuff for problems like this (e.g. tile traversal). Every game programmer should own it. :)
    http://bit.ly/RTCDbook

  9. Jake Birkett says:

    The solution was not in your line of sight.

  10. Juuso says:

    @Andreas: yeh, thanks. Like Jake said… it was not in my line of sight. I was thinking something way too complex ^_^

    @shagwana: I checked that one, thanks.

    @tonic: eh, I’d rather keep it so that somebody else tackles this for me :P

  11. Joonas says:

    You can likely use the A* algorithm. http://en.wikipedia.org/wiki/A*_search_algorithm

    I wrestled with this some years ago when trying to put together a tile-based stealth title on BlitzBasic. What I came up with after brushing up my math skills for days was basically the A* algorithm. It worked for me, but I wasn’t using it in realtime.

  12. Juuso says:

    @Joonas: That’s one cool avatar. O:)

    …and, I might look something simpler – such as vision check. I think it’ll work just fine for me. Thanks.

Leave a Reply