test_joints.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. extends Test
  2. const OPTION_JOINT_TYPE = "Joint Type/%s Joint (%d)"
  3. const OPTION_TEST_CASE_BODIES_COLLIDE = "Test case/Attached bodies collide"
  4. const OPTION_TEST_CASE_WORLD_ATTACHMENT = "Test case/No parent body"
  5. const OPTION_TEST_CASE_DYNAMIC_ATTACHMENT = "Test case/Parent body is dynamic (no gravity)"
  6. const OPTION_TEST_CASE_DESTROY_BODY = "Test case/Destroy attached body"
  7. const OPTION_TEST_CASE_CHANGE_POSITIONS = "Test case/Set body positions after added to scene"
  8. const BOX_SIZE = Vector3(1.0, 1.0, 1.0)
  9. var _update_joint = false
  10. var _selected_joint = null
  11. var _bodies_collide = false
  12. var _world_attachement = false
  13. var _dynamic_attachement = false
  14. var _destroy_body = false
  15. var _change_positions = false
  16. var _joint_types = {}
  17. func _ready():
  18. var options = $Options
  19. var joints = $Joints
  20. for joint_index in joints.get_child_count():
  21. var joint_node = joints.get_child(joint_index)
  22. joint_node.visible = false
  23. var joint_name = String(joint_node.name)
  24. var joint_short = joint_name.substr(0, joint_name.length() - 5)
  25. var option_name = OPTION_JOINT_TYPE % [joint_short, joint_index + 1]
  26. options.add_menu_item(option_name)
  27. _joint_types[option_name] = joint_node
  28. options.add_menu_item(OPTION_TEST_CASE_BODIES_COLLIDE, true, false)
  29. options.add_menu_item(OPTION_TEST_CASE_WORLD_ATTACHMENT, true, false)
  30. options.add_menu_item(OPTION_TEST_CASE_DYNAMIC_ATTACHMENT, true, false)
  31. options.add_menu_item(OPTION_TEST_CASE_DESTROY_BODY, true, false)
  32. options.add_menu_item(OPTION_TEST_CASE_CHANGE_POSITIONS, true, false)
  33. options.connect(&"option_selected", Callable(self, "_on_option_selected"))
  34. options.connect(&"option_changed", Callable(self, "_on_option_changed"))
  35. _selected_joint = _joint_types.values()[0]
  36. _update_joint = true
  37. func _process(_delta):
  38. if _update_joint:
  39. _update_joint = false
  40. await _create_joint()
  41. $LabelJointType.text = "Joint Type: " + String(_selected_joint.name)
  42. func _input(event):
  43. var key_event = event as InputEventKey
  44. if key_event and not key_event.pressed:
  45. var joint_index = key_event.keycode - KEY_1
  46. if joint_index >= 0 and joint_index < _joint_types.size():
  47. _selected_joint = _joint_types.values()[joint_index]
  48. _update_joint = true
  49. func _on_option_selected(option):
  50. if _joint_types.has(option):
  51. _selected_joint = _joint_types[option]
  52. _update_joint = true
  53. func _on_option_changed(option, checked):
  54. match option:
  55. OPTION_TEST_CASE_BODIES_COLLIDE:
  56. _bodies_collide = checked
  57. _update_joint = true
  58. OPTION_TEST_CASE_WORLD_ATTACHMENT:
  59. _world_attachement = checked
  60. _update_joint = true
  61. OPTION_TEST_CASE_DYNAMIC_ATTACHMENT:
  62. _dynamic_attachement = checked
  63. _update_joint = true
  64. OPTION_TEST_CASE_DESTROY_BODY:
  65. _destroy_body = checked
  66. _update_joint = true
  67. OPTION_TEST_CASE_CHANGE_POSITIONS:
  68. _change_positions = checked
  69. _update_joint = true
  70. func _create_joint():
  71. cancel_timer()
  72. var root = $Objects
  73. while root.get_child_count():
  74. var last_child_index = root.get_child_count() - 1
  75. var last_child = root.get_child(last_child_index)
  76. root.remove_child(last_child)
  77. last_child.queue_free()
  78. var child_body = create_rigidbody_box(BOX_SIZE, true)
  79. if _change_positions:
  80. root.add_child(child_body)
  81. child_body.transform.origin = Vector3(0.0, -1.5, 0.0)
  82. else:
  83. child_body.transform.origin = Vector3(0.0, -1.5, 0.0)
  84. root.add_child(child_body)
  85. var parent_body = null
  86. if not _world_attachement:
  87. parent_body = create_rigidbody_box(BOX_SIZE, true)
  88. if _dynamic_attachement:
  89. parent_body.gravity_scale = 0.0
  90. child_body.gravity_scale = 0.0
  91. else:
  92. parent_body.freeze = true
  93. if _change_positions:
  94. root.add_child(parent_body)
  95. parent_body.transform.origin = Vector3(0.0, 1.5, 0.0)
  96. else:
  97. parent_body.transform.origin = Vector3(0.0, 1.5, 0.0)
  98. root.add_child(parent_body)
  99. var joint = _selected_joint.duplicate()
  100. joint.visible = true
  101. joint.set_exclude_nodes_from_collision(not _bodies_collide)
  102. root.add_child(joint)
  103. if parent_body:
  104. joint.set_node_a(joint.get_path_to(parent_body))
  105. joint.set_node_b(joint.get_path_to(child_body))
  106. if _destroy_body:
  107. await start_timer(0.5).timeout
  108. if is_timer_canceled():
  109. return
  110. child_body.queue_free()