Browse Source

Made math type args (like Vec3Arg) const so that it remains possible to change them to a const reference (#890)

Jorrit Rouwe 1 year ago
parent
commit
c1556df68b

+ 7 - 7
Jolt/Math/MathTypes.h

@@ -17,17 +17,17 @@ class Mat44;
 class DMat44;
 
 // Types to use for passing arguments to functions
-using Vec3Arg = Vec3;
+using Vec3Arg = const Vec3;
 #ifdef JPH_USE_AVX
-	using DVec3Arg = DVec3;
+	using DVec3Arg = const DVec3;
 #else
 	using DVec3Arg = const DVec3 &;
 #endif
-using Vec4Arg = Vec4;
-using UVec4Arg = UVec4;
-using Vec8Arg = Vec8;
-using UVec8Arg = UVec8;
-using QuatArg = Quat;
+using Vec4Arg = const Vec4;
+using UVec4Arg = const UVec4;
+using Vec8Arg = const Vec8;
+using UVec8Arg = const UVec8;
+using QuatArg = const Quat;
 using Mat44Arg = const Mat44 &;
 using DMat44Arg = const DMat44 &;
 

+ 3 - 2
Jolt/Physics/Collision/BroadPhase/QuadTree.cpp

@@ -1330,10 +1330,11 @@ void QuadTree::CastAABox(const AABoxCast &inBox, CastShapeBodyCollector &ioColle
 		JPH_INLINE int				VisitNodes(Vec4Arg inBoundsMinX, Vec4Arg inBoundsMinY, Vec4Arg inBoundsMinZ, Vec4Arg inBoundsMaxX, Vec4Arg inBoundsMaxY, Vec4Arg inBoundsMaxZ, UVec4 &ioChildNodeIDs, int inStackTop)
 		{
 			// Enlarge them by the casted aabox extents
-			AABox4EnlargeWithExtent(mExtent, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
+			Vec4 bounds_min_x = inBoundsMinX, bounds_min_y = inBoundsMinY, bounds_min_z = inBoundsMinZ, bounds_max_x = inBoundsMaxX, bounds_max_y = inBoundsMaxY, bounds_max_z = inBoundsMaxZ;
+			AABox4EnlargeWithExtent(mExtent, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
 
 			// Test 4 children
-			Vec4 fraction = RayAABox4(mOrigin, mInvDirection, inBoundsMinX, inBoundsMinY, inBoundsMinZ, inBoundsMaxX, inBoundsMaxY, inBoundsMaxZ);
+			Vec4 fraction = RayAABox4(mOrigin, mInvDirection, bounds_min_x, bounds_min_y, bounds_min_z, bounds_max_x, bounds_max_y, bounds_max_z);
 
 			// Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
 			return SortReverseAndStore(fraction, mCollector.GetPositiveEarlyOutFraction(), ioChildNodeIDs, &mFractionStack[inStackTop]);

+ 5 - 4
Jolt/Physics/Collision/SortReverseAndStore.h

@@ -15,18 +15,19 @@ JPH_NAMESPACE_BEGIN
 JPH_INLINE int SortReverseAndStore(Vec4Arg inValues, float inMaxValue, UVec4 &ioIdentifiers, float *outValues)
 {
 	// Sort so that highest values are first (we want to first process closer hits and we process stack top to bottom)
-	Vec4::sSort4Reverse(inValues, ioIdentifiers);
+	Vec4 values = inValues;
+	Vec4::sSort4Reverse(values, ioIdentifiers);
 
 	// Count how many results are less than the max value
-	UVec4 closer = Vec4::sLess(inValues, Vec4::sReplicate(inMaxValue));
+	UVec4 closer = Vec4::sLess(values, Vec4::sReplicate(inMaxValue));
 	int num_results = closer.CountTrues();
 
 	// Shift the values so that only the ones that are less than max are kept
-	inValues = inValues.ReinterpretAsInt().ShiftComponents4Minus(num_results).ReinterpretAsFloat();
+	values = values.ReinterpretAsInt().ShiftComponents4Minus(num_results).ReinterpretAsFloat();
 	ioIdentifiers = ioIdentifiers.ShiftComponents4Minus(num_results);
 
 	// Store the values
-	inValues.StoreFloat4(reinterpret_cast<Float4 *>(outValues));
+	values.StoreFloat4(reinterpret_cast<Float4 *>(outValues));
 
 	return num_results;
 }

+ 4 - 3
Samples/Tests/Character/CharacterTest.cpp

@@ -73,15 +73,16 @@ void CharacterTest::RestoreState(StateRecorder &inStream)
 void CharacterTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool inSwitchStance, float inDeltaTime)
 {
 	// Cancel movement in opposite direction of normal when touching something we can't walk up
+	Vec3 movement_direction = inMovementDirection;
 	Character::EGroundState ground_state = mCharacter->GetGroundState();
 	if (ground_state == Character::EGroundState::OnSteepGround
 		|| ground_state == Character::EGroundState::NotSupported)
 	{
 		Vec3 normal = mCharacter->GetGroundNormal();
 		normal.SetY(0.0f);
-		float dot = normal.Dot(inMovementDirection);
+		float dot = normal.Dot(movement_direction);
 		if (dot < 0.0f)
-			inMovementDirection -= (dot * normal) / normal.LengthSq();
+			movement_direction -= (dot * normal) / normal.LengthSq();
 	}
 
 	// Stance switch
@@ -92,7 +93,7 @@ void CharacterTest::HandleInput(Vec3Arg inMovementDirection, bool inJump, bool i
 	{
 		// Update velocity
 		Vec3 current_velocity = mCharacter->GetLinearVelocity();
-		Vec3 desired_velocity = sCharacterSpeed * inMovementDirection;
+		Vec3 desired_velocity = sCharacterSpeed * movement_direction;
 		desired_velocity.SetY(current_velocity.GetY());
 		Vec3 new_velocity = 0.75f * current_velocity + 0.25f * desired_velocity;