biplan.gd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. class_name Biplan
  2. extends SeatedVehicle
  3. const PLANE_FORWARD := Vector3.BACK # z increases when moving forward
  4. @export var thrust_power: float = 3500.0
  5. @export var turn_to_torque: float = 1000.0
  6. @export var move_to_pitch: float = 2000.0
  7. @export var wing_resistance: float = 2500.0
  8. @export var horizontal_foil_resistance: float = 125.0
  9. @export var vertical_foil_resistance: float = 250.0
  10. @export var lift: float = 50.0
  11. var target_torque: float = 0.0
  12. var target_pitch: float = 0.0
  13. var target_rotation := Vector3.ZERO
  14. var target_thrust := Vector3.ZERO
  15. @onready var HorizontalFoil : Node3D = $HorizontalFoil
  16. @onready var VerticalFoil : Node3D = $VerticalFoil
  17. func trigger_thrust(activate: bool) -> void:
  18. if activate:
  19. target_thrust = PLANE_FORWARD * thrust_power
  20. else:
  21. target_thrust = Vector3.ZERO
  22. func trigger_direction(dir: Vector2) -> void:
  23. target_torque = dir.x * turn_to_torque
  24. target_pitch = dir.y * move_to_pitch
  25. func get_free_seat() -> Node3D:
  26. return $DrivingSeat
  27. func _physics_process(_delta) -> void:
  28. # Command part
  29. _apply_plane_rotation()
  30. _apply_plane_thrust()
  31. # Air simulation part
  32. if is_sleeping():
  33. return
  34. _apply_wing_resistance()
  35. _apply_foil_resistance()
  36. _apply_lift()
  37. func _apply_plane_rotation() -> void:
  38. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  39. apply_torque(torque)
  40. func _apply_plane_thrust() -> void:
  41. var force: Vector3 = transform.basis * target_thrust
  42. apply_central_force(force)
  43. func _apply_wing_resistance() -> void:
  44. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  45. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  46. var wing_force = transform.basis * local_wing_force
  47. apply_central_force(wing_force)
  48. func _apply_foil_resistance() -> void:
  49. _apply_horizontal_foil_resistance()
  50. _apply_vertical_foil_resistance()
  51. func _apply_horizontal_foil_resistance() -> void:
  52. var foil_position : Vector3 = HorizontalFoil.get_position()
  53. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  54. var local_foil_force = Vector3.UP * -horizontal_foil_resistance * vertical_speed
  55. apply_force(local_foil_force, foil_position)
  56. func _apply_vertical_foil_resistance() -> void:
  57. var foil_position : Vector3 = VerticalFoil.get_position()
  58. var horizontal_speed = linear_velocity.dot(transform.basis * Vector3.RIGHT)
  59. var local_foil_force = Vector3.RIGHT * -vertical_foil_resistance * horizontal_speed
  60. apply_force(local_foil_force, foil_position)
  61. func _apply_lift() -> void:
  62. var forward_speed = linear_velocity.dot(transform.basis * PLANE_FORWARD)
  63. if forward_speed < 0.0 :
  64. forward_speed = 0.0
  65. var local_lift_force = Vector3.UP * lift * forward_speed
  66. var lift_force = transform.basis * local_lift_force
  67. apply_central_force(lift_force)
  68. func _on_dir_changed(dir: Vector2) -> void:
  69. trigger_direction(dir)
  70. func _on_main_action(pressed: bool) -> void:
  71. trigger_thrust(pressed)