首頁>Game>source

我正在對飞機物件實施俯仰,偏航和侧倾,但是当旋轉物體(俯仰,横摇或偏航)時,它開始缓慢旋轉,但是旋轉運動很快就非常快。 這很刺耳.我不確定如何使它更平滑。

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

玩家輸入代碼:

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)

飞機運動指令碼:

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)

我如何使旋轉運動更平稳? 註意:這是一款街機遊戲,對物理的複雜解決方案並不十分感兴趣。

註2:如果要測試,可以在此處簽出 https://github.com/balmeidaa/godot_sky_aces

最新回復
  • 6月前
    1 #

    虽然是全新的 對於遊戲開發,我有一个問题所在.如果我理解正確,運動應该是一致的,但是運動会在每个增量處逐个增量地加速。

    迴想一下 _physics_process delta 是固定的秒數,預設為1/60秒.让我们以推力為例.当 ui_up 首先按下控製按钮,將1/60(〜0.01666)添加到推力中,因此,假設推力開始值為0.5,則新值變為0.51666,然後再经過1/60秒(当 _physics_process 稱為下一个),新值將變為0.53333,依此類推。 1秒後,推力值為1.5,並且每个增量仍以增量增加,因此它一直在加速。

    如果在按下控制元件時處於恒定的速度變化之後,則可能需要記錄第一次按下按钮的次數,增加固定的預定量(您可以尝試找到合適的量),然後記錄按钮何時 被釋放並再次减少相同的量.換句话說,根本不要使用增量.這樣,您可以避免加速.希望對您有所帮助. :)

相似問題

  • 3d meshes:Unity網格仅渲染一組三角形
  • unity:結合钳位大小和一些爆炸力時遇到問题