problem in using transform

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

I try to move a cube towards the direction that it is facing, Here’s the code:

extends KinematicBody

func _ready():
	set_fixed_process(true)
	
func _fixed_process(delta):
	var transform = get_transform()
	
	if Input.is_action_pressed("ui_accept"):
		move(Vector3(transform[2].x,0,transform[2].z)) 

I try to use both transform and transform.basis, they produce the same result, I want to know the difference between them. What does each row of the martix represents? I just tried and know that [2] would do the job. The tutorial in the docs skip some of the explanation in the end.

:bust_in_silhouette: Reply From: Jatz

You can’t just use a transform row in a matrix and expect it to work.
The matrix uses all three rows to store things like, position, scale and rotation,

KinematicBody is a physics object.
Most of the time that means you have to use global transformations to act on the body.

move is a function of KinematicBody, the paramater it needs has to be in global coordinates, something like this will move the body in the z axis:

var forward = 1.0
move(get_global_transform().basis.xform(Vector3(0,0,forward)))
:bust_in_silhouette: Reply From: Zylann

You can use -transform.basis.z to get the forward vector.
You can see basis as a representation of the 3 spatial axes of the transformation.
Godot uses the OpenGL convention, so X is right, Y is up and Z is backwards (notice the - sign).

Now you can use just z fine, but when dealing with nodes such as Camera they are actually facing -z (you can verify this by looking at a Camera in a scene, the pink indicator shows where the camera looks at towards its transform).