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