User Tag List

Results 1 to 1 of 1

Thread: One Object Following Another with object avoidance

  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)

    One Object Following Another with object avoidance

    This is an explanation of a system I designed a few weeks ago, which enables ONE object to follow the player, whilst avoiding obstacles.

    It currently works for only one object, and maybe to intense to have multiple followers.

    [size:14pt]Threaded Path Technique[/size]
    [size:11pt]You Will Need:[/size]

    Move Safely 2 Object
    Advanced Direction Object

    ...and in your frame you will need...

    A small 2x2 Active Object - We'll call this "LOS DETECTOR". It's just a small yellow box. It'll be used to test for obstacles between two points.
    A Player Object - Basically, just the player.
    A Follower Object - The NPC you want to follow him.
    A Small Circular 16x16 Active Object - We'll name it "MARKER". This will act like a node, or a 'point' in the path for the follower. Like a breadcrumb, if you wanna use the Hansel and Grettle analogy.


    It works like this:


    Imagine walking through a maze holding a piece of thread. Where it pulls tight, it gets caught against the edges, like this:


    See where it pulls tight?

    Well this does something similar. Using Move Safely 2, it tests for obstacles between the player and the follower. We call this a Line Of Sight (LOS) Routine. It basically moves a detector object between two points to find out if there are obstacles between them. If not, then those two points have a 'clear line of sight'.

    So if we test the line of sight between our follower and player, then we now have this:


    Move Safely basically moves our 'LOS detector' object along that line, constantly asking it "are you over an obstacle?... how about now?... and now? ... and now?"

    What we do next is a couple of events like this:

    ALWAYS
    --- Set 'State' of 'Follower' to 'CAN SEE'

    On Safety
    + Detector is over Obstacle
    + Only One Action When Event Loops
    --- Set 'State' of 'Follower' to 'CANT SEE'
    --- Create 'MARKER' at 'LOS Detector'
    --- Stop Safety Loop and Push Out

    The 'State' bit is just an Alterable string. We'll use this later when we're coding the behaviour of the follower AI - how he'll react when he CAN see the player, and when he CANT. He starts off assuming that he can see you (the always event), and then tests for an obstacle in case he can't.

    When the LOS Detector colides with a wall, a MARKER is created at the point of colision, as shown here:


    It has added a marker where the colision happened.

    However, we're not quite finished with that above event yet.

    Because the Marker is so close to the wall, any NPC character trying to follow that Marker would wack into the wall as well. Ideally, we want to lift the marker away from the wall, giving more space for the NPC (the follower) to move in.

    To do this, we need to find the angle of that dotted line. So we use the Advanced Direction object to do that. We want the angle between the follower and the player. So we change our event to:

    On Safety
    + Detector is over Obstacle
    + Only One Action When Event Loops
    --- Set 'State' of 'Follower' to 'CANT SEE'
    --- Set 'Angle' of 'LOS Detector' to Angle between Follower and Player
    --- Create 'MARKER' at 'LOS Detector'
    --- Stop Safety Loop and Push Out

    Our basic objective is to move the Marker away from the wall at an angle of 90 degrees from the line of sight. We do this twice, once doing it 90 degrees clockwise, and the other time 90 degrees anticlockwise.

    That last bit is very important!! It lets the system check what side of the Line of Sight is blocked off. If you don't get how important this is, just you try doing it without



    See how we've split the marker into two? One on the left of the dotted line, one on the right? This represents the way we've moved it right, tested if it was overlapping an obstacle, and then moved it left and done the same.

    In this case, the side without an obstacle is the left hand side, so we've moved it in that direction.

    This basically lifts the marker away from the wall. The marker is what the Follower will navigate to when he can't see the player. Moving it away from the wall gives him a bit more room to move. I would move it about 16 pixels.

    As a quick note, in case you don't know, you can use trigonometry to help with this. For instance:

    X = Xpos("MARKER") + sin(LineAngle("LOS DETECTOR"))*16

    ...will move the marker 16 pixels away from its previous position, along the Line Angle. For the Y coordinate, you do:

    Y = Ypos("MARKER") + cos(LineAngle("LOS DETECTOR"))*16


    Anyway, what we're left with now is this:



    It's starting to take shape isn't it? But this isn't the end.

    While the Follower is walking towards the Marker, the Player will still be wandering around, perhaps turning more corners, etc.

    So we carry on doing LOS tests, but instead of looking from the Follower to the Player, we look from the NEWEST MARKER to the Player, like this:



    If you're careful, you can code it in such a way that it will simply re-use the Line of Sight code above, but using the new marker. If a wall gets in the way again, you end up with another Marker, followed by another, followed by another, like this:



    And that's how it works.

    To make your Follower follow the path, you can Spread a value inside the Markers, giving them an ID number. Then do the following events:

    STATE of Follower = "CANT SEE"
    + ID of Marker = 'NObjects("MARKER")-1'
    --- Look in direction of MARKER

    Follower colides with Marker
    --- Destroy Marker

    STATE of Follower = "CAN SEE"
    --- Look in direction of PLAYER
    --- Destroy MARKER

    This will make him always follow the OLDEST Marker that exists. When he touches it, it gets destroyed, so the he has to follow the next one.

    When he can see the player, all MARKER objects get instantly destroyed, to clean the memory up a bit.

    [size:14pt]Some Useful Expressions[/size]

    An expression or two you may find useful:

    Oldest Marker ID = NObjects("MARKER")-1
    Second Oldest Marker ID = NObjects("MARKER")-2
    Newest Marker ID = 0

    When I built my own example of this, I made it so the OLDEST Marker always follows the Follower. And the dotted line is always drawn from the NEWEST Marker to the Player.

    If there is only one Marker, then that one is both newest AND oldest, so it acts like the line was being drawn from the Follower to the Player.

    The Follower then always looks to the second oldest Marker to provide it with its directions.

    Simple



    [size:14pt]Practical Example[/size]

    There's a practical example attached to this article, because there's only so much you can actually explain. This is the downer with a procedure like this, it's too complex to explain using a commented example, and too practical to explain using just an article. So this example will hopefully show all the ins and outs of coding something like this.

    More importantly, it'll let you see the effect in action.

    It has two frames, one is a basic example, the second is a complex maze. The little NPC character will follow you no matter where you go.

    1st Example

    A small square, you walk around the square, and the NPC will follow you. It draws two lines to show the Line of Sight processes that are running.

    2nd Example

    A Maze. The NPC will follow you through the maze. In addition to the features from example 1, you can retrace your steps and 'undo' the path you have laid. This way if the NPC is a long way behind, and you decide you've taken a wrong turning and go somewhere else, the NPC won't imitate your mistake.

    This frame has better comments.

    Have fun!
    Attached files Attached files

Similar Threads

  1. YWHTG: You Will Hate This Game [Flash] Skill / Avoidance game
    By FGRaptor in forum Released Games & Apps in Flash
    Replies: 2
    Last Post: 2nd May 2016, 03:14 AM
  2. List object and Dropdown object bug in Full Screen mode
    By MTCMusic in forum Hardware Accelerated Runtime
    Replies: 2
    Last Post: 26th July 2013, 01:22 PM
  3. Object order issue when rendering. (Move object in front of/behind...)
    By JayTholen in forum SWF/Flash Export Module Version 2.0
    Replies: 1
    Last Post: 16th November 2012, 11:39 PM
  4. How to make a backdrop object overlap an active object? mfa included
    By path in forum Multimedia Fusion 2 - Technical Support
    Replies: 4
    Last Post: 30th August 2012, 10:07 PM
  5. Beta 15, Testing backdrop object, active object and drag and drop movement
    By Fernando in forum Android Export Module Version 2.0
    Replies: 3
    Last Post: 9th February 2012, 11:57 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
  •