FrictionPerTriangleTest.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/General/FrictionPerTriangleTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  8. #include <Jolt/Geometry/Triangle.h>
  9. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  10. #include <Layers.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(FrictionPerTriangleTest)
  12. {
  13. JPH_ADD_BASE_CLASS(FrictionPerTriangleTest, Test)
  14. }
  15. void FrictionPerTriangleTest::Initialize()
  16. {
  17. const int num_sections = 5;
  18. const float section_size = 50.0f;
  19. // Create a strip of triangles
  20. TriangleList triangles;
  21. for (int z = 0; z <= num_sections; ++z)
  22. {
  23. float z1 = section_size * (z - 0.5f * num_sections);
  24. float z2 = z1 + section_size;
  25. Float3 v1 = Float3(-100.0f, 0, z1);
  26. Float3 v2 = Float3(100.0f, 0, z1);
  27. Float3 v3 = Float3(-100.0f, 0, z2);
  28. Float3 v4 = Float3(100.0f, 0, z2);
  29. triangles.push_back(Triangle(v1, v3, v4, z));
  30. triangles.push_back(Triangle(v1, v4, v2, z));
  31. }
  32. // Create materials with increasing friction
  33. PhysicsMaterialList materials;
  34. for (uint i = 0; i <= num_sections; ++i)
  35. {
  36. float friction = float(i) / float(num_sections);
  37. materials.push_back(new MyMaterial("Friction " + ConvertToString(friction), Color::sGetDistinctColor(i), friction, 0.0f));
  38. }
  39. // A ramp
  40. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new MeshShapeSettings(triangles, std::move(materials)), RVec3::sZero(), Quat::sRotation(Vec3::sAxisX(), 0.2f * JPH_PI), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  41. // A box with friction 1 that slides down the ramp
  42. Ref<BoxShape> box_shape = new BoxShape(Vec3(2.0f, 2.0f, 2.0f), cDefaultConvexRadius, new MyMaterial("Box Friction 1", Color::sYellow, 1.0f, 0.0f));
  43. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box_shape, RVec3(0, 60.0f, -75.0f), Quat::sRotation(Vec3::sAxisX(), 0.2f * JPH_PI), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  44. }
  45. void FrictionPerTriangleTest::sGetFrictionAndRestitution(const Body &inBody, const SubShapeID &inSubShapeID, float &outFriction, float &outRestitution)
  46. {
  47. // Get the material that corresponds to the sub shape ID
  48. const PhysicsMaterial *material = inBody.GetShape()->GetMaterial(inSubShapeID);
  49. if (material == PhysicsMaterial::sDefault)
  50. {
  51. // This is the default material, use the settings from the body (note all bodies in our test have a material so this should not happen)
  52. outFriction = inBody.GetFriction();
  53. outRestitution = inBody.GetRestitution();
  54. }
  55. else
  56. {
  57. // If it's not the default material we know its a material that we created so we can cast it and get the values
  58. const MyMaterial *my_material = static_cast<const MyMaterial *>(material);
  59. outFriction = my_material->mFriction;
  60. outRestitution = my_material->mRestitution;
  61. }
  62. }
  63. void FrictionPerTriangleTest::sOverrideContactSettings(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  64. {
  65. // Get the custom friction and restitution for both bodies
  66. float friction1, friction2, restitution1, restitution2;
  67. sGetFrictionAndRestitution(inBody1, inManifold.mSubShapeID1, friction1, restitution1);
  68. sGetFrictionAndRestitution(inBody2, inManifold.mSubShapeID2, friction2, restitution2);
  69. // Use the default formulas for combining friction and restitution
  70. ioSettings.mCombinedFriction = sqrt(friction1 * friction2);
  71. ioSettings.mCombinedRestitution = max(restitution1, restitution2);
  72. }
  73. void FrictionPerTriangleTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  74. {
  75. sOverrideContactSettings(inBody1, inBody2, inManifold, ioSettings);
  76. }
  77. void FrictionPerTriangleTest::OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  78. {
  79. sOverrideContactSettings(inBody1, inBody2, inManifold, ioSettings);
  80. }