ActiveEdgesTest.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/ActiveEdgesTest.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Jolt/Geometry/Triangle.h>
  9. #include <Layers.h>
  10. JPH_IMPLEMENT_RTTI_VIRTUAL(ActiveEdgesTest)
  11. {
  12. JPH_ADD_BASE_CLASS(ActiveEdgesTest, Test)
  13. }
  14. void ActiveEdgesTest::Initialize()
  15. {
  16. const float cWidth = 5.0f;
  17. const float cLength = 10.0f;
  18. // Setings for a frictionless box
  19. Ref<BoxShape> box_shape = new BoxShape(Vec3(1.0f, 1.0f, 1.0f), cDefaultConvexRadius);
  20. BodyCreationSettings box_settings(box_shape, Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  21. box_settings.mFriction = 0.0f;
  22. box_settings.mLinearDamping = 0.0f;
  23. box_settings.mAllowSleeping = false;
  24. // Create various triangle strips
  25. TriangleList triangles;
  26. for (int angle = -90; angle <= 90; angle++)
  27. {
  28. // Under which normal we want to place the block
  29. Vec3 desired_normal = angle < 0? Vec3(0, 1, -1).Normalized() : Vec3(0, 1, 0);
  30. float best_dot = -FLT_MAX;
  31. // Place segments
  32. float x = cWidth * angle;
  33. Vec3 v1(x, 0.0f, -0.5f * cLength);
  34. Vec3 v2(x + cWidth, 0.0f, -0.5f * cLength);
  35. for (int total_angle = 0, cur_segment = 0; abs(total_angle) <= 90 && cur_segment < 90; total_angle += angle, ++cur_segment)
  36. {
  37. // Determine positions of end of this segment
  38. float total_angle_rad = DegreesToRadians(float(total_angle));
  39. Quat rotation = Quat::sRotation(Vec3::sAxisX(), total_angle_rad);
  40. Vec3 delta = cLength * rotation.RotateAxisZ();
  41. Vec3 v3 = v1 + delta;
  42. Vec3 v4 = v2 + delta;
  43. // Check if this segment is the best segment to place the dynamic block on
  44. Vec3 normal = (v3 - v1).Cross(v2 - v1).Normalized();
  45. float dot = normal.Dot(desired_normal);
  46. if (dot > best_dot)
  47. {
  48. best_dot = dot;
  49. box_settings.mPosition = (v1 + v2 + v3 + v4) / 4 + normal;
  50. box_settings.mRotation = rotation;
  51. }
  52. // Add segment
  53. triangles.push_back(Triangle(v1, v3, v4));
  54. triangles.push_back(Triangle(v1, v4, v2));
  55. // Add segment mirrored in Z axis
  56. if (cur_segment != 0)
  57. {
  58. Vec3 flip(1, 1, -1);
  59. triangles.push_back(Triangle(flip * v1, flip * v4, flip * v3));
  60. triangles.push_back(Triangle(flip * v1, flip * v2, flip * v4));
  61. }
  62. // The end of the segment will be the start for the next iteration
  63. v1 = v3;
  64. v2 = v4;
  65. }
  66. // Place box on best segment
  67. Body &body = *mBodyInterface->CreateBody(box_settings);
  68. mBodyInterface->AddBody(body.GetID(), EActivation::Activate);
  69. // For convex segments give the block a push
  70. if (angle >= 0)
  71. body.SetLinearVelocity(Vec3(0, 0, 2.0f));
  72. }
  73. // Mesh
  74. BodyCreationSettings mesh_settings(new MeshShapeSettings(triangles), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  75. mesh_settings.mFriction = 0.0f;
  76. Body &mesh = *mBodyInterface->CreateBody(mesh_settings);
  77. mBodyInterface->AddBody(mesh.GetID(), EActivation::DontActivate);
  78. }