0 votes

I know there is globaltranslation, translate, distanceto.

But is there a way to find translation (x,y,z) coordinates, based off of another node or vector point?

in Engine by (293 points)

1 Answer

0 votes

You mean a relative "translation" vector3?
You substract the x/y/z vectors of each other.

node2.global_transform.origin - node1.global_transform.origin

That is the vector3 which points from node1 to node2 (in global space).

If you need that vector also in local coordinate space then probably i.e. a node1.global_transform.xform_inv(vector3) will convert it into local space.

See here for some info:
https://godotforums.org/discussion/18480/godot-3d-vector-physics-cheat-sheet#latest

by (3,232 points)

xform sounds exactly like what I need, but whenever I try to apply it like you've stated or the documents state, I get one of two errors depending on where it is:

If it's a variable inside a function, I get: Invalid call. Nonexistent function 'xform' in base 'Vector3'.

or

If it's a global variable, I get: Invalid get index 'transform' (on base: 'Nil').

The variable is: var blob = $t1parent/test1.transform.origin.xform(Vector3()) #Just as a test

How do I make this work?

xforms are methods of a Transform or Basis:
http://docs.godotengine.org/en/stable/classes/class_transform.html

Basically, xform takes a local position and translates it into global (or parent node) space.
xform_inv translates a global (or parent) coordinate into a local coordinate.

You can use xform alternatively also for AABB and Planes.

You used transform.origin in your code which results in a Vector3 of the nodes position (inside the parent nodes coordinate space). You can not call xform on that but you could use it as a parameter.

Here's an example that transforms the position of the local vector 0,0,-1 (so -1 on z-axis) according to the current rotation of the node into a global position.

global_transform.xform(Vector3(0,0,-1))

Sorry for taking a while to reply. I've finally had a chance to sit down on this again.

I understand how xform syntax works and I'm sure it works how I need it based off of trying it. But there is a problem with it and I want to make sure I understand this method.

How I was hoping it worked was: When using xform that it essentially makes the target of the node, it's (0,0,0) position in 3D space.

But that doesn't seem to be the case, as when I try to create a movement script based off where the node is in relation to the target, the node spazzes sometimes. I'm trying to understand exactly it works. I'm not the greatest with matrix math.

Here's my example code, no errors pop up, it's just not moving as expected. I'm trying to simply have t1 follow a controlled node/mesh staying 5 units away from it and only moving on the z axis.

func _process(delta): 
    var distance = t1.transform.origin.distance_to(t2.transform.origin)
    var target = t1.global_transform.xform(t2.global_transform.origin)
    print ("target = ", target.z)
    print(distance)
    if target.z > 0:
        t1.transform.origin = t1.transform.origin.linear_interpolate(t1.transform.origin - (Vector3(0,0,distance) - Vector3(0,0,5)),delta)
    if target.z < 0:
        t1.transform.origin = t1.transform.origin.linear_interpolate(t1.transform.origin + (Vector3(0,0,distance) - Vector3(0,0,5)),delta)

Hm, I might not fully understand your code but already the xform for target in the second line looks strange. Normally, you don't pass global positions/coords to an xform.

I use some code in the spaceship demo (see the forum post linked above) to keep the camera behind (so on z-axis) the player. That code uses also interpolation and stays approximately in some distance.
(Watch how the camera follows:)
Space ship demo
This is the code for the camera node. Maybe that helps?

extends Camera

const CAM_DIST = 60.0
const Z_FRONT = -1 #in this game the front side is towards negative Z

onready var player = $"../Player"

var tTarget = Transform()

func _physics_process(delta):
    if current:
        tTarget.origin = player.global_transform.origin + (player.global_transform.basis.z * player.Z_FRONT * -1 * CAM_DIST)

        tTarget = tTarget.looking_at(player.global_transform.origin,player.global_transform.basis.y)

        global_transform = global_transform.interpolate_with(tTarget,delta * 4.0) 

tTarget is the "ideal" position at exactly CAM_DIST (on players z-axis) behind the player. That target is followed "smoothly" by using interpolation.

I love what you've made with the ship, super smooth flying.

The code you posts helps me understand how the math works behind it, which is helpful. But I still can't figure out if I'm misunderstanding xform, or if my math is just off.

I've uploaded a sample of what is going on with my simply prototype, this is what the above code is out putting:

test1

I just want the node chasing to stay on the Z axis and follow the player controlling staying 5 units away on the Z-axis . But sometimes, it either spazzes or it even moves away. Which makes me think, my math is off and/or I don't understand how xform is calculated.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.