Physics_WorldBodyBusWorksOnEditorComponents.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 : C29032500
  7. # Test Case Title : Check that WorldRequestBus works with editor components
  8. # fmt: off
  9. class Tests():
  10. find_staticshapebox = ("Found StaticShapeBox", "Failed to find StaticShapeBox")
  11. find_staticsphere = ("Found StaticSphere", "Failed to find StaticSphere")
  12. find_staticbox = ("Found StaticBox", "Failed to find StaticBox")
  13. find_staticcapsule = ("Found StaticCapsule", "Failed to find StaticCapsule")
  14. find_staticmesh = ("Found StaticMesh", "Failed to find StaticMesh")
  15. find_shapebox = ("Found ShapeBox", "Failed to find ShapeBox")
  16. find_sphere = ("Found Sphere", "Failed to find Sphere")
  17. find_box = ("Found Box", "Failed to find Box")
  18. find_capsule = ("Found Capsule", "Failed to find Capsule")
  19. find_mesh = ("Found Mesh", "Failed to find Mesh")
  20. aabb_staticshapebox = ("Correct AABB for StaticShapeBox", "Incorrect AABB for StaticShapeBox")
  21. aabb_staticsphere = ("Correct AABB for StaticSphere", "Incorrect AABB for StaticSphere")
  22. aabb_staticbox = ("Correct AABB for StaticBox", "Incorrect AABB for StaticBox")
  23. aabb_staticcapsule = ("Correct AABB for StaticCapsule", "Incorrect AABB for StaticCapsule")
  24. aabb_staticmesh = ("Correct AABB for StaticMesh", "Incorrect AABB for StaticMesh")
  25. aabb_shapebox = ("Correct AABB for ShapeBox", "Incorrect AABB for ShapeBox")
  26. aabb_sphere = ("Correct AABB for Sphere", "Incorrect AABB for Sphere")
  27. aabb_box = ("Correct AABB for Box", "Incorrect AABB for Box")
  28. aabb_capsule = ("Correct AABB for Capsule", "Incorrect AABB for Capsule")
  29. aabb_mesh = ("Correct AABB for Mesh", "Incorrect AABB for Mesh")
  30. raycast_staticshapebox = ("Correct raycast for StaticShapeBox", "Incorrect raycast for StaticShapeBox")
  31. raycast_staticsphere = ("Correct raycast for StaticSphere", "Incorrect raycast for StaticSphere")
  32. raycast_staticbox = ("Correct raycast for StaticBox", "Incorrect raycast for StaticBox")
  33. raycast_staticcapsule = ("Correct raycast for StaticCapsule", "Incorrect raycast for StaticCapsule")
  34. raycast_staticmesh = ("Correct raycast for StaticMesh", "Incorrect raycast for StaticMesh")
  35. raycast_shapebox = ("Correct raycast for ShapeBox", "Incorrect raycast for ShapeBox")
  36. raycast_sphere = ("Correct raycast for Sphere", "Incorrect raycast for Sphere")
  37. raycast_box = ("Correct raycast for Box", "Incorrect raycast for Box")
  38. raycast_capsule = ("Correct raycast for Capsule", "Incorrect raycast for Capsule")
  39. raycast_mesh = ("Correct raycast for Mesh", "Incorrect raycast for Mesh")
  40. # fmt: on
  41. def Physics_WorldBodyBusWorksOnEditorComponents():
  42. r"""
  43. Summary:
  44. Runs a test to make sure that a WorldBodyBus works property for components
  45. Level Description:
  46. - Dynamic
  47. - Sphere: Sphere with Rigid body
  48. - Box: Box with Rigid body
  49. - Capsule: Capsule with Rigid body
  50. - Mesh: Sedan car Mesh with Rigid body
  51. - ShapeBox: Shape Collider component + Box with rigidBody
  52. - Static
  53. - StaticSphere: Sphere with only Coollider component
  54. - StaticBox: Box with only Coollider component
  55. - StaticCapsule: Capsule with only Coollider component
  56. - StaticMesh: Sedan car Mesh with only Coollider component
  57. - StaticShapeBox: Only Shape Collider component + Box
  58. TopDown view:
  59. ____
  60. [!] o [ ] ( ) (____)
  61. ShapeBox Sphere Box Capsule Mesh
  62. ____
  63. [!] o [ ] ( ) (____)
  64. StaticShapeBox StaticSphere StaticBox StaticCapsule StaticMesh
  65. Expected Outcome:
  66. Checks AABB and RayCast functions of WorldBodyBus against the All the entities in the level
  67. :return:
  68. """
  69. import os
  70. import sys
  71. import azlmbr.legacy.general as general
  72. import azlmbr.bus
  73. import math
  74. from editor_python_test_tools.utils import Report
  75. from editor_python_test_tools.utils import TestHelper as helper
  76. from editor_python_test_tools.utils import vector3_str, aabb_str
  77. AABB_THRESHOLD = 0.01 # Entities won't move in the simulation
  78. helper.init_idle()
  79. helper.open_level("Physics", "Physics_WorldBodyBusWorksOnEditorComponents")
  80. def create_aabb(aabb_min_tuple, aabb_max_tuple):
  81. return azlmbr.math.Aabb_CreateFromMinMax(azlmbr.math.Vector3(aabb_min_tuple[0], aabb_min_tuple[1], aabb_min_tuple[2]),
  82. azlmbr.math.Vector3(aabb_max_tuple[0], aabb_max_tuple[1], aabb_max_tuple[2]))
  83. class EntityData:
  84. def __init__(self, name, target_aabb):
  85. self.name = name
  86. self.target_aabb = target_aabb
  87. def get_test_tuple_for_entity(testprefix, entity_name):
  88. return Tests.__dict__[testprefix.lower() + "_" + entity_name.lower()]
  89. ENTITY_DATA = [ EntityData("ShapeBox", create_aabb((509.82, 523.08, 32.81), (510.82, 524.08, 33.81))),
  90. EntityData("Sphere", create_aabb((509.82, 526.39, 32.81), (510.82, 527.39, 33.81))),
  91. EntityData("Box", create_aabb((509.82, 529.66, 32.81), (510.82, 530.66, 33.81))),
  92. EntityData("Capsule", create_aabb((510.07, 533.70, 32.81), (510.57, 534.20, 33.81))),
  93. EntityData("Mesh", create_aabb((509.48, 536.30, 33.31), (511.16, 540.38, 34.38))),
  94. EntityData("StaticShapeBox", create_aabb((512.08, 523.08, 32.81), (513.08, 524.08, 33.81))),
  95. EntityData("StaticSphere", create_aabb((512.08, 526.39, 32.81), (513.08, 527.39, 33.81))),
  96. EntityData("StaticBox", create_aabb((512.08, 529.66, 32.81), (513.08, 530.66, 33.81))),
  97. EntityData("StaticCapsule", create_aabb((512.33, 533.70, 32.81), (512.83, 534.20, 33.81))),
  98. EntityData("StaticMesh", create_aabb((511.74, 536.30, 33.31), (513.42, 540.38, 34.38))) ] # AABB data obtained by observation
  99. for entity_data in ENTITY_DATA:
  100. entity_id = general.find_editor_entity(entity_data.name);
  101. Report.result(get_test_tuple_for_entity("find", entity_data.name), entity_id.IsValid())
  102. if entity_id.IsValid():
  103. # AABB test
  104. aabb = azlmbr.physics.WorldBodyRequestBus(azlmbr.bus.Event, "GetAabb", entity_id)
  105. Report.info("%s AABB -> %s" % (entity_data.name, aabb_str(aabb)))
  106. Report.info("%s expected AABB -> %s" % (entity_data.name, aabb_str(entity_data.target_aabb)))
  107. is_expected_aabb_size = aabb.min.IsClose(entity_data.target_aabb.min, AABB_THRESHOLD) and aabb.max.IsClose(entity_data.target_aabb.max, AABB_THRESHOLD)
  108. Report.result(get_test_tuple_for_entity("aabb", entity_data.name), is_expected_aabb_size)
  109. # Raycast test
  110. entity_pos = azlmbr.components.TransformBus(azlmbr.bus.Event, "GetWorldTM", entity_id).GetPosition()
  111. raycast_request = azlmbr.physics.RayCastRequest()
  112. raycast_request.Start = entity_pos.Add(azlmbr.math.Vector3(0.0, 0.0, 100.0))
  113. raycast_request.Distance = 500.0
  114. raycast_request.Direction = azlmbr.math.Vector3(0.0, 0.0, -1.0)
  115. result = azlmbr.physics.WorldBodyRequestBus(azlmbr.bus.Event, "RayCast", entity_id, raycast_request)
  116. if result:
  117. # Following line crashes due to a hydra bug, use distance for now
  118. # has_hit = ragdoll_id.Equal(result.EntityId)
  119. has_hit = result.Distance > 0.1 and math.isclose(result.Position.x, entity_pos.x) and math.isclose(result.Position.y, entity_pos.y)
  120. Report.info("Hit: %s" % vector3_str(result.Position))
  121. Report.result(get_test_tuple_for_entity("raycast", entity_data.name), has_hit)
  122. else:
  123. Report.failure(get_test_tuple_for_entity("raycast", entity_data.name))
  124. if __name__ == "__main__":
  125. from editor_python_test_tools.utils import Report
  126. Report.start_test(Physics_WorldBodyBusWorksOnEditorComponents)