FrictionPerTriangleTest.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Tests/Test.h>
  5. #include <Jolt/Physics/Collision/ContactListener.h>
  6. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  7. /// 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
  8. class FrictionPerTriangleTest : public Test, public ContactListener
  9. {
  10. public:
  11. JPH_DECLARE_RTTI_VIRTUAL(FrictionPerTriangleTest)
  12. // See: Test
  13. virtual void Initialize() override;
  14. /// A custom material implementation that stores its own friction and restitution
  15. /// 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)
  16. class MyMaterial : public PhysicsMaterialSimple
  17. {
  18. public:
  19. // Note: Not implementing serialization because we don't serialize this material in this example!
  20. /// Constructor
  21. MyMaterial(const string_view &inName, ColorArg inColor, float inFriction, float inRestitution) : PhysicsMaterialSimple(inName, inColor), mFriction(inFriction), mRestitution(inRestitution) { }
  22. float mFriction;
  23. float mRestitution;
  24. };
  25. /// Extract custom friction and restitution from a body and sub shape ID
  26. static void sGetFrictionAndRestitution(const Body &inBody, const SubShapeID &inSubShapeID, float &outFriction, float &outRestitution);
  27. /// Calculates and overrides friction and restitution settings for a contact between two bodies
  28. static void sOverrideContactSettings(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings);
  29. // If this test implements a contact listener, it should be returned here
  30. virtual ContactListener *GetContactListener() override { return this; }
  31. // See: ContactListener
  32. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  33. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  34. };