Hello everyone,

I building a AI setup and i trying to let him localise the nearest mine. When it's done, it must build something near it. I need for that to match the direction.

When i make the differance distance, in grid scale, i have (19, 1) but i need something like vector2(1, 0) or (vector2.right), or another direction depending of the differance result.
How can i make matching this ?

Not with clamp or range_lerp, i think.

  • You just have to check the sign of each axis to get the direction.

    var dir = Vector2(0, 0)
    if test.x > 0:
       dir.x = 1
    else:
       dir.x = -1
    # etc

    But you might have to normalize test first, and then use an epsilon so that diagonals are handled properly.

    There may be a step function that would make this easier to quantize rather than having a bunch of if statements.

What does 19, 1 refer to. That doesn't seem like a logical direction.

Is it more logical for you if...

var test = map.world_to_map(mine.position) - map.world_to_map(hq.position) 
print(test) --- (19, 1)

Do you mean to get the square root of the larger value of your vector?

No, i want the AI find especially where is the nearest mine and which direction the building to build near the mine must be. For example, if you see my screenshot, the building (furnace) must be placed between the mine and the headquarter, just to the left to the mine.

In another map, for example, if the mine is upper to the headquarter, the AI must know the mine is hupper, so the furnace will be just downer to the mine.

After, the AI pick one of the points to build the furnace.

Like this:

You just have to check the sign of each axis to get the direction.

var dir = Vector2(0, 0)
if test.x > 0:
   dir.x = 1
else:
   dir.x = -1
# etc

But you might have to normalize test first, and then use an epsilon so that diagonals are handled properly.

There may be a step function that would make this easier to quantize rather than having a bunch of if statements.

Yes, normalized give me an interresting result, i have (0.998618, 0.052559) as result.

I don't recognize all technical terms yet, what is an espilon ? If i apply a round of the result, i have (1, 0), kind of exactly what i looking for. round() is an espilon ?

    In this case round() may be enough, but I would test it on some values that are close to diagonal.

    Thank you, Megalomaniak, for the documentation.

    @cybereality, i have tried with somes coordinates close to diagonal position and i have (1 ,1) (negative or positive) by the round output. This is enough to what i want to do.

    Thank you.