Collider_PxMeshConvexMeshCollides.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Test case ID : C4982803
  6. Test Case Title : Verify that when the shape Physics Asset is selected,
  7. PxMesh option gets enabled and a Px Mesh can be selected and assigned to the object
  8. """
  9. # fmt: off
  10. class Tests():
  11. create_collider_entity = ("Entity created successfully", "Failed to create Entity")
  12. mesh_added = ("Added Mesh component", "Failed to add Mesh component")
  13. physx_collider_added = ("Added PhysX Mesh Collider component", "Failed to add PhysX Mesh Collider component")
  14. physx_rigid_body_added = ("Added PhysX Rigid Body component", "Failed to add PhysX Rigid Body component")
  15. assign_fbx_mesh = ("Assigned fbx mesh", "Failed to assign fbx mesh")
  16. create_terrain = ("Terrain entity created successfully", "Failed to create Terrain Entity")
  17. add_physx_shape_collider = ("Added PhysX Shape Collider", "Failed to add PhysX Shape Collider")
  18. add_box_shape = ("Added Box Shape", "Failed to add Box Shape")
  19. enter_game_mode = ("Entered game mode", "Failed to enter game mode")
  20. exit_game_mode = ("Exited game mode", "Failed to exit game mode")
  21. test_collision = ("Entity collided with terrain", "Failed to collide with terrain")
  22. # fmt: on
  23. def Collider_PxMeshConvexMeshCollides():
  24. """
  25. Summary:
  26. Load level with Entity above ground having PhysX Mesh Collider, PhysX Rigid Body and Mesh components.
  27. Verify that the entity falls on the ground and collides with the terrain when fbx convex mesh is added to collider.
  28. Expected Behavior:
  29. The entity falls on the ground and collides with the terrain.
  30. Test Steps:
  31. 1) Load the level
  32. 2) Create test entity above the ground
  33. 3) Add PhysX Mesh Collider, PhysX Rigid Body and Mesh components.
  34. 4) Select fbx convex mesh in PhysX Mesh Collider component.
  35. 5) Create terrain entity with physx terrain
  36. 6) Enter game mode
  37. 7) Verify that the entity falls on the ground and collides with the terrain.
  38. Note:
  39. - This test file must be called from the Open 3D Engine Editor command terminal
  40. - Any passed and failed tests are written to the Editor.log file.
  41. Parsing the file or running a log_monitor are required to observe the test results.
  42. :return: None
  43. """
  44. # Builtins
  45. import os
  46. # Helper Files
  47. from editor_python_test_tools.editor_entity_utils import EditorEntity
  48. from editor_python_test_tools.utils import Report
  49. from editor_python_test_tools.utils import TestHelper as helper
  50. from editor_python_test_tools.asset_utils import Asset
  51. from consts.physics import PHYSX_SHAPE_COLLIDER
  52. from consts.physics import PHYSX_MESH_COLLIDER
  53. import azlmbr.math as math
  54. import editor_python_test_tools.hydra_editor_utils as hydra
  55. # Open 3D Engine Imports
  56. import azlmbr
  57. import azlmbr.legacy.general as general
  58. # Constants
  59. MESH_ASSET_PATH = os.path.join("assets", "Physics", "Collider_PxMeshConvexMeshCollides", "spherebot", "r0-b_body.fbx.pxmesh")
  60. TIMEOUT = 2.0
  61. # 1) Load the level
  62. hydra.open_base_level()
  63. # 2) Create test entity
  64. collider = EditorEntity.create_editor_entity_at([512.0, 512.0, 33.0], "Collider")
  65. Report.result(Tests.create_collider_entity, collider.id.IsValid())
  66. # 3) Add PhysX Mesh Collider, PhysX Rigid Body and Mesh components.
  67. collider_component = collider.add_component(PHYSX_MESH_COLLIDER)
  68. Report.result(Tests.physx_collider_added, collider.has_component(PHYSX_MESH_COLLIDER))
  69. collider.add_component("PhysX Dynamic Rigid Body")
  70. Report.result(Tests.physx_rigid_body_added, collider.has_component("PhysX Dynamic Rigid Body"))
  71. collider.add_component("Mesh")
  72. Report.result(Tests.mesh_added, collider.has_component("Mesh"))
  73. # 4) Select fbx convex mesh in PhysX Mesh Collider component.
  74. mesh_asset = Asset.find_asset_by_path(MESH_ASSET_PATH)
  75. collider_component.set_component_property_value("Shape Configuration|Asset|PhysX Mesh", mesh_asset.id)
  76. mesh_asset.id = collider_component.get_component_property_value("Shape Configuration|Asset|PhysX Mesh")
  77. Report.result(Tests.assign_fbx_mesh, mesh_asset.get_path().lower() == MESH_ASSET_PATH.replace(os.sep, "/").lower())
  78. # 5) Create terrain entity with physx terrain
  79. terrain = EditorEntity.create_editor_entity_at([512.0, 512.0, 31.0], "Terrain")
  80. terrain.add_component("PhysX Static Rigid Body")
  81. Report.result(Tests.create_terrain, terrain.id.IsValid())
  82. terrain.add_component(PHYSX_SHAPE_COLLIDER)
  83. Report.result(Tests.add_physx_shape_collider, terrain.has_component(PHYSX_SHAPE_COLLIDER))
  84. box_shape_component = terrain.add_component("Box Shape")
  85. Report.result(Tests.add_box_shape, terrain.has_component("Box Shape"))
  86. box_shape_component.set_component_property_value("Box Shape|Box Configuration|Dimensions",
  87. math.Vector3(1024.0, 1024.0, 1.0))
  88. # 6) Enter game mode
  89. helper.enter_game_mode(Tests.enter_game_mode)
  90. # 7) Verify that the entity falls on the ground and collides with the terrain
  91. class Collider:
  92. id = general.find_game_entity("Collider")
  93. touched_ground = False
  94. terrain_id = general.find_game_entity("Terrain")
  95. def on_collision_begin(args):
  96. other_id = args[0]
  97. if other_id.Equal(terrain_id):
  98. Report.info("Touched ground")
  99. Collider.touched_ground = True
  100. handler = azlmbr.physics.CollisionNotificationBusHandler()
  101. handler.connect(Collider.id)
  102. handler.add_callback("OnCollisionBegin", on_collision_begin)
  103. helper.wait_for_condition(lambda: Collider.touched_ground, TIMEOUT)
  104. Report.result(Tests.test_collision, Collider.touched_ground)
  105. # 8) Exit game mode
  106. helper.exit_game_mode(Tests.exit_game_mode)
  107. if __name__ == "__main__":
  108. from editor_python_test_tools.utils import Report
  109. Report.start_test(Collider_PxMeshConvexMeshCollides)