Box2D Forums

It is currently Wed May 22, 2013 10:46 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Jul 29, 2011 5:40 pm 
Offline

Joined: Thu Jul 28, 2011 1:34 pm
Posts: 11
Hello. I'm new to Box2d, and i apologize about my english and c++ knowledge.

I modyfied a basic Confined.h test from TestBed, to demonstrate what i asking.
The problem is, when ball with friction=0, restitution=1, linear and angular damping=0 faced with a wall at a small angle of incidence and small speed, it must be perfectly elastic collision. Ball must bounce from wall, but it sticks to the wall, and slides.

Maybe someone knows, how make perfectly elastic collision, when at small angles and speeds ball bounce off the walls?
It is necessary to preserve the total momentum of the system after the collision.

Regards.

Code of modyfied Confined.h, modyfied and added strings marked with "//<-Added--" and "//<-Modyfied--".
Code:
#ifndef CONFINEDMOD_H
#define CONFINEDMOD_H

class ConfinedMod : public Test
{
public:

   ConfinedMod()
   {
      {
         b2BodyDef bd;
         b2Body* ground = m_world->CreateBody(&bd);
         b2PolygonShape shape;
         shape.SetAsEdge(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f));
         ground->CreateFixture(&shape, 0.0f);    // Floor
         shape.SetAsEdge(b2Vec2(-10.0f, 0.0f), b2Vec2(-10.0f, 20.0f));
         ground->CreateFixture(&shape, 0.0f);    // Left wall
         shape.SetAsEdge(b2Vec2(10.0f, 0.0f), b2Vec2(10.0f, 20.0f));
         ground->CreateFixture(&shape, 0.0f);    // Right wall
         shape.SetAsEdge(b2Vec2(-10.0f, 20.0f), b2Vec2(10.0f, 20.0f));
         ground->CreateFixture(&shape, 0.0f);    // Roof
      }

      m_world->SetGravity(b2Vec2(0.0f, 0.0f));
   }

   void CreateCircle()
   {
      float32 radius = 2.0f;
      b2CircleShape shape;
      shape.m_p.SetZero();
      shape.m_radius = radius;

      b2FixtureDef fd;
      fd.shape = &shape;
      fd.density = 1.0f;
      fd.friction = 0.0f;
      fd.restitution = 1.0f; //<-Added--

      b2Vec2 p(-7.0f, 3.0f); //<-Modyfied--
      b2BodyDef bd;
      bd.type = b2_dynamicBody;
      bd.position = p;
      bd.linearDamping = 0.0f; //<-Added--
      bd.angularDamping = 0.0f; //<-Added--
      //bd.allowSleep = false;
      b2Body* body = m_world->CreateBody(&bd);

      body->CreateFixture(&fd);
      b2Vec2* MyVec = new b2Vec2(11.5f, -1.1f); //<-Added--
        body->ApplyLinearImpulse(*MyVec, body->GetWorldCenter()); //<-Added--
   }

   void Keyboard(unsigned char key)
   {
      switch (key)
      {
      case 'c':
         CreateCircle();
         break;
      }
   }

   void Step(Settings* settings)
   {
      bool sleeping = true;
      for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
      {
         if (b->GetType() != b2_dynamicBody)
         {
            continue;
         }

         if (b->IsAwake())
         {
            sleeping = false;
         }
      }

      if (m_stepCount == 180)
      {
         m_stepCount += 0;
      }

      //if (sleeping)
      //{
      //   CreateCircle();
      //}

      Test::Step(settings);

      for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
      {
         if (b->GetType() != b2_dynamicBody)
         {
            continue;
         }

         b2Vec2 p = b->GetPosition();
         if (p.x <= -10.0f || 10.0f <= p.x || p.y <= 0.0f || 20.0f <= p.y)
         {
            p.x += 0.0;
         }
      }

      m_debugDraw.DrawString(5, m_textLine, "Press 'c' to create a circle.");
      m_textLine += 15;
   }

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

#endif


I originally wrote the code for ActionScript 3.0, using Box2dFlash port.
So, I thought that the problem in IEEE-754 Arithmetic, but now i write same test in C++, and see that problem is still here.
My post for AS3.0.


Top
 Profile  
 
PostPosted: Fri Jul 29, 2011 9:33 pm 
Offline

Joined: Tue Feb 24, 2009 4:10 pm
Posts: 564
Location: Michigan
if i recall correctly very small forces are sometimes ignored for the sake of performance.
this comes up in billiards simulations posts I've seen around.
there is some core setting that you can change in box2d to address the issue, sadly i do not know which one.
i hope that might point you in the right direction.


Top
 Profile  
 
PostPosted: Sat Jul 30, 2011 12:12 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Look in the file b2Settings.h in the section titled Dynamics, maybe playing with some of those might help.


Top
 Profile  
 
PostPosted: Sat Jul 30, 2011 12:50 am 
Offline

Joined: Thu Jul 28, 2011 1:34 pm
Posts: 11
Great thanks to sketchbookgames and irresistible force.
Changing b2_velocityThreshold variable in b2Settings.h (C++) or in b2Settings.as (Action script in Box2dFlash) to lower value solves this problem.
Code:
/**
* A velocity threshold for elastic collisions. Any collision with a relative linear
* velocity below this threshold will be treated as inelastic.
*/
static public const b2_velocityThreshold:Number = 1.0;      // 1 m/s


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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