Material_NoEffectIfNoColliderShape.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. # Test case ID : C5296614
  7. # Test Case Title : Check that unless you assign a shape to a physX collider component,
  8. # the material assigned to it does not take affect
  9. # fmt: off
  10. class Tests:
  11. # level
  12. enter_game_mode = ("Entered game mode", "Failed to enter game mode")
  13. exit_game_mode = ("Exited game mode", "Couldn't exit game mode")
  14. collider_1_found = ("collider_1 was found", "collider_1 was not found")
  15. collider_2_found = ("collider_2 was found", "collider_2 was not found")
  16. ball_1_found = ("ball_1 was found", "ball_1 was not found")
  17. ball_1_gravity = ("ball_1 gravity is disabled", "ball_1 gravity is enabled")
  18. ball_1_collision = ("ball_1 collided with collider_1", "ball_1 passed through collider_1")
  19. ball_2_found = ("ball_2 was found", "ball_2 was not found")
  20. ball_2_gravity = ("ball_2 gravity is disabled", "ball_2 gravity is enabled")
  21. ball_2_collision = ("ball_2 passed through collider_2", "ball_2 collided with collider_2")
  22. trigger_1_found = ("trigger_1 was found", "trigger_1 was not found")
  23. trigger_2_found = ("trigger_2 was found", "trigger_2 was not found")
  24. # fmt: on
  25. def Material_NoEffectIfNoColliderShape():
  26. """
  27. Summary:
  28. Runs an automated test to verify that unless you assign a shape to a PhysX collider component,
  29. the material assigned to it does not take affect
  30. Level Description:
  31. 4 colliders named "collider_1", "collider_2", "ball_1" and "ball_2", all with PhysX Collider component.
  32. collider_1 has no shape assigned to it, but collider_2 has box shape.
  33. ball_1 and ball_2 have sphere shape, PhysX Rigid Body component, gravity disabled and initial linear velocity
  34. of 20 m/s on Y axis.
  35. Each ball is positioned in front of its respective collider.
  36. Expected Behavior:
  37. The balls are supposed to move towards the colliders.
  38. Ball_1 should pass through collider_1 WITHOUT collision, and enter trigger_1.
  39. Ball_2 should collide with collider_2 and NOT enter trigger_2.
  40. Test Steps:
  41. 1) Load the level
  42. 2) Enter game mode
  43. 3) Setup entities
  44. 4) Wait for balls to collide with colliders and/or triggers
  45. 5) Report results
  46. 6) Exit game mode
  47. 7) Close editor
  48. """
  49. import os
  50. import sys
  51. from editor_python_test_tools.utils import Report
  52. from editor_python_test_tools.utils import TestHelper as helper
  53. import azlmbr.legacy.general as general
  54. import azlmbr.bus
  55. # Constants
  56. TIME_OUT = 2.0
  57. def get_test(entity_name, suffix):
  58. return Tests.__dict__[entity_name + suffix]
  59. class Entity:
  60. def __init__(self, name):
  61. self.name = name
  62. self.validate_ID()
  63. def validate_ID(self):
  64. self.id = general.find_game_entity(self.name)
  65. found_tuple = get_test(self.name, "_found")
  66. Report.critical_result(found_tuple, self.id.IsValid())
  67. class Ball(Entity):
  68. def __init__(self, name, collider, trigger):
  69. Entity.__init__(self, name)
  70. self.collider = collider
  71. self.trigger = trigger
  72. self.collided_with_collider = False
  73. self.collided_with_trigger = False
  74. self.validate_gravity()
  75. self.setup_collision_handler()
  76. def validate_gravity(self):
  77. gravity_enabled = azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "IsGravityEnabled", self.id)
  78. gravity_tuple = get_test(self.name, "_gravity")
  79. Report.critical_result(gravity_tuple, not gravity_enabled)
  80. def collider_hit(self, args):
  81. colliding_entity_id = args[0]
  82. if colliding_entity_id.Equal(self.id):
  83. Report.info(self.name + " collided with " + self.collider.name)
  84. self.collided_with_collider = True
  85. def trigger_hit(self, args):
  86. colliding_entity_id = args[0]
  87. if colliding_entity_id.Equal(self.id):
  88. Report.info(self.name + " collided with " + self.trigger.name)
  89. self.collided_with_trigger = True
  90. def setup_collision_handler(self):
  91. self.collider.handler = azlmbr.physics.CollisionNotificationBusHandler()
  92. self.collider.handler.connect(self.collider.id)
  93. self.collider.handler.add_callback("OnCollisionBegin", self.collider_hit)
  94. self.trigger.handler = azlmbr.physics.TriggerNotificationBusHandler()
  95. self.trigger.handler.connect(self.trigger.id)
  96. self.trigger.handler.add_callback("OnTriggerEnter", self.trigger_hit)
  97. def both_balls_have_moved():
  98. return ball_1.collided_with_trigger and ball_2.collided_with_collider
  99. helper.init_idle()
  100. # 1) Load the level
  101. helper.open_level("Physics", "Material_NoEffectIfNoColliderShape")
  102. # 2) Enter game mode
  103. helper.enter_game_mode(Tests.enter_game_mode)
  104. # 3) Setup entities
  105. collider_1 = Entity("collider_1")
  106. collider_2 = Entity("collider_2")
  107. trigger_1 = Entity("trigger_1")
  108. trigger_2 = Entity("trigger_2")
  109. ball_1 = Ball("ball_1", collider_1, trigger_1)
  110. ball_2 = Ball("ball_2", collider_2, trigger_2)
  111. # 4) Wait for balls to collide
  112. helper.wait_for_condition(both_balls_have_moved, TIME_OUT)
  113. # 5) Report results
  114. Report.result(Tests.ball_1_collision, not ball_1.collided_with_collider and ball_1.collided_with_trigger)
  115. Report.result(Tests.ball_2_collision, ball_2.collided_with_collider and not ball_2.collided_with_trigger)
  116. # 6) Exit game mode
  117. helper.exit_game_mode(Tests.exit_game_mode)
  118. if __name__ == "__main__":
  119. from editor_python_test_tools.utils import Report
  120. Report.start_test(Material_NoEffectIfNoColliderShape)