3

I am using Box2D for collision detection only. My code is similar to that in Ray Wenderlich's tutorial here.

I am encountering a problem with this method. Since the code bypasses the Box2D simulation, there is no collision response. Therefore, sprites can overlap. I am aware that Box2D collision APIs provide a unit normal vector to help resolve collisions. However, this vector conveys direction but not magnitude. Therefore, I cannot determine how far I should move overlapping sprites. Does anyone know how to use the Box2D collision APIs to manually resolve an overlap?

4

1 回答 1

1

我不知道 iOS 的东西,但你想做的是扩展 b2ContactListener 并覆盖 PreSolve。

void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);

如果你设置contact->SetEnabled(false) 你会发生碰撞但是它不会被处理。一旦发生碰撞,您可以执行类似于以下的操作。

const b2Manifold* 歧管 = 接触->GetManifold();

    if (manifold->m_pointCount == 0)
    {
        return;
    }

    b2Fixture* fixtureA = contact->GetFixtureA();
    b2Fixture* fixtureB = contact->GetFixtureB();

    b2PointState state1[b2_maxManifoldPoints], state2[b2_maxManifoldPoints];
    b2GetPointStates(state1, state2, oldManifold, manifold);

    b2WorldManifold worldManifold;
    contact->GetWorldManifold(&worldManifold);

    for (int32 i = 0; i < manifold->m_pointCount && m_pointCount < k_maxContactPoints; ++i)
    {
        ContactPoint* cp = m_points + m_pointCount;
        cp->fixtureA = fixtureA;
        cp->fixtureB = fixtureB;
        cp->position = worldManifold.m_points[i];
        cp->normal = worldManifold.m_normal;
        cp->state = state2[i];
        ++m_pointCount;
    } 
于 2013-05-01T22:24:37.773 回答