[Solved] How do I pick objects within a family?

Get help using Construct 2

Post » Tue Jun 14, 2016 9:23 am

Let's say I have a Family with 100 Sprites randomly placed on the screen.

I want to pick all of objects in that family that is closer than 100px to another one in the same family, if there is any?

In my mind this would work but it doesn't really.
Pick by evaluate. distance(family1.X, family1.Y, family1.X, family1.Y) < 100

It seems like it's always true, since itself is always closer than 100px to itself.

So what is the smartest way to filter out itself in the evaluation?

Expected: If any objects in a family is closer than 100px to eachother = True (Picked)
Last edited by tunepunk on Tue Jun 14, 2016 12:06 pm, edited 1 time in total.
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Tue Jun 14, 2016 11:17 am

Image

This didn't seem to work either as expected. Is there no way to compare and pick the way I'm trying?
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Tue Jun 14, 2016 11:39 am

Image

Another test that didn't work. What am I doing wrong?

capX
https://dl.dropboxusercontent.com/u/20560446/Scirra/distancepicking.capx
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Tue Jun 14, 2016 12:05 pm

Image

Finally solved it. Had to create a second family with identical objects in it.

Sharing if anyone finds it useful.

But if anyone has a smarter solution that doesn't involve 2 families with same objects, please let me know.
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Tue Jun 14, 2016 12:44 pm

In the first attempt, you locked the first created sprite to the mouse. Then you looped trough all sprites, and feeded all those to the function.

The function is a NEW TOP-EVENT, so it forgets (by design, and luckely so) any previous picked lists. So the function picks out of all sprites those that that have a differend UID then those you feeded to the function. And yes, including the one you locked to the mouse. But also all others. Then loops trough them, and compares the distance. As a result they kinda flip-flop. In one loop one gets set 50 % opaque, in the next loop that same one gets back to 100 %. You needed to compare to only that one that you locked to the mouse.

In the second attempt. The for each runs on each member of the family, because there is no previous picked list. There is also no previous picked list for Sprite. So it picks out of all sprites those that dont have the same UID. Wich is just all of them. So basecaly it runs trought all family members and for each famely member it runs trough all sprites. If you have 100 sprites, that would be a loop that counts 100 * 100 times. Again creating some flip-flop thing.

Here is a working example. I used drag and drop to be able to explore more situations.
https://drive.google.com/open?id=0B1SSu ... HUydDNQcmM

As you see i feed to the function only the postion of ONE sprite. In the function i pick all but that one sprite. Then i compare the distance of those only one time against the position of that one sprite.
B
33
S
18
G
27
Posts: 2,447
Reputation: 20,358

Post » Tue Jun 14, 2016 12:52 pm

In your last post, that should set each sprite that is less then 50px away from each other opaque.
That works if the sprites in the layout are far away from eachother, creating the illusion that those that are close to the one that is locked to the mouse are picked.
That works if that was what you intented to do.
B
33
S
18
G
27
Posts: 2,447
Reputation: 20,358

Post » Tue Jun 14, 2016 3:23 pm

99Instances2Go wrote:In your last post, that should set each sprite that is less then 50px away from each other opaque.
That works if the sprites in the layout are far away from each other, creating the illusion that those that are close to the one that is locked to the mouse are picked.
That works if that was what you intended to do.


Yeah... Last test does what I intended it to do. but is a bit heavy on resources (for each) as it goes through every single family1 object every tick and checks if there is any object in family 2 within the specified distance. If I have a lot of objects that's gonna be super heavy, so it's not optimal.

Now I'm looking for a smarter (more performance friendly) way to do the same thing with events. I know i could use collisions to do something similar, but I'm trying to do sort of a customized collision detection based on distance between objects or within a radius in a family, and not a collision polygon.

Maybe the most efficient way would be to add some steps or increment? First i can start by checking every 0.5 seconds if somethinng is within a slightly larger radius. I could create a variable (check rate) that is based on the distance between objects, that gets smaller as the distance shrinks.
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Tue Jun 14, 2016 3:28 pm

Did you even open my example ?
B
33
S
18
G
27
Posts: 2,447
Reputation: 20,358

Post » Tue Jun 14, 2016 3:42 pm

99Instances2Go wrote:Did you even open my example ?


I did. It worked in a similar fashion. But I'm going to have lots of objects that are going moving around randomly on the screen. And when they get close to eachother they change direction.
Follow my progress on Twitter
or in this thread Archer Devlog
B
35
S
15
G
17
Posts: 944
Reputation: 12,210

Post » Tue Jun 14, 2016 8:15 pm

Then you gots the solution, there is not really a more performance friendly solution. As long as you dont check for overlaps/collissions in the whole loop, you be fine. Sorry i got on the wrong leg because of this sprite you dragged arround.
B
33
S
18
G
27
Posts: 2,447
Reputation: 20,358


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 15 guests