Question

I don't know why but box2D is ignoring any collision when I drag it. I've added the bullet, played with the Stepping parameters but still it completely ignores it.

Code

class QueryCallback : public b2QueryCallback
{
public:
    QueryCallback(const b2Vec2& point)
    {
        m_point = point;
        m_fixture = NULL;

    }

    bool ReportFixture(b2Fixture* fixture)
    {
        b2Body* body = fixture->GetBody();
        if (body->GetType() == b2_dynamicBody)

        {

            bool inside = fixture->TestPoint(m_point);
            if (inside)
            {
                m_fixture = fixture;

                // We are done, terminate the query.
                return false;
            }
        }

        // Continue the query.
        return true;
    }

    b2Vec2 m_point;
    b2Fixture* m_fixture;
};

Box2dModel::Box2dModel() {

    b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);
    _world = new b2World(gravity);
    _world->SetAllowSleeping(true);
    _mouseJoint = NULL;

    _debugdraw = new GLESDebugDraw(PTM_RATIO);
    _world->SetDebugDraw(_debugdraw);

    // test
    b2BodyDef bodyDef;
    bodyDef.bullet = true;
    bodyDef.fixedRotation = true;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(100/PTM_RATIO, 200/PTM_RATIO);

    b2PolygonShape polygonShape;
    polygonShape.SetAsBox(50/PTM_RATIO, 50/PTM_RATIO);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &polygonShape;
    fixtureDef.density = 10.0f;

    _body = _world->CreateBody(&bodyDef);
    _body->CreateFixture(&fixtureDef);
    //_body->SetGravityScale(0);

    //ground
    b2BodyDef bodyDef01;
    bodyDef01.position.Set(160/PTM_RATIO, 50/PTM_RATIO);

    b2PolygonShape polygonShape01;
    polygonShape01.SetAsBox(100/PTM_RATIO, 50/PTM_RATIO);

    b2FixtureDef fixtureDef01;
    fixtureDef01.shape = &polygonShape01;

    _ground = _world->CreateBody(&bodyDef01);
    _ground->CreateFixture(&fixtureDef01);

    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
    _debugdraw->SetFlags(flags);
}

Box2dModel::~Box2dModel() {

    CC_SAFE_DELETE(_world);
}

bool Box2dModel::MouseDown(const b2Vec2& p) {

    _worldPosition = p;

    if (_mouseJoint != NULL)
    {
        return false;
    }


    b2AABB aabb;
    b2Vec2 d;
    d.Set(1.0f/PTM_RATIO, 1.0f/PTM_RATIO);
    aabb.lowerBound = p - d;
    aabb.upperBound = p + d;

    QueryCallback callback(p);
    _world->QueryAABB(&callback, aabb);

    if (callback.m_fixture) {

        b2Body* body = callback.m_fixture->GetBody();
        b2MouseJointDef md;
        md.bodyA = _ground;
        md.bodyB = body;
        md.target = p;
        md.maxForce = 1000.0f * body->GetMass();
        _mouseJoint = (b2MouseJoint*)_world->CreateJoint(&md);
        body->SetAwake(true);
        return true;

    }


    return false;
}

void Box2dModel::MouseUp(const b2Vec2& p) {

    if (_mouseJoint)
    {
        _world->DestroyJoint(_mouseJoint);
        _mouseJoint = NULL;
    }

}

void Box2dModel::MouseMove(const b2Vec2& p) {

    _worldPosition = p;

    if (_mouseJoint)
    {
        _mouseJoint->SetTarget(p);
    }
}

void Box2dModel::update(Settings* settings, float delta) {

    if (settings->pause)
    {
        if (settings->singleStep)
        {
            settings->singleStep = 0;
        }
        else
        {
            delta = 0.0f;
        }
    }

    float32 subStep = 3;

    for (int i = 0; i < subStep; i++) {
        _world->Step(delta/subStep, settings->velocityIterations, settings->positionIterations);
    }


    //_world->ClearForces();


}

Test Video

https://dl.dropboxusercontent.com/u/604317/test.mov

Était-ce utile?

La solution

You need to set collideConnected to true for the mouse joint if you want the dragged thing to collide with fixtures belonging to the ground body.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top