FrictionPerTriangleTest.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. /// This test demonstrates how you can use a contact listener and your own material definition to get friction and restitution per triangle or sub shape of a compound shape
  9. class FrictionPerTriangleTest : public Test, public ContactListener
  10. {
  11. public:
  12. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, FrictionPerTriangleTest)
  13. // See: Test
  14. virtual void Initialize() override;
  15. /// A custom material implementation that stores its own friction and restitution
  16. /// 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)
  17. class MyMaterial : public PhysicsMaterialSimple
  18. {
  19. public:
  20. // Note: Not implementing serialization because we don't serialize this material in this example!
  21. /// Constructor
  22. MyMaterial(const string_view &inName, ColorArg inColor, float inFriction, float inRestitution) : PhysicsMaterialSimple(inName, inColor), mFriction(inFriction), mRestitution(inRestitution) { }
  23. float mFriction;
  24. float mRestitution;
  25. };
  26. /// Extract custom friction and restitution from a body and sub shape ID
  27. static void sGetFrictionAndRestitution(const Body &inBody, const SubShapeID &inSubShapeID, float &outFriction, float &outRestitution);
  28. /// Calculates and overrides friction and restitution settings for a contact between two bodies
  29. static void sOverrideContactSettings(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings);
  30. // If this test implements a contact listener, it should be returned here
  31. virtual ContactListener *GetContactListener() override { return this; }
  32. // See: ContactListener
  33. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  34. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  35. };