[new to Box2D] ApplyImpulse problem

I try to implement box2d in my C++ game engine

The integration went well and it was quite easy to implement it with my opengl 2d stuff

However , i have a problem that i didnt manage to fix , I want to Applies forces on the body but all my test
with ApplyForce and ApplyImpluse has no effect to my bodies

It s just a simple test with box shapes

my body initialisation looks like this

Code:
pWorld = PhysicsWorld::GetSingleton()->pWorld;
   
   b2BodyDef bodyDef;

   this->pos  = pos;
   this->size = size;

   bodyDef.position.Set(pos.x,pos.y);
   
   body = pWorld->CreateBody(&bodyDef);

   b2PolygonDef shapeDef;
   shapeDef.SetAsBox(size.x*.5f,size.y*.5f);
   shapeDef.density = density;
   shapeDef.friction = fricton;
   shapeDef.restitution = restitution;
   body->CreateShape(&shapeDef);

   b2BodyDef bDef;
   bDef.linearDamping = 0.01f;
   bDef.angularDamping = 0.01f;

   body->SetMassFromShapes();



ApplyForce code , called latter of a keyboard action

Code:
   if (pCore->MouseLeft()){
      //c = VGL_Color(1,0,0,1);//
      body->ApplyImpulse(b2Vec2(0,10),body->GetWorldCenter());
   
   }


I ve google about ApplyForce/Impulse and this code looks corrects, the simulation is ok , but i m not able to add any forces with ApplyForce/Impulse on my bodies

Any ideas ? what can be wrong ?
Is density greater than 0? Do you step the simulation?
i tried many values but acutal density is set to 5.0f

I step the simulation each frame of my program

Code:
pWorld->Step( *pdeltaTime * .01f ,10);


the gravity effect is applied but ApplyImpulse has no effect
This may be because your impulse is too weak. With that density and a large enough box, gravity would counteract the impulse and keep the box on the ground.
you re right, looks like my mass is very heavy, so if I use a very large value , like 100000.f (or 5.f * body->GetMass()) , it fixes the problem 🙂


It always a problem to find correct values to use with physics engines 😉

I choose to use pos and size that fits my openGL display (1024*768 in Ortho2d mode ), but i ve got precision problem, I just have one main floor at 0,0 and some boxes are 5-10 pixels above the floor (see attached screenshot).

Any advice about that?
Too large objects would be my guess. You have to keep in mind Box2D uses meters as units. If your boxes are 30 pixel wide, you are dealing with building sized boxes. That would explain the huge impulse force required. Box2D is tuned for objects in the 0.1-10 meters range and some things may not work properly when objects are outside that range.
I implemented a ratio 1/100 btw openGL coordinates and Box2D with 2 macros

Code:
#define OGL_TO_B2D(x) x*0.01f
#define B2D_TO_OGL(x) x*100.f


it s better, but the problem is still present (see attached)

my box size is now 0.5f
Have you tried checking with debug draw to see if your shapes stack that way? It could be a display bug.
not yet .

I looked at the exemples, debug draw implementation exemple uses openGL , but I didnt manage to get it implemented in my engine yet.

anyway, it dont look like a draw bug because sometimes the boxes remains above the floor for 2-5 seconds and then drops on the floor.

maybe something is wrong with my friction or restitution params
velk wrote:
anyway, it dont look like a draw bug because sometimes the boxes remains above the floor for 2-5 seconds and then drops on the floor.
I just had the same problem and solved it. If you set your boxes to have fixedRotation = true, you might notice the problem goes away. If so, this is because you aren't calculating your rotation matrices correctly. The angle provided by Box2D is in radians, so you need to convert it to degrees. You do this by multiplying it by 180.0f / b2_pi.

In OpenGL:
Code:
glRotatef(angle * (180.0f / b2_pi), 0.0f, 0.0f, 1.0f);
What was happening the corner of the box was hitting the ground, but the rotation of the box was rendered incorrectly, making it look like it was hovering for a while until it came to rest.

Your problem could be any number of things, but hopefully that solves it for you. 🙂

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