Nearest instance?

Discussion and feedback on Construct 2

Post » Wed Sep 21, 2011 10:18 am

[QUOTE=Ashley] A couple of subevents makes it fairly easy. You don't need an array - just two values for 'closest so far' and 'closest UID'. Local variables would be good for those. The algorithm works like this:

1. Set 'closest so far' to a large number e.g. 999999, and 'closest UID' to -1.
2. For each object:
---a) if the distance to this object is less than 'closest so far':
-----> set 'closest so far' to the distance to this object
-----> set 'closest UID' to this object's UID
3. Pick object with UID = 'closest UID'
---> do something with this instance - it's the closest[/QUOTE]

Combine this with jayjay's idea of having a large invisible square/circle object (collision mask) so you pick only the objects within it/overlapping it, then you wouldn't have to worry about having lots of objects to iterate through. Unless you needed the range to be really big then this would be pointless as you would have to iterate through all objects anyway.
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

Post » Wed Sep 21, 2011 11:05 am

No, the range is relatively small.
However, this UID and looping is part of what is confusing to me about construct. SO what am I to do? A large invisible circle around the object? I am still a bit confused.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Sep 21, 2011 11:06 am

I think the best way (since so many people would need this) would be for the function to be built into the program and able to be optionally used.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Sep 21, 2011 11:35 am

[QUOTE=sqiddster] No, the range is relatively small.
However, this UID and looping is part of what is confusing to me about construct. SO what am I to do? A large invisible circle around the object? I am still a bit confused.[/QUOTE]

Every object has unique ID (UID) in Construct and is used to select a specific object (no two objects have the same UID). Of all the ways to do it you will still have to loop through all objects you want to test the distance to. To optimise this and make it faster you would need to make sure you do the bare minimum with events to each object only focusing on the objects that matter. So:

Use paint or whatever to draw a square say 320x320 pixels in size (or whatever the largest distance is you want to check) fill it pink or whatever (doesn't matter), set this as a new object call it rangeCollisionBox or whatever, set opacity to 80 just so you can see whats through it. Set this objects visibility to "invisible" you don't want to see it when you run the program. Setup the events so that every "tick" the rangeCollisionBox is positioned at the players position or do this every time you want to check the nearest object.

Next, do a "for each" on every object you're checking the distance to then add a sub event underneath this which checks whether the objects are overlapping the rangeCollisionBox. If they are then perform Ashley's algorithm on each of these.

I'm not 100% sure this is right and 100% sure it makes sense as I can't test at the moment. I hope it helps I'll see if I can knock up a capx tonight.

Edit: If Ashley made the nearest function and it used his method you could still use a collision box to filter the objects.00Rez2011-09-21 11:44:17
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

Post » Wed Sep 21, 2011 12:02 pm

A .capx would be nice... they are always very informative!
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Sep 21, 2011 12:38 pm

[QUOTE=sqiddster] A .capx would be nice... they are always very informative![/QUOTE]

You're lucky I managed to do this at work! It's here. Hope this helps you and anyone else!

The block points itself to the nearest purple enemy. You can see the collision box!

Edit: Here's an exported version if you just want to see it in action!

Edit2: Updated links to take out the collision test. Now they only use Ashley's method!00Rez2011-09-21 23:22:56
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

Post » Wed Sep 21, 2011 2:18 pm

Don't use invisible collision objects! It could make it run much slower than the loop I described using events only.

Remember the way condition works already has a built-in 'for each'. So an event reading 'A overlaps B' means 'For each A: test if overlaps B, and remember both instances if so'. Therefore, using an object to test overlap of nearby objects still tests every single instance in the game. Worse, collision checks can be much slower than just measuring the distance. So I would strongly recommend you do not use a big overlapping sprite - just use something based on the events I posted, it's the fastest and simplest way.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Wed Sep 21, 2011 2:36 pm

[QUOTE=Ashley] Don't use invisible collision objects! It could make it run much slower than the loop I described using events only.

Remember the way condition works already has a built-in 'for each'. So an event reading 'A overlaps B' means 'For each A: test if overlaps B, and remember both instances if so'. Therefore, using an object to test overlap of nearby objects still tests every single instance in the game. Worse, collision checks can be much slower than just measuring the distance. So I would strongly recommend you do not use a big overlapping sprite - just use something based on the events I posted, it's the fastest and simplest way.[/QUOTE]

Whoops sorry about that! Thanks for that Ashley. A forgot to think about how the collision works :/ I've updated the links to take out the collision square! Just uses your method now...
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

Post » Wed Sep 21, 2011 10:35 pm

It seems the .capx still uses a collision box?

EDIT: I removed all the code that checks for collision. It runs perfectly and at 59fps when there are many squares to check. So it seems OK.

Next step. What happens when I have multiple instances of the object which checks as well?sqiddster2011-09-21 22:51:23
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Wed Sep 21, 2011 11:35 pm

[QUOTE=sqiddster] It seems the .capx still uses a collision box?

EDIT: I removed all the code that checks for collision. It runs perfectly and at 59fps when there are many squares to check. So it seems OK.

Next step. What happens when I have multiple instances of the object which checks as well?[/QUOTE]

Don't know what happened with the link DropBox didn't update the file properly I think it *should* be the new version now...

Is this what you mean?
B
21
S
7
G
2
Posts: 185
Reputation: 3,280

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 2 guests