Best way to check the distance of objects not yet reached?

Get help using Construct 2

Post » Sun Jan 17, 2016 6:37 am

I want to have objects go to other objectBs after detecting that objectA is a certain distance or length away.

Prior to that I want it to find the closest one, which I can always do by looping through each and then storing its distance in a variable and testing it each loop to see if it is lower or equal to this distance.

But what is the best or better ways of doing this in programming?

For objectFinder.IsCheckingForTarget
for objectAs
if objectA distance to objectFinder < objectFinder.maxDistCheck
>>Set objectFinder.UIDOfTarget to objectA.UID
>>Set objectFinder.DistanceToMyTarget to dist(self.X, self.Y, objectA.X, objectA.Y)
>> Set objectFinder.maxDistCheck to objectFinder.DistanceToMyTarget

yea and each tick I could have a const store its max distance and reset it when certain conditions are met, such as meeting with its target ((objectFinder.IsOnTarget))

but thats not the problem

the problem is I want a new condition to see that if the distance of this nearest one is over another set distance, find something else before it and go to it. I'm guessing i'll end up using my own system of state machines to manage this, how would you approach it?
Posts: 68
Reputation: 515

Post » Sun Jan 17, 2016 7:56 am

I would look at using the built in "pick nearest / furthest" command ...

edit ... .capx?dl=0
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the
Posts: 1,194
Reputation: 30,782

Post » Sun Jan 17, 2016 8:22 am

Well first of all, have a look at the "pick closest" condition. It does exactly what your loop does. So you could do your above code with three conditions.

Objectfinder: ischeckingfortarget is true
System: For each objectfinder
ObjectA: pick closest to (objectfinder.x, objectfinder.y)

So now let's talk about what you want to do. So roughly this:

Each finder pick closest obj and save it as

Move finder to their target

If the distance to the target is less than some distance
Then go back to start and pick another close object but not the old target.

What I'm not clear on is when the finder gets close to it's target and it picks a new one, do you want it to ignore just the last target or do you want to ignore all previous targets? Or do you want to do something else?
I just re-read what you wrote, so I guess you want a target, once reached to be ignored until the finder gets X distance away.

I guess you could filter away all the objects that are close to the target before finding the closest.
I'd give your finder object a variable called target with a default value of -1. Then Your events could look like this:

Finder: target = -1
For each finder
System: pick Sprite by comparison distance(Sprite.x,sprite.y,finder.x,finder.y) > 100
Sprite: pick closest to (finder.x, finder.y)
--- finder: set target to Sprite.uid

For each finder
Sprite: pick by uid
--- finder: set angle to (Sprite.x, Sprite.y)
--- finder: move forward 100*dt pixels

For each finder
Sprite: pick by uid
System: compare distance(Sprite.x,Sprite.y,finder.x,finder.y) <100
--- finder: set target to -1

Edit: ninja'd
Posts: 5,156
Reputation: 63,448

Post » Mon Jan 18, 2016 2:47 am

I did as both of you had said and it came out quite alright all things considered. I did end up making my own state system using booleans and variable scopes. But it works. And doesnt require anything weird or even foreach loops beyond the conditional ones.
Posts: 68
Reputation: 515

Return to How do I....?

Who is online

Users browsing this forum: yojona and 9 guests