使用鼠标关节拖动时,box2d忽略了碰撞

时间:2014-04-28 10:34:55

标签: c++ box2d cocos2d-x

我不知道为什么但是当我拖动它时,box2D忽略了任何碰撞。我已经添加了子弹,使用步进参数进行了播放但仍然完全忽略了它。

代码

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();


}

测试视频

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

1 个答案:

答案 0 :(得分:0)

如果您希望拖动的物体与属于地面物体的灯具发生碰撞,则需要将鼠标关节的collideConnected设置为true。