FrictionPerTriangleTest.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Tests/Test.h>
  6. #include <Jolt/Physics/Collision/ContactListener.h>
  7. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  8. class FrictionPerTriangleTest : public Test, public ContactListener
  9. {
  10. public:
  11. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, FrictionPerTriangleTest)
  12. // Description of the test
  13. virtual const char *GetDescription() const override
  14. {
  15. return "Demonstrates how you can use a contact listener and your own material definition to get friction and restitution per triangle or per sub shape of a compound shape.\n"
  16. "Friction increases while going down the slope. Box should eventually stop.";
  17. }
  18. // See: Test
  19. virtual void Initialize() override;
  20. /// A custom material implementation that stores its own friction and restitution
  21. /// Note: Make sure you set PhysicsMaterial::sDefault to something your application understands (explicitly check PhysicsMaterial::sDefault to prevent casting to the wrong type in sGetFrictionAndRestitution)
  22. class MyMaterial : public PhysicsMaterialSimple
  23. {
  24. public:
  25. // Note: Not implementing serialization because we don't serialize this material in this example!
  26. /// Constructor
  27. MyMaterial(const string_view &inName, ColorArg inColor, float inFriction, float inRestitution) : PhysicsMaterialSimple(inName, inColor), mFriction(inFriction), mRestitution(inRestitution) { }
  28. float mFriction;
  29. float mRestitution;
  30. };
  31. /// Extract custom friction and restitution from a body and sub shape ID
  32. static void sGetFrictionAndRestitution(const Body &inBody, const SubShapeID &inSubShapeID, float &outFriction, float &outRestitution);
  33. /// Calculates and overrides friction and restitution settings for a contact between two bodies
  34. static void sOverrideContactSettings(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings);
  35. // If this test implements a contact listener, it should be returned here
  36. virtual ContactListener *GetContactListener() override { return this; }
  37. // See: ContactListener
  38. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  39. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  40. };