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

box2d is ignoring collision when dragging with mouse joint

本文关键字:拖动 碰撞 鼠标 box2d      更新时间:2023-10-16

我不知道为什么,但当我拖动box2D时,它会忽略任何碰撞。我添加了项目符号,使用Stepping参数播放,但它仍然完全忽略它。

代码

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

如果希望拖动的物体与属于地面主体的固定装置碰撞,则需要将鼠标关节的collapseConnected设置为true。