1
\$\begingroup\$

I'm implementing pitch, yaw and roll on airplane object, but when rotating the object (either pitch, roll or yaw), it starts rotating slowly but then the rotating movement goes awfully fast very quickly. it's quite jarring. i'm not sure how to make more smooth.

The issues:

  • I think the problem lies in the acceleration when the object rotates, i want to be an uniform speed
  • Other problem from this code, it's that if want to change the rotating direction it's slow to change, takes a while to rotate to the opposite direction

The player input code:

extends Node
const MAX_CAM_ANGLE = 30
var pitch_dir = 0
var yaw_dir = 0
var roll_dir = 0
var thrust = 0.5
var input = ''
func _ready():
    $DebugStats.add_property($Plane, "transform:origin", "round")
    $DebugStats.add_property($Plane, "force", "round")
    $DebugStats.add_property($Plane, "torque", "round")
    $DebugStats.add_property(self, "pitch_dir", "round")
    $DebugStats.add_property(self, "input", "")

func _physics_process(delta):
    process_inputs(delta)
    process_movement(delta)
    
func process_inputs(delta):
    if Input.is_action_pressed("p1_fire"):
        $Plane.fire_weapon()
        input = 'fire'
    if Input.is_action_pressed("ui_up"):
        thrust += delta
        input = 'accel'
    if Input.is_action_pressed("ui_down"):
        thrust -= delta
        input = 'slow'
    if Input.is_action_pressed("p1_pitch_up"):
        pitch_dir += delta
        input = 'up'
    if Input.is_action_pressed("p1_pitch_down"):
        pitch_dir -= delta
        input = 'down'
    if Input.is_action_pressed("p1_roll_left"):
        roll_dir += delta
        input = 'yaw left'
    if Input.is_action_pressed("p1_roll_right"):
        roll_dir -= delta
        input = 'yaw right'
    if Input.is_action_pressed("p1_yaw_left"):
        yaw_dir += delta
        input = 'yaw left'
    if Input.is_action_pressed("p1_yaw_right"):
        yaw_dir -= delta
        input = 'yaw right'

    if Input.is_action_pressed("ui_quit"):
        get_tree().quit()
    
#TODO Adjust values to make movement more smooth
func process_movement(delta):
     thrust = clamp(thrust, 0.2, 1)
     pitch_dir  = clamp(pitch_dir, -.6, .6)
     yaw_dir  = clamp(yaw_dir, -.6, .6)
     roll_dir  = clamp(roll_dir, -.6, .6)
     $Plane.calc_force(thrust, pitch_dir, roll_dir, yaw_dir)

The plane movement script:

extends RigidBody

const v3 = Vector3(0, 0, 0)
const scalar_z = Vector3(0, 0, -1)
var force = v3
var torque = v3
var drag = v3
var lift = v3
var thrust = v3

const MAX_THRUST_TURN = 150
const MAX_THRUST = 1  # temporal value just to debug the rotating problem
const MAX_CAM_ANGLE = 30
const DRAG_CONST = 1 # temporal value just to debug the rotating problem

# Calculates flying speed and direction
func calc_force(thrust, pitch_dir, roll_dir, yaw_dir):
    var speed = MAX_THRUST * (thrust)
    var drag_coef = DRAG_CONST / MAX_THRUST / MAX_THRUST  
    thrust = transform.basis.z * (-speed)
    drag = transform.basis.z * drag_coef
    lift = transform.basis.y * drag_coef
    
    force = thrust + drag + lift
    var pitch = global_transform.basis.x * pitch_dir * MAX_THRUST_TURN
    var yaw = global_transform.basis.z * yaw_dir * MAX_THRUST_TURN
    var roll = global_transform.basis.z * roll_dir * MAX_THRUST_TURN
    torque = pitch + yaw + roll

# Applies all force at once in the airplane object
func _integrate_forces(state):
    add_central_force(force)
    add_torque(torque)

How i can make the rotating movement more smoother? Note: this is an arcade game, not really interested in a complex solution for the physics.

Note 2: if you want to test it, you can check out here https://github.com/balmeidaa/godot_sky_aces

\$\endgroup\$
2
  • \$\begingroup\$ I'm not sure about this, but I think that anything called in physics_process(delta) will have delta implemented. I don't think you have to call delta in process_movements and process_input. I don't think that's your problem, but it might be worth a shot. \$\endgroup\$
    – Millard
    Nov 30, 2020 at 20:30
  • \$\begingroup\$ @Millard i been thinkering with the code, and i think it has to do with how to force is applied, from the player perspective its seem that suddenly the rotation force was applied instead of a more uniform push, so i did try to limit the rotation speed, and now is super slow to rotate. \$\endgroup\$
    – Progs
    Nov 30, 2020 at 22:20

1 Answer 1

1
\$\begingroup\$

While completely new to game development, I have an idea what the problem is. If I understand correctly, the movement should be uniform, but the movement accelerates at every delta by delta.

Recall that for _physics_process delta is a fixed fraction of a second, by default 1/60 seconds. Let's take thrust as an example. When the ui_up control is first pressed, 1/60 (~0.01666) is added to thrust, so assuming the thrust start value of 0.5, the new value becomes 0.51666, and after another 1/60 seconds (when _physics_process is called next), the new value becomes 0.53333, etc. After 1 second, the thrust value is at 1.5 and still increasing by delta at every delta, so it keeps accelerating.

If you are after a constant speed change when the control is pressed, you probably want to record the first button press, increase by a fixed predetermined amount (you can experiment to find the right amount), and then record when the button is released and decrease by the same amount again. In other words, don't use delta at all. That way, you avoid the acceleration. I hope that helps. :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .