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

Author Topic: Question about bouncing  (Read 1837 times)

0 Members and 1 Guest are viewing this topic.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
Question about bouncing
« on: December 06, 2012, 04:35:56 pm »
How can I make the ball bounce back if it reaches the end of the screen?

#include <SFML/Graphics.hpp>
int main()
{
    int screen_w = 640;
    int screen_h = 480;
    sf::RenderWindow window(sf::VideoMode(screen_w,screen_h), "SAMPLE");
    sf::Image image;
    image.loadFromFile("ball.png");
    image.createMaskFromColor(sf::Color(255,0,255));
    sf::Texture texture;
    texture.loadFromImage(image);
    sf::Sprite sprite(texture);
    float ballx = 0.1;
    float bally = 0.0;
    while(window.isOpen())
    {
        sprite.move(ballx,bally);
        window.clear();
        window.draw(sprite);
        window.display();
               
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            window.close();
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
            ballx = -0.1;
            bally = 0.0;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
            ballx = 0.1;
            bally = 0.0;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
            bally = 0.1;
            ballx = 0.0;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
            bally = -0.1;
            ballx = 0.0;
            }

        }    
    }
}
 
« Last Edit: December 06, 2012, 04:42:33 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Question about bouncing
« Reply #1 on: December 06, 2012, 04:48:38 pm »
Don't use the sf::Keyboard with in the event loop, otherwise the checks will only happen when an event is triggered, which doesn't make sense, since sf::Keyboard doesn't have anything to do with events...

To 'bounce' a ball you can simply negate the X or Y speed, depending if it hits the top or botton (Y) or left or right (X).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Question about bouncing
« Reply #2 on: December 06, 2012, 04:58:46 pm »
Yes thats what I want but what will I use to make ballx negate when it reaches the end of X of screen?

Im trying this code but its wrong..
Quote
#include <SFML/Graphics.hpp>

int main()
{
    int screen_w = 640;
    int screen_h = 480;
    sf::RenderWindow window(sf::VideoMode(screen_w,screen_h), "SAMPLE");
    sf::Image image;
    image.loadFromFile("ball.png");
    image.createMaskFromColor(sf::Color(255,0,255));
    sf::Texture texture;
    texture.loadFromImage(image);
    sf::Sprite sprite(texture);
    float ballx = 0.1;
    float bally = 0.0;
    while(window.isOpen())
    {
        sprite.move(ballx,bally);
        window.clear();
        window.draw(sprite);
        window.display();

        sf::Event event;
        while(window.pollEvent(event))
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            window.close();
        }
        if(sprite.getPosition().x <= screen_w-20)
        ballx = -0.1;
    }
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Question about bouncing
« Reply #3 on: December 07, 2012, 12:58:02 am »
Of course it's wrong, although you don't explain what's wrong.
You want to invert ballx if your sprite.x reaches screen_w - 20: so you want to react if ballx is GREATER OR EQUAL than screen_w-20. ;)

(also, use the code tag, not the quote ^^)

 

anything