Box2D Forums

It is currently Sun May 19, 2013 8:28 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Sat Oct 31, 2009 2:49 pm 
Offline

Joined: Sat Oct 31, 2009 2:40 pm
Posts: 5
Location: france
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 ?


Top
 Profile  
 
PostPosted: Sat Oct 31, 2009 3:15 pm 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
Is density greater than 0? Do you step the simulation?


Top
 Profile  
 
PostPosted: Sat Oct 31, 2009 3:24 pm 
Offline

Joined: Sat Oct 31, 2009 2:40 pm
Posts: 5
Location: france
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


Top
 Profile  
 
PostPosted: Sat Oct 31, 2009 4:41 pm 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
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.


Top
 Profile  
 
PostPosted: Sun Nov 01, 2009 2:42 am 
Offline

Joined: Sat Oct 31, 2009 2:40 pm
Posts: 5
Location: france
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?


Attachments:
floor_problem.jpg
floor_problem.jpg [ 24.49 KiB | Viewed 1470 times ]
Top
 Profile  
 
PostPosted: Sun Nov 01, 2009 5:36 am 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
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.


Top
 Profile  
 
PostPosted: Sun Nov 01, 2009 10:40 am 
Offline

Joined: Sat Oct 31, 2009 2:40 pm
Posts: 5
Location: france
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


Attachments:
File comment: precision problem
floor_pb2.jpg
floor_pb2.jpg [ 39.23 KiB | Viewed 1463 times ]
Top
 Profile  
 
PostPosted: Sun Nov 01, 2009 12:02 pm 
Offline

Joined: Sun May 24, 2009 7:43 am
Posts: 107
Location: Quebec, Canada
Have you tried checking with debug draw to see if your shapes stack that way? It could be a display bug.


Top
 Profile  
 
PostPosted: Sun Nov 01, 2009 12:43 pm 
Offline

Joined: Sat Oct 31, 2009 2:40 pm
Posts: 5
Location: france
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


Top
 Profile  
 
PostPosted: Wed Nov 11, 2009 10:22 pm 
Offline

Joined: Sat Nov 07, 2009 1:23 pm
Posts: 2
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. :)


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 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