Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Circle getting stuck in a box

I'm creating a game scene that has two rigidbodies, a circle and a box. I want to simulate an elastic collision but the velocity's normal component is ommited: This is how the ball is bouncing: https://vgy.me/c7wkPZ.gif And this is how it should bounce: https://vgy.me/MJSr8y.png

This is the inicialization code:

void Ball::setBody(float32 x, float32 y, float32 radius, b2World& world) {
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.fixedRotation = true;
    bodyDef.position.x = x;
    bodyDef.position.y = y;
    bodyDef.linearDamping = 0.0f;
    b2CircleShape shape;
    shape.m_p.Set(0.0f, 0.0f);
    shape.m_radius = radius;
    b2FixtureDef fixtureDef;
    fixtureDef.density = 1.0f;
    fixtureDef.filter.categoryBits = 0b0000'0000'0000'0000'0010;
    fixtureDef.filter.maskBits = 0b0000'0000'0000'0000'1111;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 1.0f;
    fixtureDef.shape = &shape;
    SetUp(bodyDef, shape, fixtureDef, world);
}

void Wall::setBody(float32 x, float32 y, float32 width, float32 height, b2World& world) {
    b2BodyDef bodyDef;
    bodyDef.type = b2_staticBody;
    bodyDef.fixedRotation = true;
    bodyDef.position.x = x;
    bodyDef.position.y = y;
    bodyDef.linearDamping = 0.0f;
    b2PolygonShape shape;
    shape.SetAsBox(width / 2.0f, height / 2.0f);
    b2FixtureDef fixtureDef;
    fixtureDef.density = 1.0f;
    fixtureDef.filter.categoryBits = 0b0000'0000'0000'0000'0100;
    fixtureDef.filter.maskBits = 0b0000'0000'0000'0000'0010;
    fixtureDef.friction = 0.0f;
    fixtureDef.isSensor = false;
    fixtureDef.restitution = 1.0f;
    fixtureDef.shape = &shape;
    SetUp(bodyDef, shape, fixtureDef, world);
}

void RigidBody::SetUp(b2BodyDef& body, b2Shape& shape, b2FixtureDef& fixture, b2World& world) {
    // Add to world
    _body = world.CreateBody(&body);
    // Join the fixture to the shape
    fixture.shape = &shape;
    // Create the fixture
    _fixture = _body->CreateFixture(&fixture);
}

void GameState::init() {
    Game::setWorld(*_world);
    GameObject *gameObject = new Wall(50, 50, 20, 20, _game->getTextures()[SIDE]);
    _gameObjects.push_back(gameObject);

    gameObject = new Ball(100,100,5, _game->getTextures()[BALL]);
    _gameObjects.push_back(gameObject);

}

This is the game cicle:

void State::_fixUpdate(float32 time) { 
    _world->Step(time, 8, 3);
}

void State::run() { 
    b2Timer startTime;
    while (!_exit)
    {
        if (startTime.GetMilliseconds() >= 0.001f/(FRAMERATE))
        {
            _fixUpdate(startTime.GetMilliseconds());
            startTime.Reset();
        }
    }
}

Comments