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

Author Topic: how to make a sprite bounce across screen  (Read 4124 times)

0 Members and 1 Guest are viewing this topic.

Programmy

  • Newbie
  • *
  • Posts: 19
    • View Profile
how to make a sprite bounce across screen
« on: August 08, 2018, 09:16:57 am »

 I'm having problems with implementation, mainly how do you make the sprite change direction and bounce when it hits the boundary I have made "m_bounds", and make it travel diagonally,and have managed to make the first sprite move but sprite 2 is just going through the wall, I'm still learning C++ so
a simple way to do it would be appreciated not very complex implementation :)

Game.h file
#pragma once

#include "Window.h"
#include "World.h"
#include "Snake.h"
#include "Eagle.h"
#include "Airjeti.h"




class Game
{
public:
        Game();
       
        ~Game();
       


        void setterWindow();
        Window* GetWindow();
       




private:
        Window m_window;
        sf::RenderWindow window;
        sf::VertexArray points;
       
        sf::Texture spaceship;
        sf::Sprite sprite;
       
        sf::Texture Airjet;
        sf::Sprite sprite2;

        sf::RectangleShape m_bounds1;
        sf::RectangleShape m_bounds2;
        sf::RectangleShape m_bounds3;
        sf::RectangleShape m_bounds4;

        Eagle eagle;
        World world;
        Airjeti airjeti;
        bool m_bounce;

       
};


 


Game.cpp file

#include "Game.h"
#include <SFML/Graphics.hpp>
#include <iostream>

Game::Game(): window(sf::VideoMode(800, 600), "Test",sf::Style::Default),points(sf::Points,4)
{
       
}
Game::~Game()
{

}
void Game::setterWindow()
{
       
       
        if (!spaceship.loadFromFile("window/spaceship.png"))
        {
                std::cout << "Failed to load" << std::endl;
        }
        sprite.setTexture(spaceship);


        if (!Airjet.loadFromFile("c:window/Eagle.png"))
        {
                std::cout << "Failed to load" << std::endl;
        }
        sprite2.setTexture(Airjet);
       
       
        sf::Vector2u size = Airjet.getSize();/////////////

        sprite2.setOrigin(0.100f,0.100f);

        sf::Vector2f increment(0.4f, 0.4f);
       
        sprite.setOrigin(0.100f, 0.100f);
       
        m_bounds1.setFillColor(sf::Color(150, 0, 0));
        m_bounds1.setOrigin(0.5f, 0.5f);
        m_bounds1.setSize(sf::Vector2f(10, 1000));

        m_bounds2.setFillColor(sf::Color(150, 0, 0));
        m_bounds2.setOrigin(0.5f, 0.5f);
        m_bounds2.setSize(sf::Vector2f(1000, 10));

        m_bounds3.setFillColor(sf::Color(150, 0, 0));
        m_bounds3.setOrigin(10,-590 );
        m_bounds3.setSize(sf::Vector2f(1000, 10));

        m_bounds4.setFillColor(sf::Color(150, 0, 0));
        m_bounds4.setOrigin(-790, 10);
        m_bounds4.setSize(sf::Vector2f(10, 1000));


        sf::Event event;
        while (window.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                        window.close();

               
                 
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        sprite.move(-5, 0);
                }
                 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                 {
                        sprite.move(+5, 0);
                 }
                 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                 {
                        sprite.move(0, -5);
                }
                 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        sprite.move(0, +5);
                }

        }
       
       
        if (sprite2.getGlobalBounds().intersects(m_bounds3.getGlobalBounds()))
        {
               
               
                sprite2.move(-10, +10);
               
               
        }

        window.clear(sf::Color::White);
        sprite2.setPosition(sprite2.getPosition() + increment);
        window.draw(sprite);
        window.draw(m_bounds1);
        window.draw(m_bounds2);
        window.draw(m_bounds3);
        window.draw(m_bounds4);
        window.draw(sprite2);
        window.display();
       
       
}


Window* Game::GetWindow()
{

        return &m_window;
}



 

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: how to make a sprite bounce across screen
« Reply #1 on: August 27, 2018, 06:25:44 pm »
Physics implementations such as bouncing sprites require some more math. Google "c++ bounce" you'll get many examples on how to make it work such as

http://www.cplusplus.com/forum/beginner/192045/
https://stackoverflow.com/questions/19475213/bouncing-ball-issue

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: how to make a sprite bounce across screen
« Reply #2 on: August 29, 2018, 04:01:36 pm »
There is also an example on the SFML wiki, here:
https://github.com/SFML/SFML/wiki/Source%3A-Bouncing-ball
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything