diff --git a/src/mathematics/mathematics_functions.cpp b/src/mathematics/mathematics_functions.cpp index 54aa0878..1e13a0f1 100644 --- a/src/mathematics/mathematics_functions.cpp +++ b/src/mathematics/mathematics_functions.cpp @@ -47,7 +47,7 @@ void reactphysics3d::computeBarycentricCoordinatesInTriangle(const Vector3& a, c decimal denom = d00 * d11 - d01 * d01; v = (d11 * d20 - d01 * d21) / denom; w = (d00 * d21 - d01 * d20) / denom; - u = decimal(1.0) - u - w; + u = decimal(1.0) - v - w; } // Clamp a vector such that it is no longer than a given maximum length diff --git a/test/tests/mathematics/TestMathematicsFunctions.h b/test/tests/mathematics/TestMathematicsFunctions.h index f7cb56e5..1939efc2 100644 --- a/test/tests/mathematics/TestMathematicsFunctions.h +++ b/test/tests/mathematics/TestMathematicsFunctions.h @@ -107,19 +107,23 @@ class TestMathematicsFunctions : public Test { Vector3 a(0, 0, 0); Vector3 b(5, 0, 0); Vector3 c(0, 0, 5); + Vector3 testPoint(4, 0, 1); decimal u,v,w; computeBarycentricCoordinatesInTriangle(a, b, c, a, u, v, w); - test(approxEqual(u, 1.0, 0.0001)); - test(approxEqual(v, 0.0, 0.0001)); - test(approxEqual(w, 0.0, 0.0001)); + test(approxEqual(u, 1.0, 0.000001)); + test(approxEqual(v, 0.0, 0.000001)); + test(approxEqual(w, 0.0, 0.000001)); computeBarycentricCoordinatesInTriangle(a, b, c, b, u, v, w); - test(approxEqual(u, 0.0, 0.0001)); - test(approxEqual(v, 1.0, 0.0001)); - test(approxEqual(w, 0.0, 0.0001)); + test(approxEqual(u, 0.0, 0.000001)); + test(approxEqual(v, 1.0, 0.000001)); + test(approxEqual(w, 0.0, 0.000001)); computeBarycentricCoordinatesInTriangle(a, b, c, c, u, v, w); - test(approxEqual(u, 0.0, 0.0001)); - test(approxEqual(v, 0.0, 0.0001)); - test(approxEqual(w, 1.0, 0.0001)); + test(approxEqual(u, 0.0, 0.000001)); + test(approxEqual(v, 0.0, 0.000001)); + test(approxEqual(w, 1.0, 0.000001)); + + computeBarycentricCoordinatesInTriangle(a, b, c, testPoint, u, v, w); + test(approxEqual(u + v + w, 1.0, 0.000001)); } };