Freezing ?

Anyone have freezing issues when using box2d?

Basically I have a simulation full of squares, and I've noticed on certain collisions specifically near the corners, the entire process will come to a screeching halt.

I've never experienced this before, only since I've integrated box2d. I'm wondering if it's getting stuck in a dead loop somehow in the collision methods?

I'm doing very little fancy... just some boxes being pushed around and the freezing is intermittent, but consistent.

Just curious if it's a known issue or if there's some obvious thing I might be doing wrong? It's essentially the hello world program with some more boxes, controls, etc..

I'll post a binary if folks are willing to see.
Please post some code that can be run in the testbed.
Sure. It seems to be related to very high torque values (angular velocity?). Sorry this test is a little weird, I was trying to re-create the scenario in my demo. Zoom out immediately (i use mousewheel). But, if you jumble the torque around (I added an ApplyTorque call), it'll freeze in different places.

I originally wasn't modifying my torque or angular velocity in my game -- I suppose after falling and bouncing around so much it just hit whatever threshold might be causing the problem. I was able to recreate the issue in my game by artificially inflating torque (as done here).

Maybe that's bad, maybe this is just an overflow issue... but if it's a bug where a constraint is needed, perhaps that'll help resolve the issue. Not sure. Maybe I need to just avoid such high values! I've only had Box2d a week now, be gentle on me 🙂



I basically just modified the verticle stack test (VerticalStack.h). You should be able to just copy-paste this and compile.

Code:
/*
* Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef VERTICAL_STACK_H
#define VERTICAL_STACK_H

class VerticalStack : public Test
{
public:

   enum
   {
      e_columnCount = 1,
      e_rowCount = 100
      //e_columnCount = 1,
      //e_rowCount = 1
   };

   VerticalStack()
   {
      {
         b2BodyDef bd;
         bd.angle = 2.95f;
         bd.position.Set(24.0f, -60.0f);
         b2Body* ground = m_world->CreateBody(&bd);

         b2PolygonShape shape;
         shape.SetAsBox(25.0f, 5.0f);
         ground->CreateFixture(&shape, 0.0f);
      }

      //float32 xs[5] = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f};

      float32 xs[5] = {0.0f, -22.0f, -12.0f, 11.0f, 22.0f};

      for (int32 j = 0; j < e_columnCount; ++j)
      {
         b2PolygonShape shape;
         shape.SetAsBox(2.5f, 2.5f);

         b2FixtureDef fd;
         fd.shape = &shape;
         fd.density = 0.05f;
         fd.friction = 0.05f;
         fd.restitution = 0.45f;

         for (int i = 0; i < e_rowCount; ++i)
         {
            b2BodyDef bd;
            bd.type = b2_dynamicBody;

            int32 n = j * e_rowCount + i;
            b2Assert(n < e_rowCount * e_columnCount);
            m_indices[n] = n;
            bd.userData = m_indices + n;

            float32 x = 0.0f;
            //float32 x = RandomFloat(-0.02f, 0.02f);
            //float32 x = i % 2 == 0 ? -0.025f : 0.025f;
            bd.position.Set(xs[j] + x, 8.0f * i);
            bd.angle = RandomFloat(-3.14f, 3.14f);
            b2Body* body = m_world->CreateBody(&bd);
            body->ApplyTorque(50000.0f);

            m_bodies[n] = body;

            body->CreateFixture(&fd);
         }
      }

      m_bullet = NULL;
   }

   void Keyboard(unsigned char key)
   {
      switch (key)
      {
      case ',':
         if (m_bullet != NULL)
         {
            m_world->DestroyBody(m_bullet);
            m_bullet = NULL;
         }

         {
            b2CircleShape shape;
            shape.m_radius = 0.25f;

            b2FixtureDef fd;
            fd.shape = &shape;
            fd.density = 20.0f;
            fd.restitution = 0.05f;

            b2BodyDef bd;
            bd.type = b2_dynamicBody;
            bd.bullet = true;
            bd.position.Set(-31.0f, 5.0f);

            m_bullet = m_world->CreateBody(&bd);
            m_bullet->CreateFixture(&fd);

            m_bullet->SetLinearVelocity(b2Vec2(400.0f, 0.0f));
         }
         break;
      }
   }

   void Step(Settings* settings)
   {
      Test::Step(settings);
      m_debugDraw.DrawString(5, m_textLine, "Press: (,) to launch a bullet.");
      m_textLine += 15;

      //if (m_stepCount == 300)
      //{
      //   if (m_bullet != NULL)
      //   {
      //      m_world->DestroyBody(m_bullet);
      //      m_bullet = NULL;
      //   }

      //   {
      //      b2CircleShape shape;
      //      shape.m_radius = 0.25f;

      //      b2FixtureDef fd;
      //      fd.shape = &shape;
      //      fd.density = 20.0f;
      //      fd.restitution = 0.05f;

      //      b2BodyDef bd;
      //      bd.type = b2_dynamicBody;
      //      bd.bullet = true;
      //      bd.position.Set(-31.0f, 5.0f);

      //      m_bullet = m_world->CreateBody(&bd);
      //      m_bullet->CreateFixture(&fd);

      //      m_bullet->SetLinearVelocity(b2Vec2(400.0f, 0.0f));
      //   }
      //}
   }

   static Test* Create()
   {
      return new VerticalStack;
   }

   b2Body* m_bullet;
   b2Body* m_bodies[e_rowCount * e_columnCount];
   int32 m_indices[e_rowCount * e_columnCount];
};

#endif

I was able to reproduce the problem and filed an issue:
http://code.google.com/p/box2d/issues/detail?id=115

I'll look into a fix soon.

For now you can probably prevent the infinite loop by periodically normalizing the body angle to the range -pi to pi.

Thanks!
Awesome, thanks! 😀

Original thread: https://box2d.org/forum/viewtopic.php?t=4790