GDNative C++ how do I get the transform of KinematicBody?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KramchayDig

how do I get the transform?
GDScript → Transform(transform.basis,$“mesh_root”.transform * Vector3(0,0,0))
C++ → ???

:bust_in_silhouette: Reply From: Zylann

Your GDScript example looks quite complicated if all you want is to really get the transform of the KinematicBody.
Assuming your script is on that KinematicBody, its transform is obtained with just… well, this:

transform

Which in GDNative would be:

get_transform()

But if you want to use the code you posted specifically, which you wrote as:

Transform(transform.basis,$"mesh_root".transform * Vector3(0,0,0))

Then in GDNative, it would be:

Transform(get_transform().get_basis(), Object::cast_to<Spatial>(get_node("mesh_root"))->get_transform() * Vector3(0, 0, 0))

The Vector3(0,0,0) at the end looks like a poor way of getting origin, so after changing that and handling crash cases properly, it becomes:

Spatial *mesh_root = Object::cast_to<Spatial>(get_node("mesh_root"));
ERR_FAIL_COND(mesh_root == nullptr);
Transform(get_transform().get_basis(), mesh_root->get_transform().get_origin());

Keep in mind this is not related to KinematicBody, as transform is a property all Spatial nodes have.
Besides, what you wrote is not really getting the transform of the body. It is instead getting the position of mesh_root relatively to its parent body, with the rotation of the body itself. So if the body moves, what you get will not move.