Box2d dynamic body stuck to static
Sometimes, when dynamic body moving and collide static body, dynamic stuck, and stopping.
http://imageshack.us/photo/my-images/14/screenfgz.png/ (top and bottom lines are sensors, center shape is body)
For moving used mouseJoint->SetTarget(pos);
Settings: World
b2Vec2 gravity = b2Vec2(0.f, 0.f);
bool doSleep = true;
world = new b2World(gravity, doSleep);
world->SetContinuousPhysics(false); // when using true, dynamic tunneling through other bodies
Dynamic body
...
density = 1.f;
friction = 0.f;
restitution = 0.f;
...
body->SetBullet(true);
body->SetFixedRotation(true);
MouseJoint
b2Vec2 locationWorld = b2Vec2(pos.x/PTM_RATIO,pos.y/PTM_RATIO);
b2MouseJointDef md;
md.bodyA = groundBody;
md.bodyB = body;
md.target = body->GetPosition();
md.collideConnected = true;
md.maxForce = 1000 * body->GetMass();
md.dampingRatio = 0.1f;
md.frequencyHz = 2.f;
mouseJoint = (b2MouseJoin开发者_JAVA百科t *)world->CreateJoint(&md);
Dynamic and static bodies are rectangles. Rotation = 0.
How remove stucking?
If you are seeing the dynamic box getting caught on the tiles in the static body as it seems from the screenshot, this is a known issue in Box2D at the moment. You could improve it by using one large polygon for the static body, or a loop shape, or simply bevel the corners of the dynamic body so they are not perfectly square.
Solution: use edges for create fixture (at dynamic and static bodies)
shape.SetAsEdge(b2Vec2(x,y),b2Vec2(x2,y2));
bodyWall->CreateFixture(&shapeDef);
instead
shape.SetAsBox(width,height, b2Vec2(x,y),angle);
精彩评论