首页>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

最新回答
  • 2021-1-2
    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:结合钳位大小和一些爆炸力时遇到问题