0 votes

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?

Godot version 3.5.1
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.