# Incorrect 180 degree rotation

Attention Topic was automatically imported from the old Question2Answer platform.

I am using the following way to rotate an object:

transform.basis = transform.basis.rotated(new, ang*delta)

Where new is roughly equal to the x-axis and ang * delta <0.001
In general, everything works

But if before that there was a rotation along the z axis. When x-axis rotation is 90 degrees or more, the object is rotated 180 degrees along the y-axis.

I have absolutely no idea what’s going on. I would be very grateful for solving the problem

Hi,
can you provide a working example of your problem?
Sounds like some gimbal lock effect.

klaas | 2020-10-03 17:42

Hey. what example do you mean?

tem12qaz | 2020-10-03 17:47

a bit of code with the effect youve descriped that i can throw in godot to have a look

klaas | 2020-10-03 18:14

Sorry for the long answer, I was very busy. There is a lot of code here, but not all

``````vel1.y = -vel.y
vel1.x = 0
vel1.z = 0
``````
``````if vel:
vel = vel.rotated(Vector3(0,1,0), sum_rot_y)
vel1 = vel1.rotated(Vector3(0,1,0), sum_rot_y)

vel = vel.rotated(Vector3(1,0,0), rotation.x)
vel1 = vel1.rotated(Vector3(1,0,0), rotation.x)

vel = vel.rotated(Vector3(0,0,1), rotation.z)
vel1 = vel1.rotated(Vector3(0,0,1), rotation.z)

vel1 = vel1.rotated(Vector3(0,1,0), rotation.y)
vel = vel.rotated(Vector3(0,1,0), rotation.y)

print(vel1.normalized(), rotation.y)
ang1 = vel.angle_to(pre_vel)
if \$RayCast.is_colliding():
normal = \$RayCast.get_collision_normal()
normal = Vector3(stepify(normal.x, 0.01),stepify(normal.y, 0.01),stepify(normal.z, 0.01))
vel1 = vel1.normalized()
vel1 = Vector3(stepify(vel1.x, 0.01),stepify(vel1.y, 0.01),stepify(vel1.z, 0.01))
new = vel1.cross(normal).normalized()

ang = stepify(vel1.angle_to(normal), 0.0000001)
if ang != 0:

if new != Vector3():
print(rotation.y,'          ', new,'          ',ang*delta)
transform.basis = transform.basis.rotated(new, ang*delta)
print(rotation.y,'          ', new,'          ',ang*delta)
vel = vel.rotated(new,ang*delta*5)
ang1 = rotation.y

if abs(rotation.y - ang1) >1:
transform.basis = transform.basis.rotated(new, 3.1416)
else:
normal = 0
ang = 0
``````

tem12qaz | 2020-10-05 15:29