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


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 <SFMLAudio.hpp>
#include <SFMLGraphics.hpp>
#include <SFMLNetwork.hpp>
#include <SFMLSystem.hpp>
#include <SFMLWindow.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!





I would encourage you to be very specific; what do you want Ball to do and what have you tried to achieve this? How should Ball update and render and what have you come up with?
– alter igel
Jun 30 at 0:59


Ball


Ball





But to get the ball rolling, so to speak, you can take a look at sfml's sf::Vector2f to store position and velocity, and note that Ball::draw() will need access to window in order to do any drawing. You can also consider using a std::vector<Ball> to store many balls, each of which you'd need to update and draw.
– alter igel
Jun 30 at 1:02



sf::Vector2f


Ball::draw()


window


std::vector<Ball>





Hi, I just updated to one of the previous things I tried, I understand most of it is probably wrong, but I can't work out from the errors I receive :/ How would I go about giving the draw function access to the window?
– Brodie Griggs
Jun 30 at 1:08






You're referencing main's window object, which is certainly not in scope inside Ball::draw(). Consider changing the function's signature to Ball::draw(sf::RenderWindow& window); and then passing window in from main
– alter igel
Jun 30 at 1:11


main


window


Ball::draw()


Ball::draw(sf::RenderWindow& window);


window


main





I understand, would it be void draw(sf::RenderWindow& window) instead of the void draw() ? Thanks very much for helping by the way!
– Brodie Griggs
Jun 30 at 1:15


void draw(sf::RenderWindow& window)


void draw()




1 Answer
1



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV