check distance between 2 instances

0 favourites
  • Hi,

    I have multiple instances of the same object moving around.

    How can I check collision course or distance between 2 instances?

    Thanks,

    Roi

  • Distance(instance1.x,instance1.y,instance2.x,instance2.y)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • how can i do that?

    all instances have the name of the object.

  • Extract from the how do I FAQ

    Picking/selecting instance(s)

        Picking separate instances from a same object - LINK

  • Sorry I still have difficulties with this.

    If I have only 2 instances I guess I can use uid 1 and 2.

    What if I have multiple? which I do.

    What if its random? I want to use for-each loop.

    I want to check distance between 2 instances of the same object

    and I have more than 2 instances of that object.

  • First checking between two instances is a pain because of picking.

    Also it can be pretty cpu intensive if you have many instances because you will have to check instance.count * instance.count times

    But anyway it can be done with picking ID like :

    distance(sprite(0).x,sprite(0).y,sprite(1).x,sprite(1).y)[/code:2dvpkpb2]
    So I guess for distance checking it would look like
    Sprite has two instance variable :
    [ul]
    	[li]closest   //will hold the UID of the closest instance[/li]
    	[li]dist      // will hold the distance of the instance matching the UID[/li]
    [/ul][code:2dvpkpb2]System: for "first" from 0 to sprite.Count-1
       System: for "second" from 0 to sprite.Count-1
       System: Pick Sprite instance loopindex("first")
          Local variable tmpDist = 0 //for clarity
          -> System: set tmpDist to distance(sprite(loopindex("first").X,sprite.(loopindex("first").Y,sprite(loopindex("second").X,sprite.(loopindex("second").Y)
          // Check if closer
          System: Sprite(loopindex("first").dist > tmpDist
             -> Sprite: set closest to Sprite(loopindex("second)).UID
             -> Sprite: set dist to tmpDist
          // Only happen at first check
          System: loopindex("second") = 0
             -> Sprite: set closest to Sprite(loopindex("second)).UID
             -> Sprite: set dist to tmpDist[/code:2dvpkpb2]
    You should get the UID of the closest instance of each sprite to each other in the sprite.closest  instance variable.
  • Thanks.

    I actually only have 3 but I wanted to know how to do it if I have random

    number of instances.

  • let me know if that worked :D... I didn't test that, just wrote as it came on the little quick reply thingy and then edit a bit.

  • Yann I'm sorry to bump your old thread.

    There something made me confuse a lot is:

    "System: Sprite(loopindex("first").dist > tmpDist"

    Isn't using compare two values?

    I didn't understand at all, would you clarify this solution?

  • Joannesalfa

    Hmm after carefull reading this code shouldn't work

    Here is a corrected/commented version

    // two nested loop to compare each instances to each other
    + System: for "first" from 0 to sprite.Count-1
         Local number firstPass = 1 // tell if it's the first pass (to set default "second" as closest)
         
         + System: for "second" from 0 to sprite.Count-1
         + System: loopindex("first") != loopindex("second") // avoid comparing with itself
            Local number tmpDist = 0 // will hold the distance between the current "first" and "second" instance
            -> System: set tmpDist to distance(sprite(loopindex("first").X,sprite.(loopindex("first").Y,sprite(loopindex("second").X,sprite.(loopindex("second").Y)
    
            + System: Pick Sprite instance loopindex("first") 
                // first time we check this "first" instance
                // the "second" is set as the closest by default
                //  OR      
                // the current distance kept in sprite.dist is greater than the current one
                // it means the current "second" one is closer, so we overwrite the values with this one
                + System: firstPass = 1  
                   OR
                + System: Sprite(loopindex("first").dist > tmpDist  
                     -> System: set firstPass to 0
                     -> Sprite: set closest to Sprite(loopindex("second)).UID
                     -> Sprite: set dist to tmpDist[/code:1w6j1y72]It probably looks better without all those comments actually [code:1w6j1y72]+ System: for "first" from 0 to sprite.Count-1
         Local number firstPass = 1 
         
         + System: for "second" from 0 to sprite.Count-1
         + System: loopindex("first") != loopindex("second")
            Local number tmpDist = 0
            -> System: set tmpDist to distance(sprite(loopindex("first").X,sprite.(loopindex("first").Y,sprite(loopindex("second").X,sprite.(loopindex("second").Y)
    
            + System: Pick Sprite instance loopindex("first") 
                + System: firstPass = 1  
                   OR
                + System: Sprite(loopindex("first").dist > tmpDist  
                     -> System: set firstPass to 0
                     -> Sprite: set closest to Sprite(loopindex("second)).UID
                     -> Sprite: set dist to tmpDist[/code:1w6j1y72]
  • Yann

    EDIT:

    I've cleaned your code which is messy, now it's implemented.

    The result was... I don't get what is working, there no working distance each sprites.

    Here is a .capx you may review.

    https://dl.dropboxusercontent.com/u/76227787/LoopDistanceIncomplete.capx

  • Joannesalfa

    This code is an example of how to check distance between each instance to find the closest one for each of them.

    So printing any distance in a text object doesn't make any sense.

    closestInstance.capx

    That's what you can use it for.

    You can also use the dist variable which is just the distance between the current and the closest. And it is used during the loop to get to the closest one.

    Answering the question "How can I check distance between 2 instances?" is closely tied to what you want to use this comparison for. So I had to provide such case as an illustration.

    (also you made a mistake, when I write "!=" it means "not equal to")

  • Yann Awesome! :D

    I'm sorry about "!=" it's my first time to know it means not equal, maybe if you put "=/=" it would mean in common sense.

    It's useful solution for RTS games.

  • Serious? Just get a huge 0-opacity circular sprite, spawn and pin in every instance, test collisions.

  • breackzin

    How would that give you the closest instance ?

    Also if you want something to be invisible you better set it to invisible instead of setting the opacity to 0 so it's not processed by the drawing function.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)