why does the result of simulation differ from time to time?

hello,i got some problem with box2d.
everytime i shooted 30 bodyes in same time,same place and same speed
but the landing result differs from time to time
why the result is random....
this is my EdgeTest.h

Everytime I press 'c'
i got the different result
but after i press the "restart" button ,the first result is same
it's liked exist some random seriales in box2d contact result
"restart" can reset the random seed...
Code:
#ifndef EDGE_TEST_H
#define EDGE_TEST_H
#define MAX_TEST_BALL 30
class EdgeTest : public Test
{
public:

   EdgeTest()
   {
      {
         b2BodyDef bd;
         b2Body* ground = m_world->CreateBody(&bd);

         b2Vec2 v1(-10.0f, 6.0f), v2(-10.0f, 0.0f), v3(0.0f, 0.0f);
         b2Vec2 v4(0.0f, 6.0f) ;

         b2EdgeShape shape;

         shape.Set(v1, v2);
         shape.m_hasVertex3 = true;
         shape.m_vertex3 = v3;
         ground->CreateFixture(&shape, 0.0f);

         shape.Set(v2, v3);
         shape.m_hasVertex0 = true;
         shape.m_hasVertex3 = true;
         shape.m_vertex0 = v1;
         shape.m_vertex3 = v4;
         ground->CreateFixture(&shape, 0.0f);

         shape.Set(v3, v4);
         shape.m_hasVertex0 = true;
         shape.m_hasVertex3 = false;
         shape.m_vertex0 = v2;
         ground->CreateFixture(&shape, 0.0f);
      }

      {   
         b2BodyDef bd;
         bd.type = b2_dynamicBody;
         bd.position.Set(-5.5f, 0.6f);
         bd.allowSleep = false;
   

         b2CircleShape shape;
         shape.m_radius = 0.5f;
         for (int i=0;i<MAX_TEST_BALL;i++)
         {
            m_testBody[i]= m_world->CreateBody(&bd);
            m_testBody[i]->CreateFixture(&shape, 1.0f);
          }
         m_bombIndex=MAX_TEST_BALL; 
      }
   }
               b2Body*  m_testBody[MAX_TEST_BALL];
   int m_bombIndex ;
   void Step(Settings* settings)
   {
      bool advanceRay = settings->pause == 0 || settings->singleStep;
      if (m_bombIndex >=0 && m_bombIndex<MAX_TEST_BALL)
      {
            m_testBody[m_bombIndex]->SetLinearVelocity(b2Vec2(-2.0f+m_bombIndex*0.1f,20.0f));
             m_testBody[m_bombIndex]->SetTransform(b2Vec2(0  ,6),0.0f); 
            m_bombIndex++; 
      }
      Test::Step(settings);
      
   }
   void Keyboard(unsigned char key)
   {
      switch (key)
      {
      case 'c':
         {
            m_bombIndex=0; 
         }
         break;
      }
   }
               static Test* Create()
   {
      return new EdgeTest;
   }
};

#endif

It's because you are pressing 'c' at different times in each run, which introduces a kind of randomness.
Instead of pressing 'c' yourself, you could make the program do it in a repeatable way by putting something like this into the Step function:
Code:
if ( m_stepCount % 300 )
    m_bombIndex = 0;
This would be like you pressing 'c' every 5 seconds (with the default testbed speed of 60fps). m_stepCount is a variable in the Test class.
irresistible force wrote:
It's because you are pressing 'c' at different times in each run, which introduces a kind of randomness.
Instead of pressing 'c' yourself, you could make the program do it in a repeatable way by putting something like this into the Step function:
Code:
if ( m_stepCount % 300 )
    m_bombIndex = 0;
This would be like you pressing 'c' every 5 seconds (with the default testbed speed of 60fps). m_stepCount is a variable in the Test class.


thank you for your attention
I add this code in step function,auto bomb 30 balls about 10 second
but I got a new result every 10s.
Code:
            if ( m_stepCount % 600==0 )
               m_bombIndex = 0;


30 balls have same LinearVelocity,same start position,and same density ...
and bomb with same frequency,no other body to disturb them
why result is not same?
thank you again.

I got the answer when asked questions...

maybe some parameter that I didn't attented is not same with last time bomb...

so i try this

m_testBody[m_bombIndex]->SetAngularVelocity( 0);

bingo.......
Sorry, I should have explained that better. I meant that you would get the same result each time you pressed 'Restart' and let it run, not every time the m_bombIndex variable was reset.

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