How to disable Gimbal lock?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By abhaskumarsinha
:warning: Old Version Published before Godot 3 was released.
func _process(delta):
if Input.is_action_pressed("a"):
    get_node(".").rotate_y(deg2rad(5))
if Input.is_action_pressed("d"):
    get_node(".").rotate_y(deg2rad(-5))
if Input.is_action_pressed("w"):
    get_node(".").rotate_x(deg2rad(5))
if Input.is_action_pressed("s"):
    get_node(".").rotate_x(deg2rad(-5))

Having problems with Gimbal Lock, how to fix that?

When I rotate myself to upwards 45 degree and sideways 45 degrees, then I the camera also rotates, I want to fix it, so that camera does’t rotate on its view axis.

I am unsure if I understand fully where your problem is.

Is the camera a child of a Kinetic/RigidBody ?
Then it will rotate with the movement of the body.

You could move the camera out of that body and adjust its position/angle separately.

Something like this (untested) inside the camera node:

extends Camera

var body

const CAMDIST = 20

func _ready():
	body=get_node("../Body") #init with position of body (edit to adjust node name/path)
	set_fixed_process(true)
	
func _fixed_process(delta):
	var bt=body.get_transform()
	var ct=get_transform()
    var lookdir = bt.basis.z #get rotated z-vector of body
    lookdir.y = 0 #don't look up/down
	ct.origin=bt.origin #camera position = body position
	ct = ct.looking_at(lookdir,Vector3(0,1,0))
	set_transform(ct)

wombatstampede | 2017-10-04 10:59