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

Get help using Construct 2

Post » Sun May 15, 2016 7:20 am

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.
B
8
S
4
G
1
Posts: 16
Reputation: 1,276

Post » Sun May 15, 2016 9:35 am

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

Code: Select all
 For Each EnemyObject order by distance(ObjectBeingDragged.X, ObjectBeingDragged.Y, EnemyObject.X, EnemyObject.Y) ascending


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: Select all
 

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

I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Sun May 15, 2016 9:49 am

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.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,469

Post » Sun May 15, 2016 11:06 am

@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.
B
8
S
4
G
1
Posts: 16
Reputation: 1,276

Post » Sun May 15, 2016 11:55 am

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sun May 15, 2016 12:04 pm

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.

plugin-jcw-trace-raycast_t172320
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sun May 15, 2016 2:17 pm

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
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950


Return to How do I....?

Who is online

Users browsing this forum: RandomlyMooN and 6 guests