Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - BreadMonika

Pages: [1]
General / SFML and Box2D cannot move a body
« on: October 25, 2023, 05:30:31 am »
I'm working with SFML and Box2D for physics, the problem is that I'm not able to move the body that I created, I'm using the function ApplyLinearImpulse in order to move the body, but it does not seem to be working, I would greatly appreciate some help with this. Here is my code:

namespace sfdd
    const float SCALE = 32.f;
int main(int argc, char** argv)
    //Setup SFML
        sf::RenderWindow window(sf::VideoMode(1024, 768, 32), "Box2D - SFML Debug Draw Test");

        b2World world(b2Vec2(0.f, 10.f));

        /* Initialize SFML Debug Draw */
        SFMLDebugDraw debugDraw(window);


        /* Set initial flags for what to draw */
        debugDraw.SetFlags(b2Draw::e_shapeBit); //Only draw shapes

        /* constants for time step and physics accuracy */
        const int velocityIterations = 6;
        const int positionIterations = 2;

        /* Create the bounding box */
        b2BodyDef boundingBoxDef;
        boundingBoxDef.type = b2_staticBody;
        float xPos = (window.getSize().x / 2.f) / sfdd::SCALE;
        float yPos = 0.5f;
        boundingBoxDef.position.Set(xPos, yPos);

        b2Body* boundingBoxBody = world.CreateBody(&boundingBoxDef);

        b2PolygonShape boxShape;
        boxShape.SetAsBox((window.getSize().x) / sfdd::SCALE, 0.5f, b2Vec2(0.f, 0.f), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.0); //Top

        yPos = (window.getSize().y) / sfdd::SCALE - 1.f;
        boxShape.SetAsBox((window.getSize().x) / sfdd::SCALE, 0.5f, b2Vec2(0.f, yPos), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.f); //Bottom

        xPos -= 0.5f;
        boxShape.SetAsBox(0.5f, (window.getSize().y) / sfdd::SCALE, b2Vec2(-xPos, 0.f), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.f);//Left

        boxShape.SetAsBox(0.5f, (window.getSize().y) / sfdd::SCALE, b2Vec2(xPos, 0.f), 0.f);
        boundingBoxBody->CreateFixture(&boxShape, 1.f);//Right
        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        bodyDef.position.Set(20, 10);
        bodyDef.fixedRotation = true;
        b2Body* ground = world.CreateBody(&bodyDef); //This is not the body of the bounding box
        b2PolygonShape ps;
        ps.SetAsBox(5, 5);
        ground->CreateFixture(&ps, 1.f);

        sf::Clock deltaClock; //Clock used to measure FPS
        sf::Time deltaTime;
    // This is our little game loop.
    while (window.isOpen())
        // Check if it's time to go
        sf::Event event;

        while (window.pollEvent(event))
                        float force;
                        b2Vec2 currentVel = ground->GetLinearVelocity();

                        if (event.type == sf::Event::Closed)
                        else if (event.type == sf::Event::KeyPressed)
                                if (event.key.code == sf::Keyboard::Left)
                                        if (currentVel.x > -8.0) {
                                                force = -4.f * deltaTime.asSeconds(); // incremental acceleration to maximum speed
                                                std::cout << currentVel.x << ", " << force << ", " << deltaTime.asSeconds() << std::endl;
                                                ground->ApplyLinearImpulse(b2Vec2(force, 0.f), ground->GetLocalCenter(), true);

        world.Step(deltaTime.asSeconds(), velocityIterations, positionIterations);


        deltaTime = deltaClock.restart();


    return 0;

Graphics / Getting a sprite to follow the camera
« on: March 14, 2023, 01:12:11 am »
Hi, SFML community, I'm having issues trying to get a sprite to be fixed to a certain screen position even when the camera is moving, pretty much what I need is having a sprite with a fixed position on the screen or rather a fixed position of the camera and make it remain in there even when the view is moving around. but I want to achieve this, without using another view. I know it would be better to have a view for the HUID but, its just one sprite in this case.

I attach a gif to the problem in question.

Here is my code without my tries, as you can see I have a bool in case a sprite is fixed.

void RenderSystem::update(std::shared_ptr<sf::RenderWindow> renderer, std::unique_ptr<Bread::AssetManager>& assetManager, sf::View & camera)
    for (size_t i(0);i<layers.size();i++)
        for(auto &it: *(*layers[i].getEntities()).getEntities())
            auto& transform(it.getComponent<TransformComponent>());
            auto& sprite(it.getComponent<SpriteComponent>());
            auto& cameraPos(camera.getCenter());
            auto& cameraSize(camera.getSize());

            sf::Vector2f cameraCorner(cameraPos.x - (cameraSize.x / 2), cameraPos.y - (cameraSize.y / 2));
            sf::FloatRect entityRect(transform.position.x, transform.position.y, transform.scale.x * sprite.width, transform.scale.y * sprite.height);

            if(!isInsideOfCamera(entityRect,camera) and !sprite.isFixed)

            sf::IntRect srcRect = sprite.srcRect;
            sf::Vector2f destRect =

            sf::Sprite sprite2(*assetManager->getTexture(sprite.assetId),srcRect);

Thanks in advance. Have a good day ^-^/

Graphics / Keeping the sf::View inside of the map
« on: March 09, 2023, 01:39:53 am »
Hi, I've just started to learn SFML, and as I was experimenting with the views, I came up with a problem that I do not know how to solve, (I did this in SDL2 but things here are different), anyway, what I'm trying to do is keep the view inside of the map limits while scrolling. Like it would scroll in a super mario game, the view is not completely centered in the player but rather it moves when the player is at half of the window.

How can I keep the view inside of the map limits?

Here is what I have so far, it works well for the top and left corners but the right and bottom ones dont.

please see the gif to see my current behavior.

Here is my code:
#include <SFML/Graphics.hpp>

int main()
    sf::RenderWindow app(sf::VideoMode(480, 320), "view demo");

    // create player, at the screen center
    sf::RectangleShape player(sf::Vector2f(20, 20));
    player.setOrigin(10, 10);
    player.setPosition(240, 160);
    const float player_speed = 100;

    sf::Texture backgroundImg;

    sf::Sprite background(backgroundImg);


    // we create our custom view
    sf::View player_view(sf::FloatRect(0, 0, app.getSize().x, app.getSize().y));
    player_view.setViewport(sf::FloatRect(0, 0, 1, 1));
    sf::Clock clock;
    bool running = true;
    sf::Event event;

    while (running)
        while (app.pollEvent(event))
            if (event.type == sf::Event::Closed)
                running = false;

        // moving player
        float frametime = clock.getElapsedTime().asSeconds();
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            player.move(0, -player_speed * frametime);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            player.move(0, player_speed * frametime);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            player.move(-player_speed * frametime, 0);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            player.move(player_speed * frametime, 0);


        // we keep our view centered on the player

        sf::Vector2f position;
        auto sizeX(app.getSize().x);
        auto sizeY(app.getSize().y);
        player.getPosition().x >= sizeX / 2 ? position.x = player.getPosition().x : position.x = sizeX / 2;
        player.getPosition().y >= sizeY / 2 ? position.y = player.getPosition().y : position.y = sizeY / 2;


        // rendering entities

    return 0;

Pages: [1]