Pong Development, Confusing with the Bouncing *After Collision <SFML 2.0>

Started by
2 comments, last by stitchs_login 10 years, 11 months ago


#include "Engine.h"
//Screen Resolution properties for proper values among positioning for renders.
sf::Vector2f Screen = sf::Vector2f(800,600);
//************************Constructor****************************
Engine::Engine()
{
//
}
//************************Deconstructor****************************
Engine::~Engine()
{
//
}
//************************What is rendered upon initialization****************************
bool Engine::init()
{
//Point to the window and create one for our use.
window = new sf::RenderWindow(sf::VideoMode(Screen.x,Screen.y,32), "Pong");
//Define the player Shapes
defineShapes();
//if the window fails return a negative response
if(!window)
return false;
//else continue
return true;
}
//************************Determine what keys perform Events****************************
void Engine::ProcessEvents()
{
sf::Event event;
//perform Events while the window is open.
while(window->pollEvent(event))
{
switch(event.type)
{
//Upon the selection of the close buttons on a window Close the window
case sf::Event::Closed:
window->close();
break;
//upon a Key that is pressed close the window
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape)
window->close();
break;


}


}


//--------------------------Call upon player Functions---------------------------


//movement settings for the first rectangle on the Left
movePlayerOne();
//movement settings for the Second rectangle on the Right
movePlayerTwo();
//Determine Collison factor and move the ball
movCollision();


}
//************************Draw to the Screen [Update]****************************
void Engine::Update()
{
//clear the window
window->clear(sf::Color(40,40,40));
//draw the first rectangle on the Left
window->draw(*playerOne);
//draw the Second rectangle on the Right
window->draw(*playerTwo);
//draw the Ball in the Center
window->draw(*Ball);


}
//************************Window Display Settings****************************
void Engine::RenderFrame()
{


window->display();


}
//************************Determine the actions after Start****************************
void Engine::Main()
{
while(window->isOpen())
{
ProcessEvents();
Update();
RenderFrame();
}
}
//************************Start the Engine****************************
void Engine::Go()
{
//if it doesn't initialize send an error message
if(!init())
throw "Could not initialize game loop";
//continue to main loop
Main();


}
//************************Movement for Player One****************************
void Engine::movePlayerOne()
{
//set the speed of the player
int speed = 10000;
//Create a clock
sf::Clock clock;
//Restart the clock
clock.restart();
//if W is pressed on the keyboard move Up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
playerOne->move(0,clock.getElapsedTime().asSeconds() * -speed);
}
//if S is pressed on the Keyboard move Down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
playerOne->move(0,clock.getElapsedTime().asSeconds() * speed);
}


}
//************************Movement for Player Two****************************
void Engine::movePlayerTwo()
{
//set the speed of the player 2
int speed = 10000;
//Create another clock
sf::Clock clock;
//Restart the clock
clock.restart();
//if Up is pressed on the Keyboard Move Up
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
playerTwo->move(0,clock.getElapsedTime().asSeconds() * -speed);
}
//if Down is pressed on the Keyboard Move Down
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
playerTwo->move(0,clock.getElapsedTime().asSeconds() * speed);
}


}




//************************Begin Defining Shapes****************************
void Engine::defineShapes()
{


//Define Player One Rectangle
playerOne = new sf::RectangleShape();
//Set the Player One Rectangle Size
playerOne->setSize(sf::Vector2f(16,64));
//Set the Player One Rectangle Color
playerOne->setFillColor(sf::Color(255,255,255,255));
//Set the Player One Rectangle Position
playerOne->setPosition(sf::Vector2f(40,20));


//Define Player Two Rectangle
playerTwo = new sf::RectangleShape();
//Set the Player Two Rectangle Size
playerTwo->setSize(sf::Vector2f(16,64));
//Set the Player Two Rectangle Color
playerTwo->setFillColor(sf::Color(255,255,255,255));
//Set the Player Two Rectangle Position
playerTwo->setPosition(sf::Vector2f(740,520));


//Define Ball
Ball = new sf::RectangleShape();
//Set the Ball Size
Ball->setSize(sf::Vector2f(16,16));
//Set the Ball Color
Ball->setFillColor(sf::Color(0,255,0,255));
//Set the Ball Position
Ball->setPosition(740, 520);
}




//************************Begin Defining Collision for the Ball and Players****************************
void Engine::movCollision()
{


//Store Player Positions into Vectors
sf::Vector2f playerOnePosition = sf::Vector2f(playerOne->getPosition());


sf::Vector2f playerTwoPosition = sf::Vector2f(playerTwo->getPosition());


sf::Vector2f BallPosition = sf::Vector2f(Ball->getPosition());


//Define Rectangle around the players
sf::IntRect playerOneRect(playerOnePosition.x,playerOnePosition.y, playerOne->getSize().x, playerOne->getSize().y);


sf::IntRect playerTwoRect(playerTwoPosition.x,playerTwoPosition.y, playerTwo->getSize().x, playerTwo->getSize().y);


sf::IntRect BallRect(BallPosition.x,BallPosition.y, Ball->getSize().x, Ball->getSize().y);


//set the clock for speed
sf::Clock clock;


//Restart the clock
clock.restart();








//determine position and velocity.
sf::Vector2f pos;
sf::Vector2f velocity;


velocity.x = clock.getElapsedTime().asSeconds() * -12000;
velocity.y = clock.getElapsedTime().asSeconds() * -600;


//Detect Collison
bool collisionPlayerTwo = BallRect.intersects(playerTwoRect);
bool collisionPlayerOne = BallRect.intersects(playerOneRect);


//If Collision 
if(collisionPlayerTwo && !collisionPlayerOne)
velocity.x = velocity.x; 


if(collisionPlayerOne && !collisionPlayerTwo)
velocity.x = -velocity.x;


pos += velocity;
Ball->move(pos);






}
 

Advertisement

You're not handling pHit correctly. Firstly, you're not initializing it. It does get set either way, but it gets set incorrectly because you're fiddling with it instead of initializing it.

Secondly, there are three possible collision cases here:

1) Ball hit paddle one.

2) Ball hit paddle two.

3) Ball didn't hit anything.

You need to handle all three cases.

Does

Ball->move(?, ?)

Change the position of the ball or its direction?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

both. I originally had 3 cases but I switched it to try and just get a half of the system working at least but failed. can you show me how I might properly initialize it? sorry, I'm fairly new to SFML and my C++ knowledge is kinda well, Everywhere. this is certainly teaching me the use of pointers finally though, hard as heck to understand from the books demonstrations for C++, but I can't really figure this one out :\ I suppose I may be able to go about this a different route.

instead of using ball->move(x,y);

I could use ball->setPosition(x++,y++) || (x--,y--);

I'm not even attempting for the randomness right now. All I am trying to achieve is a bounce.

If you are able to detect which sort of collision occurs, then all you have to do is reverse the velocity on that specific axis. You can achieve this by always adding velocity to position, then, if a collision occurs, multiply that value by negative one to reverse the sign of that speed.

After this you can implement a speed multiplier to make it go faster, but that can be something to research. ;)

Oh and there is also collision with the pong arena boundaries to handle.

Hope this helps,

Stitchs.

This topic is closed to new replies.

Advertisement