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

0 favourites
  • 4 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • 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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I would look at using the built in "pick nearest / furthest" command ...

    edit ... .capx?dl=0

  • 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

  • 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.

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