I was wondering how you would go about trying to change the direction of the ball when a collision happens a little relative to the angle that the ball hit at, rather than what I have been doing and just changing the direction of the x or y axis. Could someone show this in code, and why they did what they did and why what I am doing is wrong? Also, if someone wants bonus points, could you tell me why the ball trips out when it hits the top or bottom of a paddle?
#include <iostream>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
int main()
{
srand(time(0));
const int WINDOW_WIDTH = 720;
const int WINDOW_HEIGHT = 480;
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "PONG BOIII");
window.setFramerateLimit(60);
sf::Event event;
///////paddles///////
const int PADDLE_WIDTH = 25;
const int PADDLE_HEIGHT = 100;
sf::RectangleShape paddle1(sf::Vector2f(PADDLE_WIDTH, PADDLE_HEIGHT));
paddle1.setOrigin(PADDLE_WIDTH / 2, PADDLE_HEIGHT / 2);
paddle1.setPosition(50, WINDOW_HEIGHT / 2);
sf::RectangleShape paddle2(sf::Vector2f(PADDLE_WIDTH, PADDLE_HEIGHT));
paddle2.setOrigin(PADDLE_WIDTH / 2, PADDLE_HEIGHT / 2);
paddle2.setPosition(WINDOW_WIDTH - 50, WINDOW_HEIGHT / 2);
float AIspeedDown = 0.5;
float AIspeedUP = -0.5;
/////the ball////////
const int BALL_RADIUS = 10;
float bX = 0.5;
float bY = 0.5;
sf::CircleShape theBall(BALL_RADIUS);
theBall.setPointCount(300);
theBall.setOrigin(BALL_RADIUS / 2, BALL_RADIUS / 2);
theBall.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
theBall.setFillColor(sf::Color::Cyan);
sf::Clock clock;
sf::Time time;
while (window.isOpen()) {
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
} //ends events
//moves paddle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
paddle1.move(0, -0.5 * time.asMilliseconds());
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
paddle1.move(0, 0.5 * time.asMilliseconds());
}
//checking window collision
if (paddle1.getPosition().y - paddle1.getOrigin().y <= 0) {
paddle1.move(0, 0.5 * time.asMilliseconds());
}
if (paddle1.getPosition().y + paddle1.getOrigin().y >= WINDOW_HEIGHT) {
paddle1.move(0, -0.5 * time.asMilliseconds());
}
//ball stuff
theBall.move(bX * time.asMilliseconds(), bY * time.asMilliseconds());
if (theBall.getPosition().y <= 0 || theBall.getPosition().y >= WINDOW_HEIGHT) {
bY *= -1;
}
if (theBall.getPosition().x >= WINDOW_WIDTH) {
theBall.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
bX = 0.5;
}
if (theBall.getPosition().x <= 0) {
theBall.setPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
bX = 0.5;
}
if (theBall.getGlobalBounds().intersects(paddle1.getGlobalBounds())) {
bX *= -1;
}
if (theBall.getGlobalBounds().intersects(paddle2.getGlobalBounds())) {
bX *= -1;
}
//AI for enemy paddle
if (paddle2.getPosition().y < theBall.getPosition().y) {
paddle2.move(0, AIspeedDown * time.asMilliseconds());
}
if (paddle2.getPosition().y > theBall.getPosition().y) {
paddle2.move(0, AIspeedUP * time.asMilliseconds());
}
time = clock.restart();
////////update positions//////
//////////////////////////////
window.clear(sf::Color::Blue);
//draw here//
window.draw(paddle1);
window.draw(paddle2);
window.draw(theBall);
/////////////
window.display();
} //ends game loop
return 0;
}