- Edited
Hello. I want to be able to connect two 3D points with a cylinder.
This topic helped me in general, but I made a test project to be sure everything works. This project simply randomizes points in X, Y, Z in between -2 and 2. Sometimes resulting rotation is invalid, so I want to know how to fix that.
I am changing CSGCylinder's length for each new connection, then I am updating it with look_at
and rotate_object_local
functions, as it was done in original topic. There's the important part:
func update_connection(ao, bo):
$connection.transform.origin = midpoint(ao, bo)
$connection.height = ao.distance_to(bo)
$connection.look_at(bo, Vector3(0, 1, 0))
$connection.rotate_object_local(Vector3(1,0,0), -PI/2)
This code places the connection cylinder between ao
and bo
points, transform origins for marker A and marker B, calculates distance and changes cylinder length, and then applies look_at to turn the connection.
Animation showing mixed results.
Screenshot:
UPD: I think the reason behind the broken rotations is this error:
Up vector and direction between node origin and target are aligned, look_at() failed.
It probably happened, preventing the rotation.