tiny_plane.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. class_name TinyPlane
  2. extends RigidBody3D
  3. @export var thrust_power: float = 10000.0
  4. @export var turn_to_torque: float = 2000.0
  5. @export var move_to_pitch: float = 4000.0
  6. @export var wing_resistance: float = 10.0
  7. @export var lift: float = 500.0
  8. var target_torque: float = 0.0
  9. var target_pitch: float = 0.0
  10. var target_rotation := Vector3.ZERO
  11. var target_thrust := Vector3.ZERO
  12. var _current_commander: LocalInput = null
  13. func trigger_thrust(activate: bool) -> void:
  14. if activate:
  15. target_thrust = Vector3.FORWARD * thrust_power
  16. else:
  17. target_thrust = Vector3.ZERO
  18. func trigger_direction(dir: Vector2) -> void:
  19. target_torque = -dir.x * turn_to_torque
  20. target_pitch = -dir.y * move_to_pitch
  21. ## Make the vehicle responds to driver commands
  22. func drive_with(commander: LocalInput) -> void:
  23. commander.dir_changed.connect(_on_dir_changed)
  24. commander.main_action.connect(_on_main_action)
  25. _current_commander = commander
  26. func get_out() -> void:
  27. _current_commander.dir_changed.disconnect(_on_dir_changed)
  28. _current_commander.main_action.disconnect(_on_main_action)
  29. func _physics_process(delta: float) -> void:
  30. _apply_plane_rotation()
  31. _apply_plane_thrust()
  32. _apply_wing_resistance()
  33. _apply_lift()
  34. func _apply_plane_rotation() -> void:
  35. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  36. apply_torque(torque)
  37. func _apply_plane_thrust() -> void:
  38. var force: Vector3 = transform.basis * target_thrust
  39. apply_central_force(force)
  40. func _apply_wing_resistance() -> void:
  41. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  42. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  43. var wing_force = transform.basis * local_wing_force
  44. if _current_commander != null:
  45. print("= = =")
  46. print("Vertical speed : ", vertical_speed)
  47. print("Wing force local :", local_wing_force)
  48. print("Wing force : ", wing_force)
  49. apply_central_force(wing_force)
  50. func _apply_lift() -> void:
  51. var forward_speed = linear_velocity.dot(transform.basis * Vector3.FORWARD)
  52. if forward_speed < 0.0 :
  53. forward_speed = 0.0
  54. var local_lift_force = Vector3.UP * lift * forward_speed
  55. var lift_force = transform.basis * local_lift_force
  56. if _current_commander != null:
  57. print("- - -")
  58. print("Forward speed : ", forward_speed)
  59. print("Lift force local :", local_lift_force)
  60. print("Lift force : ", lift_force)
  61. apply_central_force(lift_force)
  62. func _on_dir_changed(dir: Vector2) -> void:
  63. trigger_direction(dir)
  64. func _on_main_action(pressed: bool) -> void:
  65. trigger_thrust(pressed)