Search code examples
c++classsfml

Trying to create a class for a simple ball bounce experiment, where am I going wrong?


I am very new to programming in c++, and I'm trying to create a simple program using SFML in which it is possible to create balls which will bounce around. However I've gotten stuck before I even got to the physics sim. I can't seem to get my ball class working. I wanted to use it to store variables for each ball created and update and draw functions. But every time I end up with some sort of error and I can't find any help on something which is apparently so simple.

#include <iostream>

#include <SFML\Audio.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Network.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>


class Ball
{
public:

    // vector for position
    sf::Vector2 pos(100,100);

    // vector for velocity
    sf::Vector2 vel(0,0);

    void update()
    {
        // factors influence velocity
        // update position based on velocity
        pos.x += vel.x;
        pos.y += vel.y;
    }

    void draw()
    {
        // draw ball to the window using position vector
        sf::circleShape circle(10);
        circle.setPosition(pos.x,pos.y);
        circle.setFillColor(sf::Color::White);
        window.draw(circle);
    }
};

int main()
{
    /*create window settings*/
    sf::ContextSettings settings;
    settings.antialiasingLevel = 8; // set the antialiasing level

    /*create window*/
    sf::RenderWindow window;
    window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings);

    /*create ball(s)*/
    Ball ball01;

    /*Main loop*/
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::White);

        // call ball.update(); and ball.draw();
        ball01.update();
        ball01.draw();

        window.display();
    }
}

Any help or crit would be greatly appreciated, thanks in advance!


Solution

  • Try this snippet:

    1) Your background color should be different from the ball color.

    2) Use window.setFramerateLimit() to slow down animation.

    3) Use boundary condition so that the ball do not go out of view .

    4) Draw() needs correction as well

    class Ball
    {
    public:
    
        // vector for position
        sf::Vector2f pos{ 100, 100 };
    
        // vector for velocity
        sf::Vector2f vel{ 1, 1 };
    
        void update()
        {
            // factors influence velocity
            // update position based on velocity
            pos.x += vel.x;
            pos.y += vel.y;
    
            if (pos.x > 800 || pos.x < 0) vel.x = -vel.x; //boundary cond
            if (pos.y > 600 || pos.y < 0) vel.y = -vel.y; //boundary cond
        }
    
        void draw(sf::RenderWindow& window)
        {
            // draw ball to the window using position vector 
            sf::CircleShape circle(10);
            circle.setPosition(pos.x, pos.y);
            circle.setFillColor(sf::Color::White);
    
            window.draw(circle);
        }
    };
    
    int main()
    {
        /*create window settings*/
        sf::ContextSettings settings;
        settings.antialiasingLevel = 8; // set the antialiasing level
    
                                        /*create window*/
        sf::RenderWindow window;
        window.create(sf::VideoMode(800, 600), "Simple Physics", sf::Style::Default, settings);
    
        /*create ball(s)*/
        Ball ball01;
    
        window.setFramerateLimit(60); //slow down speed
        /*Main loop*/
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                // "close requested" event: close the window
                if (event.type == sf::Event::Closed)
                    window.close();
            }
    
            window.clear(sf::Color::Black); // ball is white so make backgnd black
    
            // call ball.update(); and ball.draw();
            ball01.update();
            ball01.draw(window);
            window.display();
        }
    }