TerrainPhysicsCollider_MaterialMapping_Works.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. class Tests:
  7. enter_game_mode = (
  8. "Entered Game Mode",
  9. "Failed to enter Game Mode"
  10. )
  11. glass_cube_moves = (
  12. "Glass cube started moving",
  13. "Glass cube has unexpectedly remained stationary"
  14. )
  15. cubes_are_stationary = (
  16. "Cubes have both come to a rest",
  17. "One or both cubes are unexpectedly still moving"
  18. )
  19. exit_game_mode = (
  20. "Exited Game Mode",
  21. "Failed to exit Game Mode"
  22. )
  23. def TerrainPhysicsCollider_MaterialMapping_Works():
  24. """
  25. Summary:
  26. Opens an existing level with a Terrain Layer Spawner split into 2 different surface areas with 2 different Physics
  27. Materials applied. Test verifies that physicalized objects properly interact with the specified Physics Material
  28. mapped surfaces on terrain.
  29. :return: None
  30. """
  31. import azlmbr.legacy.general as general
  32. import azlmbr.bus as bus
  33. import azlmbr.components as components
  34. from editor_python_test_tools.utils import Report
  35. from editor_python_test_tools.utils import TestHelper as helper
  36. def vector_is_close_to_zero(vector):
  37. return abs(vector.x) <= 0.001 and abs(vector.y) <= 0.001 and abs(vector.z) <= 0.001
  38. def is_stationary(entity):
  39. velocity = azlmbr.physics.RigidBodyRequestBus(bus.Event, "GetLinearVelocity", entity)
  40. return vector_is_close_to_zero(velocity)
  41. # Open a level with preconfigured terrain with surfaces mapped to Physics Materials
  42. helper.init_idle()
  43. helper.open_level("Terrain", "TerrainPhysicsCollider_MaterialMapping_Works")
  44. # Enter Game Mode
  45. helper.enter_game_mode(Tests.enter_game_mode)
  46. # Find the needed entities
  47. physics_cube_glass = general.find_game_entity("PhysicsCube_Glass")
  48. physics_cube_rubber = general.find_game_entity("PhysicsCube_Rubber")
  49. # Get the initial positions of each Physics Cube
  50. glass_start = components.TransformBus(bus.Event, "GetWorldTranslation", physics_cube_glass)
  51. rubber_start = components.TransformBus(bus.Event, "GetWorldTranslation", physics_cube_rubber)
  52. # Wait for cube on glass to start moving, then wait for both cubes to become stationary, re-check positions, and
  53. # calculate distance traveled
  54. glass_is_moving = helper.wait_for_condition(lambda: not is_stationary(physics_cube_glass), 3.0)
  55. Report.result(Tests.glass_cube_moves, glass_is_moving)
  56. glass_is_stationary = helper.wait_for_condition(lambda: is_stationary(physics_cube_glass), 30.0)
  57. rubber_is_stationary = helper.wait_for_condition(lambda: is_stationary(physics_cube_rubber), 10.0)
  58. Report.result(Tests.cubes_are_stationary, glass_is_stationary and rubber_is_stationary)
  59. glass_finish = components.TransformBus(bus.Event, "GetWorldTranslation", physics_cube_glass)
  60. rubber_finish = components.TransformBus(bus.Event, "GetWorldTranslation", physics_cube_rubber)
  61. glass_distance = glass_finish.GetDistance(glass_start)
  62. rubber_distance = rubber_finish.GetDistance(rubber_start)
  63. # Report results on distance traveled and validate that the Cube on the glass surface travels further
  64. expected_glass_distance = 10.0
  65. expected_rubber_distance = 1.0
  66. material_interaction = (
  67. f"PhysicsCube_Glass moved {glass_distance:.3f}m, to PhysicsCube_Rubber's {rubber_distance:.3f}m",
  68. f"PhysicsCube_Rubber: Moved {rubber_distance:.3f}m, expected < {expected_rubber_distance}m. "
  69. f"PhysicsCube_Glass: Moved {glass_distance:.3f}m, expected > {expected_glass_distance}m"
  70. )
  71. Report.result(material_interaction, glass_distance > expected_glass_distance and
  72. rubber_distance < expected_rubber_distance)
  73. # Exit Game Mode
  74. helper.exit_game_mode(Tests.exit_game_mode)
  75. if __name__ == "__main__":
  76. from editor_python_test_tools.utils import Report
  77. Report.start_test(TerrainPhysicsCollider_MaterialMapping_Works)