Browse Source

Error instead of crashing when a frequency of 0 is set on a MouseJoint. Resolves issue #1197.

--HG--
branch : minor
Alex Szpakowski 7 years ago
parent
commit
95adf7e347

+ 8 - 0
src/modules/physics/box2d/MouseJoint.cpp

@@ -25,6 +25,8 @@
 #include "World.h"
 #include "Physics.h"
 
+#include <float.h>
+
 namespace love
 {
 namespace physics
@@ -78,6 +80,12 @@ float MouseJoint::getMaxForce() const
 
 void MouseJoint::setFrequency(float hz)
 {
+	// This is kind of a crappy check. The frequency is used in an internal
+	// box2d calculation whose result must be > FLT_EPSILON, but other variables
+	// go into that calculation...
+	if (hz <= FLT_EPSILON * 2)
+		throw love::Exception("MouseJoint frequency must be a positive number.");
+
 	joint->SetFrequency(hz);
 }
 

+ 1 - 1
src/modules/physics/box2d/wrap_MouseJoint.cpp

@@ -70,7 +70,7 @@ int w_MouseJoint_setFrequency(lua_State *L)
 {
 	MouseJoint *t = luax_checkmousejoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setFrequency(arg1);
+	luax_catchexcept(L, [&]() { t->setFrequency(arg1); });
 	return 0;
 }