Get coordinates in relation to two nodes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stillinthe90s
:warning: Old Version Published before Godot 3 was released.

Having limited knowledge about 3D in general, I’m trying to get coordinates in relation to node B, from node A’s point of view (in this case left side).

The plan is to make a crude avoidance mechanism which is why this needs to work in both example 1 & 2 and regardless which way node B or A is facing.

I failed to find a solution from reading the documentation, but I suspect a bit of simple math would do the job.

:bust_in_silhouette: Reply From: Zylann

You tagged your question as 3D, but you are showing 2D examples. I know it’s for illustration, but in 3D you are missing the Y coordinate, which determines where is up and down. If you don’t mind, then your problem is 2D only using X and Z.

You can ignore the direction of both points, however if you do that you’ll have a problem, because you need to figure out where to go, and continue going the same way over time. Left? Right? Only the direction of at least one of the points can fix that.

In 2D, finding the position of the red cross can be done this way:

# Assuming A and B are 2D coordinates

# Get direction from A to B
var direction = (B - A).normalized()

# Rotate 90 degrees... left or right?
# To decide that, make the angle positive or negative.
var lateral_direction = direction.rotated(PI/2.0)

# Calculate red cross position
var cross_pos = B + lateral_direction * some_distance

Transposing that in 3D depends on how you want it to behave with a third dimension, I would say that the up vector (Y) will come into play then, because the ground might not be flat.

Thanks!
Adding a Vector3 to your solution seemed to do the trick.

var lat_dir = dir.rotated(Vector3(0,1,0),PI/2.0)

The game world is pretty flat so y-axis should not be a problem, although I am yet to test it with actual navigation.

stillinthe90s | 2017-04-20 14:35