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.


Messages - BreadMonika

Pages: [1]
1
General / SFML and Box2D cannot move a body
« on: October 25, 2023, 05:30:31 am »
Hi,
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");
        window.setFramerateLimit(60);

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

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

        world.SetDebugDraw(&debugDraw);

        /* 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)
                        {
                                window.close();
                        }
                        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);
                                        }
                                }
                        }
        }
        window.clear();

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

        world.DebugDraw();

        window.display();
        deltaTime = deltaClock.restart();

    }

    return 0;
}
 

2
SFML projects / Re: Simple Fast SFML Physics
« on: July 01, 2023, 09:44:50 pm »
Hi, I wanted to know, what's the license for this library, is it zlib?

3
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)
            {
                continue;
            }

            sf::IntRect srcRect = sprite.srcRect;
            sf::Vector2f destRect =
            {
                (transform.position.x),
                (transform.position.y)
            };

            sf::Sprite sprite2(*assetManager->getTexture(sprite.assetId),srcRect);
            sprite2.setPosition(destRect);
            sprite2.setScale(transform.scale.x,transform.scale.y);
            renderer->draw(sprite2);
        }
    }
   
}
 

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

4
Graphics / Re: Keeping the sf::View inside of the map
« on: March 13, 2023, 03:45:29 am »
That did it, thank you so much ^-^/

5
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.setFillColor(sf::Color::Red);
    player.setOrigin(10, 10);
    player.setPosition(240, 160);
    const float player_speed = 100;

    sf::Texture backgroundImg;
    backgroundImg.loadFromFile("./resources/true.jpg");


    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;
    app.setView(player_view);

    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);

        clock.restart();

        // 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;


        player_view.setCenter(position);
        app.setView(player_view);

        // rendering entities
        app.draw(background);
        app.draw(player);

        app.display();
        app.clear();
    }
    app.close();
    return 0;
}
 

Pages: [1]