Hello, I am new here and still new to SFML. I have a pong game im working on in SFML 2.0 and i need help getting the ball to angle when it bounces off one of the paddles. I have tried to use the examples in this thread
http://en.sfml-dev.org/forums/index.php?topic=5515.0 and apply them to 2.0 but it either brakes my collision or the ball wont move. If anyone could give me some help or point me in the right direction that would be great.
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
sf::Clock Clock;
float Speed = 10000.0f;
float BallSpeed = 99.3f;
bool BallDirct;
int main()
{
float PI = 3.14159265f;
float BallAngleRad;
//Starts the ball in a random direction.
srand(time(0));
BallDirct = rand() % 2;
//Making window
sf::RenderWindow App;
App.create(sf::VideoMode(800, 600), "Pung");
//Making Shapes
sf::RectangleShape Bar1(sf::Vector2f(15, 125));
sf::RectangleShape Bar2(sf::Vector2f(15, 125));
sf::RectangleShape Ball(sf::Vector2f(20, 20));
Ball.setPosition(400, 300);
Bar1.setPosition(1, 250);
Bar2.setPosition(784, 250);
Clock.restart();
//Game Loop
while(App.isOpen())
{
sf::Event Event;
while (App.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
App.close();
break;
case sf::Event::KeyPressed:
if(Event.key.code == sf::Keyboard::Escape)
{
App.close();
}
break;
}
//Movment
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
Bar1.move(0, -Speed * Clock.getElapsedTime().asSeconds());
if(Bar1.getPosition().y < 0)
Bar1.setPosition(1, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
Bar1.move(0, Speed * Clock.getElapsedTime().asSeconds());
if(Bar1.getPosition().y > 491)
Bar1.setPosition(1, 490);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
Bar2.move(0, -Speed * Clock.getElapsedTime().asSeconds());
if(Bar2.getPosition().y < 0)
Bar2.setPosition(784, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
Bar2.move(0, Speed * Clock.getElapsedTime().asSeconds());
if(Bar2.getPosition().y > 491)
Bar2.setPosition(784, 490);
}
}
//Collision and changes the balls direction and speed.
if(BallDirct == true)
{
Ball.move(-BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), -BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));
if(Bar1.getGlobalBounds().intersects(Ball.getGlobalBounds()))
{
BallDirct = false;
Ball.move(BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));
BallSpeed = BallSpeed + 10.5f;
}
}
if(BallDirct == false)
{
Ball.move(BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));
if(Ball.getGlobalBounds().intersects(Bar2.getGlobalBounds()))
{;
Ball.move(-BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), -BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));;
BallDirct = true;
BallSpeed = BallSpeed + 10.5f;
}
}
Clock.restart();
App.draw(Bar1);
App.draw(Bar2);
App.draw(Ball);
App.display();
App.clear();
}
return EXIT_SUCCESS;
}