Surface normal to transform

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

I want to set the rotation of a kinematic body to the surface normal from a raycast collision, which is a vector3. How can I turn a vector3 into a transform basis.

Thanks.

:bust_in_silhouette: Reply From: mollusca

Here’s one method, normal is the normal vector:

#Find the axis with the smallest component
var min_ind = 0
var min_axis = abs(normal.x)

if abs(normal.y) < min_axis:
        min_ind = 1
        min_axis = abs(normal.y)
if abs(normal.z) < min_axis:
        min_ind = 2
var right
#Leave the minimum axis in its place, swap the two other to get a vector perpendicular to the normal vector
if min_ind == 0:
    right = Vector3(normal.x, -normal.z, normal.y)
elif min_ind == 1:
    right = Vector3(-normal.z, normal.y, normal.x)
elif min_ind == 2:
    right = Vector3(-normal.y, normal.x, normal.z)

var up = normal.cross(right)
var basis = Matrix3(right, up, normal)