Browse Source

Added Shape::ClearCachedResult

Fixes #955
Jorrit Rouwe 1 year ago
parent
commit
b8012e1ac1
2 changed files with 22 additions and 0 deletions
  1. 4 0
      Jolt/Physics/Collision/Shape/Shape.h
  2. 18 0
      UnitTests/Physics/ShapeTests.cpp

+ 4 - 0
Jolt/Physics/Collision/Shape/Shape.h

@@ -145,6 +145,10 @@ public:
 	/// Create a shape according to the settings specified by this object.
 	virtual ShapeResult				Create() const = 0;
 
+	/// When creating a shape, the result is cached so that calling Create() again will return the same shape.
+	/// If you make changes to the ShapeSettings you need to call this function to clear the cached result to allow Create() to build a new shape.
+	void							ClearCachedResult()													{ mCachedResult.Clear(); }
+
 	/// User data (to be used freely by the application)
 	uint64							mUserData = 0;
 

+ 18 - 0
UnitTests/Physics/ShapeTests.cpp

@@ -357,6 +357,24 @@ TEST_SUITE("ShapeTests")
 		}
 	}
 
+	// Test re-creating shape using the same settings object
+	TEST_CASE("TestClearCachedResult")
+	{
+		// Create a sphere and check radius
+		SphereShapeSettings sphere_settings(1.0f);
+		RefConst<SphereShape> sphere1 = static_cast<const SphereShape *>(sphere_settings.Create().Get().GetPtr());
+		CHECK(sphere1->GetRadius() == 1.0f);
+
+		// Modify radius and check that creating the shape again returns the cached result
+		sphere_settings.mRadius = 2.0f;
+		RefConst<SphereShape> sphere2 = static_cast<const SphereShape *>(sphere_settings.Create().Get().GetPtr());
+		CHECK(sphere2 == sphere1);
+
+		sphere_settings.ClearCachedResult();
+		RefConst<SphereShape> sphere3 = static_cast<const SphereShape *>(sphere_settings.Create().Get().GetPtr());
+		CHECK(sphere3->GetRadius() == 2.0f);
+	}
+
 	// Test submerged volume calculation
 	TEST_CASE("TestGetSubmergedVolume")
 	{