How do I get the two nearest object uid of the same instance

0 favourites
  • 7 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • I have multiple objects of the same instance, i need to select the two closest to the one that is being dragged.

    So I need something like:

    [is 'object1' dragging]

    -----[pick nearest 'object1']

    ------------>set global variable uid1 = 'object1'.uid

    -----[pick nearest 'object1']

    ------------>set global variable uid2 = 'object1'.uid

    I just have no idea how im supposed to get something like that setup.

    Thanks in advance.

  • Picking n nearest instances? You may wish to mix "For Each (Ordered)" with distance formula:

     For Each EnemyObject order by distance(ObjectBeingDragged.X, ObjectBeingDragged.Y, EnemyObject.X, EnemyObject.Y) ascending [/code:ozycgyqb]
    
    This will iterate to all of your EnemyObject instances starting from EnemyObject nearest to ObjectBeingDragged in the first iteration, then the second nearest in the second iteration and so forth.
    
    Since you only need 2 nearest objects, you could go for something like:
    
    [code:ozycgyqb] 
    
    set global variable i = 1
    For Each EnemyObject order by distance(ObjectBeingDragged.X, ObjectBeingDragged.Y, EnemyObject.X, EnemyObject.Y) ascending 
    
    

    if i = 1:

    > set global variable uid1 = EnemyObject.UID

    if i = 2:

    > set global variable uid2 = EnemyObject.UID

    > stop loop

    add 1 to global variable i

    [/code:ozycgyqb]

  • In the case that all of these objects that could be drag around and all other objects you are testing for nearest distance are the same object type, I suggest you to create an invisible sprite on the object you are dragging, and refer to the code above using the invisible sprite in place of ObjectBeingDragged. This is because all those object picking is quite problematic when you have to refer to an object to another object that has the same object type.

    If you are doing the paragraph above, one of the thing you must also take care of is to eliminate the first nearest instance as you are creating the invisible sprite on the instance you wanna test on, and the first nearest instance actually has become the object you are testing for. Because of this, it must be excluded. So now, to look for 1st and 2nd nearest instances, you must now look for 2nd and 3rd nearest instance in the event sheet.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • TwinBlazar

    unfortunately no luck, and i dont think this would solve my issue either.

    Basically i have a card game, and i need to be able to drop the card between two others. right now if you let go of the card it just gets thrown to the end, which isn't ideal.

  • I suppose you started with the 'pick nearest' condition. And then you encounterd 2 problems.

    1/ 'nearest' picks nearest to a position. The 'nearest' to the position of a sprite is the sprite itself.

    2/ What ever else you try to pick under a 'is dragged' condition, only the dragged one is picked. This is just natural, because the 'on dragged' picked that dragged sprite, and for all subconditions that follow, that pick is the Previous Picked Selected Object List. All following subconditions start picking from that Previous Picked Selected Object List. And that is the dragged one.

    To solve this, the easyst way is to use functions. Functions start picking from scratch. Always.

    Therefor, here is a capX that is based on 'pick the nearest'. Just the show you why it probaly not worked, and the solutions that can be used.

    https://drive.google.com/open?id=0B1SSu ... 2txVXc3V0E

    But, as TwinBlazar mentioned, it can be easely done by building a distance map first. Here is an exemple of that.

    https://drive.google.com/open?id=0B1SSu ... 3hTUVJhT00

    Yet you have to solve the same little problems. Lowest distance to a sprites position is the sprite itself. Pick others then those picked by the dragging condition is far way easyst done with functions.

  • I have to add.

    If nearest is not just the nearest to a position, but nearest to the eachothers bounding boxes. Then you have to ray trace.

    This plugin is the easyst ray tracer arround.

  • Another solution based on size and families. This picks the nearest to the edges of the dragged sprite. You need families for this capX. Wich i usaly try to avoid in examples.

    https://drive.google.com/open?id=0B1SSu ... Dk5WjNhUWc

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