Search code examples
c++box2dgame-development

box2d falling while moving


Hey my player isn't falling while I'm pressing any of movement inputs while I'm falling. Just stands still and moves right or left.

Just watch the video; Video

My movement code;

if (right == true) {
    p_pBody.body->SetLinearVelocity(b2Vec2(5, 0));
}
else
{
    p_pBody.rect.setPosition(p_xPos * s_METPX, p_yPos * s_METPX); // Set The SFML Graphics
}
if (left == true) {
    p_pBody.body->SetLinearVelocity(b2Vec2(-5, 0));
}
else
{
    p_pBody.rect.setPosition(p_xPos * s_METPX, p_yPos * s_METPX); // Set The SFML Graphics
}

Solution

  • You're setting the vertical velocity to 0 when you're pressing right or left. That's the second coordinate of b2Vec2. If you want to have gravity, replace that zero with the vertical velocity the block has when the buttons are not being pressed.