Box2D with SFML object - this is switching and moving each other after a collision

I use Box2D and SFML to create a simple game. However, my object (the ball) does not completely land after it hits the ground. It seems that 50% of the ball went through my border, which looks very uncomfortable. Below are illustrations to illustrate.

Ball to:

enter image description here

Ball After the border hits the ground:

enter image description here

As you can see, 50% of the ball has disappeared (most likely due to displacements or something). Does anyone know how to fix this?

Below is my code for creating an object:

    circ_ = sf::CircleShape(radius);
    circ_.setOrigin(sf::Vector2f(size.x/2,size.y/2));
    circ_.setFillColor(sf::Color(255, 255, 255, 255));
    circ_.setOutlineThickness(1);
    circ_.setOutlineColor(sf::Color::Black);

    bodyDef_.position = b2Vec2(position.x/PIXEL_PER_METER, position.y/PIXEL_PER_METER);

    bodyDef_.type = b2_staticBody;
    bodyFixtureDef_.density = 1.0f;
    bodyFixtureDef_.friction = 0.3f;
    bodyFixtureDef_.restitution = 0.8f;

Where is my SFML code, I created a Box2D object using:

Ball basketBall(world, basketBallSize, basketBallPos, 0.0, basketBallRadius, false);

Where is the radius = 32. Can anyone help me? Thanks.

+4
source share
1

Box2d / , SFML / , SFML Box2d. , Box2d .

:

#include <cmath> // for M_PI define

Sprite.setPosition(sf::Vector2f(b2BallBody->GetPosition().x * PIXEL_PER_METER - Sprite.getGlobalBounds().width * 0.5f,
                                b2BallBody->GetPosition().y * PIXEL_PER_METER - Sprite.getGlobalBounds().height * 0.5f));
Sprite.setRotation(b2BallBody->GetAngle() * (float)(180.0 / M_PI));
+2

Source: https://habr.com/ru/post/1623875/


All Articles