ShapeCollider_CylinderShapeCollides.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 : C24308873
  7. # Test Case Title : Check that cylinder shape collider collides with terrain
  8. # A cylinder is suspended slightly over PhysX Terrain to check that it collides when dropped
  9. # fmt: off
  10. class Tests():
  11. enter_game_mode = ("Entered game mode", "Failed to enter game mode")
  12. find_cylinder = ("Cylinder entity found", "Cylinder entity not found")
  13. create_terrain = ("Terrain entity created successfully", "Failed to create Terrain Entity")
  14. find_terrain = ("Terrain entity found", "Terrain entity not found")
  15. add_physx_shape_collider = ("Added PhysX Shape Collider", "Failed to add PhysX Shape Collider")
  16. add_box_shape = ("Added Box Shape", "Failed to add Box Shape")
  17. cylinder_above_terrain = ("Cylinder position above ground", "Cylinder is not above the ground")
  18. time_out = ("No time out occurred", "A time out occurred, please validate level setup")
  19. touched_ground = ("Touched ground before time out", "Did not touch ground before time out")
  20. exit_game_mode = ("Exited game mode", "Couldn't exit game mode")
  21. # fmt: on
  22. def ShapeCollider_CylinderShapeCollides():
  23. """
  24. Summary:
  25. Runs a test to make sure that a PhysX Rigid Body and Cylinder Shape Collider can successfully collide with a PhysX Terrain entity.
  26. Level Description:
  27. A cylinder with rigid body and collider is positioned above a PhysX terrain.
  28. Expected Outcome:
  29. Once game mode is entered, the cylinder should fall toward and collide with the terrain.
  30. Steps:
  31. 1) Open level and create terrain Entity.
  32. 2) Enter Game Mode.
  33. 3) Retrieve entities and positions
  34. 4) Wait for cylinder to collide with terrain OR time out
  35. 5) Exit game mode
  36. 6) Close the editor
  37. :return:
  38. """
  39. import os
  40. import sys
  41. from editor_python_test_tools.editor_entity_utils import EditorEntity
  42. import azlmbr.legacy.general as general
  43. import azlmbr.bus
  44. import azlmbr.math as math
  45. from editor_python_test_tools.utils import Report
  46. from editor_python_test_tools.utils import TestHelper as helper
  47. from consts.physics import PHYSX_SHAPE_COLLIDER
  48. # Global time out
  49. TIME_OUT = 1.0
  50. # 1) Open level
  51. helper.init_idle()
  52. helper.open_level("Physics", "ShapeCollider_CylinderShapeCollides")
  53. # Create terrain entity
  54. terrain = EditorEntity.create_editor_entity_at([30.0, 30.0, 33.96], "Terrain")
  55. terrain.add_component("PhysX Static Rigid Body")
  56. Report.result(Tests.create_terrain, terrain.id.IsValid())
  57. terrain.add_component(PHYSX_SHAPE_COLLIDER)
  58. Report.result(Tests.add_physx_shape_collider, terrain.has_component(PHYSX_SHAPE_COLLIDER))
  59. box_shape_component = terrain.add_component("Box Shape")
  60. Report.result(Tests.add_box_shape, terrain.has_component("Box Shape"))
  61. box_shape_component.set_component_property_value("Box Shape|Box Configuration|Dimensions",
  62. math.Vector3(1024.0, 1024.0, 0.01))
  63. # 2)Enter game mode
  64. helper.enter_game_mode(Tests.enter_game_mode)
  65. # 3) Retrieve entities and positions
  66. cylinder_id = general.find_game_entity("PhysX_Cylinder")
  67. Report.critical_result(Tests.find_cylinder, cylinder_id.IsValid())
  68. terrain_id = general.find_game_entity("Terrain")
  69. Report.critical_result(Tests.find_terrain, terrain_id.IsValid())
  70. cylinder_pos = azlmbr.components.TransformBus(azlmbr.bus.Event, "GetWorldTranslation", cylinder_id)
  71. #Cylinder position is 64,84,35
  72. terrain_pos = azlmbr.components.TransformBus(azlmbr.bus.Event, "GetWorldTranslation", terrain_id)
  73. Report.info_vector3(cylinder_pos, "Cylinder:")
  74. Report.info_vector3(terrain_pos, "Terrain:")
  75. Report.critical_result(
  76. Tests.cylinder_above_terrain,
  77. (cylinder_pos.z - terrain_pos.z) > 0.5,
  78. "Please make sure the cylinder entity is set above the terrain",
  79. )
  80. # Enable gravity (just in case it is not enabled)
  81. if not azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "IsGravityEnabled", cylinder_id):
  82. azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "SetGravityEnabled", cylinder_id, True)
  83. class TouchGround:
  84. value = False
  85. # Collision event handler
  86. def on_collision_begin(args):
  87. other_id = args[0]
  88. if other_id.Equal(terrain_id):
  89. Report.info("Touched ground")
  90. TouchGround.value = True
  91. # Assign event handler to cylinder
  92. handler = azlmbr.physics.CollisionNotificationBusHandler()
  93. handler.connect(cylinder_id)
  94. handler.add_callback("OnCollisionBegin", on_collision_begin)
  95. # 4) Wait for the cylinder to hit the ground OR time out
  96. test_completed = helper.wait_for_condition((lambda: TouchGround.value), TIME_OUT)
  97. Report.critical_result(Tests.time_out, test_completed)
  98. Report.result(Tests.touched_ground, TouchGround.value)
  99. # 5) Exit game mode
  100. helper.exit_game_mode(Tests.exit_game_mode)
  101. if __name__ == "__main__":
  102. from editor_python_test_tools.utils import Report
  103. Report.start_test(ShapeCollider_CylinderShapeCollides)