Browse Source

Changed World:update(dt) to default to Box2D's recommended 3 internal position iterations instead of 6. Added a new variant World:update(dt, velocityiterations, positioniterations) which lets you specify the number of velocity and position iterations Box2D performs when updating the world. The defaults are 8 and 3 respectively, higher numbers increase precision and reduce performance.

Resolves issue #1135.

--HG--
branch : minor
Alex Szpakowski 9 years ago
parent
commit
20b77bd28c

+ 6 - 1
src/modules/physics/box2d/World.cpp

@@ -251,7 +251,12 @@ World::~World()
 
 
 void World::update(float dt)
 void World::update(float dt)
 {
 {
-	world->Step(dt, 8, 6);
+	update(dt, 8, 3); // Box2D 2.3's recommended defaults.
+}
+
+void World::update(float dt, int velocityIterations, int positionIterations)
+{
+	world->Step(dt, velocityIterations, positionIterations);
 
 
 	// Destroy all objects marked during the time step.
 	// Destroy all objects marked during the time step.
 	for (Body *b : destructBodies)
 	for (Body *b : destructBodies)

+ 1 - 0
src/modules/physics/box2d/World.h

@@ -131,6 +131,7 @@ public:
 	 * @param dt The timestep.
 	 * @param dt The timestep.
 	 **/
 	 **/
 	void update(float dt);
 	void update(float dt);
+	void update(float dt, int velocityIterations, int positionIterations);
 
 
 	// From b2ContactListener
 	// From b2ContactListener
 	void BeginContact(b2Contact *contact);
 	void BeginContact(b2Contact *contact);

+ 11 - 1
src/modules/physics/box2d/wrap_World.cpp

@@ -39,9 +39,19 @@ int w_World_update(lua_State *L)
 {
 {
 	World *t = luax_checkworld(L, 1);
 	World *t = luax_checkworld(L, 1);
 	float dt = (float)luaL_checknumber(L, 2);
 	float dt = (float)luaL_checknumber(L, 2);
+
 	// Make sure the world callbacks are using the calling Lua thread.
 	// Make sure the world callbacks are using the calling Lua thread.
 	t->setCallbacksL(L);
 	t->setCallbacksL(L);
-	luax_catchexcept(L, [&](){ t->update(dt); });
+
+	if (lua_isnoneornil(L, 3))
+		luax_catchexcept(L, [&](){ t->update(dt); });
+	else
+	{
+		int velocityiterations = (int) luaL_checknumber(L, 3);
+		int positioniterations = (int) luaL_checknumber(L, 4);
+		luax_catchexcept(L, [&](){ t->update(dt, velocityiterations, positioniterations); });
+	}
+
 	return 0;
 	return 0;
 }
 }