biplan.gd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 foil_resistance: float = 250.0
  9. @export var lift: float = 25.0
  10. var target_torque: float = 0.0
  11. var target_pitch: float = 0.0
  12. var target_rotation := Vector3.ZERO
  13. var target_thrust := Vector3.ZERO
  14. @onready var VerticalFoil : Node3D = $VerticalFoil
  15. func trigger_thrust(activate: bool) -> void:
  16. if activate:
  17. target_thrust = PLANE_FORWARD * thrust_power
  18. else:
  19. target_thrust = Vector3.ZERO
  20. func trigger_direction(dir: Vector2) -> void:
  21. target_torque = dir.x * turn_to_torque
  22. target_pitch = dir.y * move_to_pitch
  23. func get_free_seat() -> Node3D:
  24. return $DrivingSeat
  25. func _physics_process(_delta) -> void:
  26. # Command part
  27. _apply_plane_rotation()
  28. _apply_plane_thrust()
  29. # Air simulation part
  30. if is_sleeping():
  31. return
  32. _apply_wing_resistance()
  33. _apply_foil_resistance()
  34. _apply_lift()
  35. transform = transform.orthonormalized()
  36. func _apply_plane_rotation() -> void:
  37. var torque: Vector3 = transform.basis * Vector3(target_pitch, 0.0, target_torque)
  38. apply_torque(torque)
  39. func _apply_plane_thrust() -> void:
  40. var force: Vector3 = transform.basis * target_thrust
  41. apply_central_force(force)
  42. func _apply_wing_resistance() -> void:
  43. var vertical_speed = linear_velocity.dot(transform.basis * Vector3.UP)
  44. var local_wing_force = Vector3.UP * -wing_resistance * vertical_speed
  45. var wing_force = transform.basis * local_wing_force
  46. apply_central_force(wing_force)
  47. func _apply_foil_resistance() -> void:
  48. var foil_position : Vector3 = VerticalFoil.get_position()
  49. var horizontal_speed = linear_velocity.dot(transform.basis * Vector3.RIGHT)
  50. var local_foil_force = Vector3.RIGHT * -foil_resistance * horizontal_speed
  51. apply_force(local_foil_force, foil_position)
  52. func _apply_lift() -> void:
  53. var forward_speed = linear_velocity.dot(transform.basis * PLANE_FORWARD)
  54. if forward_speed < 0.0 :
  55. forward_speed = 0.0
  56. var local_lift_force = Vector3.UP * lift * forward_speed
  57. var lift_force = transform.basis * local_lift_force
  58. apply_central_force(lift_force)
  59. func _on_dir_changed(dir: Vector2) -> void:
  60. trigger_direction(dir)
  61. func _on_main_action(pressed: bool) -> void:
  62. trigger_thrust(pressed)