How to limit rotation on a moving AND rotating platform?

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

I am creating a turret on a platform. Think it as a battleship turret.

The turret can rotate, but it must be limited to max 2*PI/3 radians, clockwise and counterclocwise

The very problem is the turret won’t stop at 2*PI/3 radians relative to the platform IF the platform rotate beyond 0 radian or PI radiant

With rotation limit > PI radians, the clamp() method returns bizarre behavior. It is only good if platform’s rotation is 0 radian or PI radian.

In addition, the turret also lerp-ing, so using collision body to blockade the rotation is useless

I am already using KinematicBody2D or Sprite node to solve this problem, but I’m stuck now.

This is my base code without any attempt to limit rotation of the turret:

var turret_rotation_speed = PI/4 #as in radian/second 
func _physics_process(delta):
   custom_global.main_target_position = get_global_mouse_position()
   target_vector_angle = (custom_global.main_target_position - global_position).angle()
    var r = global_rotation
#define the permitted max angle change (the delta)
    var angle_delta = turret_rotation_speed * delta
#smoothen the angle by interpolation
    target_vector_angle = lerp_angle(r, target_vector_angle, 1.0)
#clamp turret angle relative to target position and permitted max angle change 
    target_vector_angle = clamp(target_vector_angle, r - angle_delta, r + angle_delta)
#rotate turret based on ship's vs target angle	
   global_rotation = target_vector_angle

Any idea to solve the rotation limiter?