User Tag List

Results 1 to 8 of 8

Thread: Quick Line of Sight

  1. #1
    Clicker Multimedia Fusion 2

    Join Date
    Sep 2006
    Location
    Britain, South Coast
    Posts
    1,030
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick Line of Sight

    Right, this is my first article for some time now, so cut me some slack if it's a little botched, heh.

    Anyhow...

    [size:20pt]QUICK SINGLE-TO-MANY LOS TESTS[/size]

    This article is written to help you create fast 'single-to-many' LOS tests. This is ideal for situations where one object must test for a line of sight to many objects.

    Your most common use of this I'll wager would be in standard one-player games where the player controls one object and battles multiple enemies (so any platformer, RPG, that kind of thing).

    Right, so I think that's enough to let you know where we're aiming at, now let's get onto how it works.

    [size:17pt]WHY IS IT QUICK?[/size]

    I think it's becoming common knowledge now that I really don't like fast loops. I mean they're alright, but they're not the fastest thing out there when it comes to iterations.

    The fastest thing I've found so far is Action Loops. Not event loops, might I add - those are slow as hell, and the reason they invented Fast Loops.

    Action loops are the little hidden loops that repeat your actions for each object selected by the conditions. So for an example, we have 50 objects and we do thus:

    ALWAYS
    --- Object: Set speed to 20

    One event, yes, but it's actually FIFTY loops. The condition is a kind of setup to decide how many objects we'll operate on, and the actions are repeated for each one. And did I mention that they're blisteringly fast?

    This is the technology that will make this system quick. We'll use fastloops, of course - but only where necessary. The bulk of it (the entire object-moving process in fact) will be done using Action Loops. You'll see what I mean by all this as we go on.

    [size:17pt]THE TECHNIQUE[/size]

    [size:14pt]SETUP[/size]

    The setup is important. In our worked example, we have one player object, and he has 100 NPCs in his world, all of whom need to be LOSsed.

    We will create 100 detectors (please don't switch off right now, I know detectorless is all the rage, and maybe you can work on that yourself, but for now just bear with me and make the detectors). So each instance of the NPC type will have an instance of the DETECTOR type.

    For every NPC *and* every DETECTOR, we give them an alterable string and name it something like "Can be seen". Note that this could just as well be a flag or value, but I'm using alterable strings here for readability.

    So the 'NPC' object type will have a 'Can be seen' property, and the 'DETECTOR' type will also have a 'Can be seen' property.

    [size:14pt]EVENTS[/size]

    The objects must assume that they CAN be seen by default, so our first event is:

    Code:
    // All detectors and enemies default to visible:
    
    ALWAYS
    --- Detector: Set 'Can be seen' to "YES"
    ---    Enemy: Set 'Can be seen' to "YES"
    Next, we start a fast loop. Now the lines traced by the LOS in this example are relative. So unlike many systems which move a detector, say, 3px per step, this system will move all the detectors by a certain percentage, so the step will be larger or smaller depending on the distance between the player and the NPC. It's avoidable, but this article just shows a proof of concept really.

    I've decided that 260 steps is sufficient to my purpose, so I'll start a fastloop with my next event line:

    Code:
    ALWAYS
    --- System: Start loop "MAIN LOOP" 260 times
    Now for the actual loop code. This will use object selection (Action Loops) to move all 100 detectors one step at the same time. Observe:

    Code:
    ON Loop "MAIN LOOP"
    --- Detector: Set X position to:
             X("PLAYER") + (X("PLAYER") - X("DETECTOR"))
                           * (loopIndex + 1) / 260.0
    
    --- Detector: Set Y position to:
             Y("PLAYER") + (Y("PLAYER") - Y("DETECTOR"))
                           * (loopIndex + 1) / 260.0
    This finds how much of the loop has already been done ((loopIndex + 1) / 260.0) and returns it as a fraction. This fraction is applied to the distance between the player and the detector (X("PLAYER") - Y("DETECTOR")). That then is added to the player position (the starting point).

    Object selection naturally pairs objects of two types, so Detector #1 will inherently be paired with NPC #1, and it all works like Noah's Ark (#and the animals went in two by two, hurrah! hurrah!#). Neatly giving us a loop of 100 objects which executes much quicker than a fastloop performing the same function.

    Next we need to test if we encounter obstacles on the way:

    Code:
    ON Loop "MAIN LOOP"
    + Detector: Overlaps Obstacle
    --- Detector: Set 'Can be seen' to "NO"
    Again, our event processes them all in one go here, much quicker.

    Finally, we're done with the fast loop. We now have 100 Detectors who carry with them the seen/unseen values of their mate. All that's left to do is copy this data from the Detector to the NPC.

    Code:
    ALWAYS
    --- NPC: Set 'Can be seen' to:
                Can be seen("DETECTOR")
    Again, object selection pairs them off so each NPC reads his own detector.

    Aaaaaand we're done. View the attached example if you wish, as it also contains a more optimised example of this which uses specific steps in pixels rather than ratios.

    (NOTE: Example requires Clickteam movement controller extension)
    Attached files Attached files

  2. #2
    Clicker Multimedia Fusion 2 DeveloperSWF Export Module
    RickyRombo's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere between here and there
    Posts
    3,167
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Quick Line of Sight

    That is perfect! Thanks Dines!

    One question, what if the player is behind the NPC, so its "out of site", but yet with this method wouldn't you be seen?

    EDIT: Wait, I guess you could add,

    *On loop("MAIN LOOP")
    +Detector is overlapping ("Active")
    -Set "Can Be Seen" of Detetector to "no"

    and have the Active follow the NPC

  3. #3
    Clicker Multimedia Fusion 2

    Join Date
    Sep 2006
    Location
    Britain, South Coast
    Posts
    1,030
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Quick Line of Sight

    I guess so - I usually use a formula to check that the angle between player and target would enable him to see it or not. If it's outside an acceptable range, set a certain value to YES or NO then discount that from the object list.

    Can't remember how I did it now

  4. #4
    Clicker

    Fusion 2.5 DeveloperFusion 2.5+ DLCAndroid Export ModuleHTML5 Export ModuleiOS Export ModuleUniversal Windows Platform Export ModuleSWF Export ModuleXNA Export ModuleInstall Creator Pro
    nivram's Avatar
    Join Date
    Jul 2006
    Location
    Bandon, Oregon
    Posts
    6,729
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Re: Quick Line of Sight

    Thanks Dines...great article.

    Marv
    ​458 TGF to CTF 2.5+ Examples and games
    http://www.castles-of-britain.com/mmf2examples.htm

  5. #5
    Forum Moderator Fusion 2.5 DeveloperAndroid Export ModuleHTML5 Export ModuleSWF Export Module
    Stephen's Avatar
    Join Date
    Aug 2008
    Location
    Montana
    Posts
    4,515
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Quick Line of Sight

    Nice.

    stephen1980
    _____________________________________________
    Nivram's Examples -Need extensions? Send me a PM.-


  6. #6
    Clickteam Clickteam
    LB's Avatar
    Join Date
    Jun 2007
    Location
    Richardson, Texas, North America
    Posts
    8,937
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Re: Quick Line of Sight

    Great tutorial! However I've ALWAYS encountered the prblem with percentage stepping that the detectors if the enemy is far enough can skip right over obsticles and assume a direct line of sight to the player when there isn't one.

    Still nice work!
    Working as fast as I can on Fusion 3

  7. #7
    Clickteam Clickteam
    Olivier's Avatar
    Join Date
    Jun 2006
    Posts
    2,996
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)

    Re: Quick Line of Sight

    Great article as usual. I just wanted to point out that this technique won't work by using a flag as the 'Can be seen' property. It's a shame because it would be the most efficient way of doing it, ressource wise.
    It won't work because you can't set a flag to another object's flag via the Expression Editor (hint hint CT). You can only set a flag ON or OFF. I've tried to work around that without success.

  8. #8
    Clicker Multimedia Fusion 2

    Join Date
    Sep 2006
    Location
    Britain, South Coast
    Posts
    1,030
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Quick Line of Sight

    Olivier: Yeah, it's a bummer. But I like the readability of strings anyway, lol.

    LB: Yeah, you can set the loop resolution quite high but it's still a risk (and a waste to be doing 266 steps for objects that are only 100px apart). The best method I found was the second example in the file, which follows this procedure but uses pixel steps instead. But I think the percentage one is simple enough to get the point across in the article - if someone understands the percentage method, they'll be able to step up quite easily to the fixed method

Similar Threads

  1. Line of sight
    By JosephFTaylor in forum Multimedia Fusion 2 - Technical Support
    Replies: 2
    Last Post: 15th June 2012, 05:12 PM
  2. Line of Sight example
    By Ryan in forum File Archive
    Replies: 4
    Last Post: 30th August 2010, 05:27 AM
  3. Line of Sight
    By Panchos in forum Multimedia Fusion 2 - Technical Support
    Replies: 14
    Last Post: 29th August 2010, 11:36 AM
  4. Line of Sight
    By Uzalu in forum Extension Development
    Replies: 1
    Last Post: 5th December 2009, 10:52 PM
  5. Line of sight
    By Beginier in forum File Archive
    Replies: 4
    Last Post: 24th June 2009, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •