Box2D Forums

It is currently Sat May 25, 2013 9:43 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Apr 29, 2012 9:46 pm 
Offline

Joined: Sun Apr 29, 2012 9:16 pm
Posts: 9
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....


Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 3:05 am 
Offline

Joined: Sun Apr 29, 2012 9:16 pm
Posts: 9
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



Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 4:45 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
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.


Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 6:36 am 
Offline

Joined: Sun Apr 29, 2012 9:16 pm
Posts: 9
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?


Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 6:51 am 
Offline

Joined: Sun Apr 29, 2012 9:16 pm
Posts: 9
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.......


Top
 Profile  
 
PostPosted: Mon Apr 30, 2012 7:09 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group