TransformedShapeTests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include "PhysicsTestContext.h"
  6. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  7. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  8. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  9. #include <Jolt/Physics/Collision/TransformedShape.h>
  10. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  11. #include <Jolt/Physics/Collision/RayCast.h>
  12. #include <Jolt/Physics/Collision/CastResult.h>
  13. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  14. TEST_SUITE("TransformedShapeTests")
  15. {
  16. TEST_CASE("TestTransformedShape")
  17. {
  18. const Vec3 half_extents(0.5f, 1.0f, 1.5f);
  19. const Vec3 scale(-2, 3, 4);
  20. const Vec3 rtshape_translation(1, 3, 5);
  21. const Quat rtshape_rotation = Quat::sRotation(Vec3(1, 2, 3).Normalized(), 0.25f * JPH_PI);
  22. const RVec3 translation(13, 9, 7);
  23. const Quat rotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI); // A rotation of 90 degrees in order to not shear the shape
  24. PhysicsMaterialSimple *material = new PhysicsMaterialSimple("Test Material", Color::sRed);
  25. // Create a scaled, rotated and translated box
  26. BoxShapeSettings box_settings(half_extents, 0.0f, material);
  27. box_settings.SetEmbedded();
  28. ScaledShapeSettings scale_settings(&box_settings, scale);
  29. scale_settings.SetEmbedded();
  30. RotatedTranslatedShapeSettings rtshape_settings(rtshape_translation, rtshape_rotation, &scale_settings);
  31. rtshape_settings.SetEmbedded();
  32. // Create a body with this shape
  33. PhysicsTestContext c;
  34. Body &body = c.CreateBody(&rtshape_settings, translation, rotation, EMotionType::Static, EMotionQuality::Discrete, 0, EActivation::DontActivate);
  35. // Collect the leaf shape transform
  36. AllHitCollisionCollector<TransformedShapeCollector> collector;
  37. c.GetSystem()->GetNarrowPhaseQuery().CollectTransformedShapes(AABox::sBiggest(), collector);
  38. // Check that there is exactly 1 shape
  39. CHECK(collector.mHits.size() == 1);
  40. TransformedShape &ts = collector.mHits.front();
  41. // Check that we got the leaf shape: box
  42. CHECK(ts.mShape == box_settings.Create().Get());
  43. // Check that its transform matches the transform that we provided
  44. RMat44 calc_transform = RMat44::sRotationTranslation(rotation, translation) * Mat44::sRotationTranslation(rtshape_rotation, rtshape_translation) * RMat44::sScale(scale);
  45. CHECK_APPROX_EQUAL(calc_transform, ts.GetWorldTransform());
  46. // Check that all corner points are in the bounding box
  47. AABox aabox = ts.GetWorldSpaceBounds();
  48. Vec3 corners[] = {
  49. Vec3(-0.99f, -0.99f, -0.99f) * half_extents,
  50. Vec3( 0.99f, -0.99f, -0.99f) * half_extents,
  51. Vec3(-0.99f, 0.99f, -0.99f) * half_extents,
  52. Vec3( 0.99f, 0.99f, -0.99f) * half_extents,
  53. Vec3(-0.99f, -0.99f, 0.99f) * half_extents,
  54. Vec3( 0.99f, -0.99f, 0.99f) * half_extents,
  55. Vec3(-0.99f, 0.99f, 0.99f) * half_extents,
  56. Vec3( 0.99f, 0.99f, 0.99f) * half_extents
  57. };
  58. for (Vec3 corner : corners)
  59. {
  60. CHECK(aabox.Contains(calc_transform * corner));
  61. CHECK(!aabox.Contains(calc_transform * (2 * corner))); // Check that points twice as far away are not in the box
  62. }
  63. // Now pick a point on the box near the edge in local space, determine a raycast that hits it
  64. const Vec3 point_on_box(half_extents.GetX() - 0.01f, half_extents.GetY() - 0.01f, half_extents.GetZ());
  65. const Vec3 normal_on_box(0, 0, 1);
  66. const Vec3 ray_direction_local(1, 1, -1);
  67. // Transform to world space and do the raycast
  68. Vec3 ray_start_local = point_on_box - ray_direction_local;
  69. Vec3 ray_end_local = point_on_box + ray_direction_local;
  70. RVec3 ray_start_world = calc_transform * ray_start_local;
  71. RVec3 ray_end_world = calc_transform * ray_end_local;
  72. Vec3 ray_direction_world = Vec3(ray_end_world - ray_start_world);
  73. RRayCast ray_in_world { ray_start_world, ray_direction_world };
  74. RayCastResult hit;
  75. ts.CastRay(ray_in_world, hit);
  76. // Check the hit result
  77. CHECK_APPROX_EQUAL(hit.mFraction, 0.5f);
  78. CHECK(hit.mBodyID == body.GetID());
  79. CHECK(ts.GetMaterial(hit.mSubShapeID2) == material);
  80. Vec3 world_space_normal = ts.GetWorldSpaceSurfaceNormal(hit.mSubShapeID2, ray_in_world.GetPointOnRay(hit.mFraction));
  81. Vec3 expected_normal = (calc_transform.GetDirectionPreservingMatrix() * normal_on_box).Normalized();
  82. CHECK_APPROX_EQUAL(world_space_normal, expected_normal);
  83. // Reset the transform to identity and check that it worked
  84. ts.SetWorldTransform(RMat44::sIdentity());
  85. CHECK_APPROX_EQUAL(ts.GetWorldTransform(), RMat44::sIdentity());
  86. // Set the calculated world transform again to see if getting/setting a transform is symmetric
  87. ts.SetWorldTransform(calc_transform);
  88. CHECK_APPROX_EQUAL(calc_transform, ts.GetWorldTransform());
  89. }
  90. }