0

I recently ran into a problem with Box2D where my player was moving at a slow speed. I use LinearImpulse's to move the body but (100, 100) was the fastest it could go.

After researching it a bit, I know that the Box2D body is too large because Box2D uses meters and I'm typing it in pixels. I know I have to scale the bodies using a PPM constant, but it's not quite making sense to me.

I've created a PPM constant and set it to 32.0f.

const float PPM = 32.0f;

Here's how I create the dynamic player body:

void Physics::playerDynamic(float x, float y, float width, float height)
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(x / PPM, y / PPM);

    body = world.CreateBody(&bodyDef);

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox((64.0f) / PPM, (64.0f) / PPM);

    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.filter.categoryBits = CATEGORY_PLAYER;
    fixtureDef.filter.maskBits = CATEGORY_PLAYER;
    fixtureDef.filter.groupIndex = -1;

    body->CreateFixture(&fixtureDef);

    world.Step(timeStep, velocityIterations, positionIterations);

    posX = x;
    posY = y;

    bodyType = "player";
}

I understand that I need to scale down the body, which I did by dividing it by the PPM. But now the body is really small and won't collide how I'd like it to.

Here is the function where I apply the impulse to the player body:

void Physics::moveBodies()
{

    myCollisionDetection myListener;
    world.SetContactListener(&myListener);

    if (bodyType != "player")
    {


    }
    else if (bodyType == "player")
    {

        body->SetAwake(true);

        float forceX = 0;
        float forceY = 0;

        switch (moveState)
        {
        case MS_UP:
            forceY = -50;
            break;
        case MS_DOWN:
            forceY = 50;
            break;
        case MS_LEFT:
            forceX = -50;
            break;
        case MS_RIGHT:
            forceX = 50;
            break;
        case MS_UPRIGHT:
            forceY = -50;
            forceX = 50;
            break;
        case MS_UPLEFT:
            forceY = -50;
            forceX = -50;
            break;
        case MS_DOWNRIGHT:
            forceY = 50;
            forceX = 50;
            break;
        case MS_DOWNLEFT:
            forceY = 50;
            forceX = -50;
            break;
        case MS_STOP:
            body->SetLinearVelocity(b2Vec2(0, 0));
        }

        body->ApplyLinearImpulse(b2Vec2(forceX, forceY ), body->GetWorldCenter(), true);
        pos = body->GetPosition();
        posX = body->GetPosition().x;
        posY = body->GetPosition().y;
    }

    world.Step(timeStep, velocityIterations, positionIterations);

}

I've been researching this for quite a while and it seems simple but I'm having a hard time applying it to my code.

So what do I do from here? Do I scale the body back up?

I'm also using SDL2 for the graphics. Do I need to change anything with the rendering?

3
  • What are the densities you are using in fixtures for your dynamic bodies? Did you show that? I'm not seeing anywhere that you've set it. My apology if I'd missed that. By default they'll be 0 which won't move very quickly with applied impulses. Oct 10, 2018 at 13:12
  • You're right, I didn't set the density. But what should I set it too? @LouisLangholtz
    – D.C.Adams
    Oct 11, 2018 at 14:08
  • Try using the value 1 for the density. If you hadn't been setting it, it should be defaulting to zero. Which is odd then because at 0 density, I don't see how giving that body a linear impulse would give it any speed at all since it's like trying to use forces to move an infinitely massive body. Oct 17, 2018 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.