Sign in to follow this  
superman3275

[SFML] How to set bounding boxes (Correctly)?

Recommended Posts

So I started SFML recently, and so far I've succeeded in getting pong to work somewhat. The problem that I'm having is that whenever I run this program it doesn't matter where the actual red or blue paddle is, whenever the green ball reaches the paddles y coordinate it bounces off nothing. I have no idea why this is happening and I'm still trying to debug. If anyone could offer some insight I'd be extremely grateful.
[CODE]
#include "stdafx.h"
#include <SFML/Graphics.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "PONG");
App.SetFramerateLimit(60);
sf::Image RedPaddle;
sf::Image BluePaddle;
sf::Image GreenBall;
if (!RedPaddle.LoadFromFile("images/RedPaddle.png"))
return EXIT_FAILURE;
if (!BluePaddle.LoadFromFile("images/BluePaddle.png"))
return EXIT_FAILURE;
if (!GreenBall.LoadFromFile("images/GreenBall.png"))
return EXIT_FAILURE;
sf::Sprite sRedPaddle(RedPaddle);
sf::Sprite sBluePaddle(BluePaddle);
sf::Sprite sGreenBall(GreenBall);
float ballx = 100;
float bally = 100;
sf::FloatRect rGreenBall;
sf::FloatRect rBluePaddle;
sf::FloatRect rRedPaddle;
sRedPaddle.Move(350, 550);
sBluePaddle.Move(350, 25);
sGreenBall.Move(375, 300);

while (App.IsOpened())
{
sf::Event Close;
while (App.GetEvent(Close))
{
if (Close.Type == sf::Event::Closed)
App.Close();
}
float ElapsedTime = App.GetFrameTime();
sBluePaddle.Move(ballx * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Left)) sRedPaddle.Move(-200 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) sRedPaddle.Move( 200 * ElapsedTime, 0);
sGreenBall.Move(ballx * ElapsedTime, bally * ElapsedTime);
rGreenBall.Top = sGreenBall.GetPosition().y;
rGreenBall.Left = sGreenBall.GetPosition().x;
rGreenBall.Bottom = rGreenBall.Top + sGreenBall.GetSize().y;
rGreenBall.Right = rGreenBall.Left + sGreenBall.GetSize().x;
rBluePaddle.Top = sBluePaddle.GetPosition().y;
rBluePaddle.Left = sBluePaddle.GetPosition().x;
rBluePaddle.Bottom = rBluePaddle.Top + sBluePaddle.GetSize().y;
rBluePaddle.Right = rBluePaddle.Top + sBluePaddle.GetSize().x;
rRedPaddle.Top = sRedPaddle.GetPosition().y;
rRedPaddle.Left = sRedPaddle.GetPosition().x;
rRedPaddle.Bottom = rRedPaddle.Top + sRedPaddle.GetSize().y;
rRedPaddle.Right = rRedPaddle.Top + sRedPaddle.GetSize().x;
if (rGreenBall.Intersects(rRedPaddle))
{
ballx = 200;
bally = -bally;
}
else if (rGreenBall.Intersects(rBluePaddle))
{
ballx = -200;
bally = -bally;
}
App.Clear();
App.Draw(sRedPaddle);
App.Draw(sBluePaddle);
App.Draw(sGreenBall);
App.Display();
}
return 0;
}
[/CODE]
So what do you think?

Share this post


Link to post
Share on other sites
I think what Jebbles has pointed out is certainly worth fixing. I'd like to ask you, though, how are you debugging? Are you stepping through your code with a debugger? I didn't learn to use a debugger for quite a while when I was more of a novice, but it's basically become a part of me so much that I couldn't live without it now. If you haven't tried stepping through your code and seeing how values are calculated and if they're what you expect, it's a good place to start.

Share this post


Link to post
Share on other sites
[quote name='superman3275' timestamp='1348022740' post='4981516']
Im coding it from scratch. #IgoogledMyMistakesIhaveTooMany (I wonder if thats a real twitter handle?)
[/quote]
I'm not sure if you're trying to answer my question about how your debugging or if you're answering something else... What compiler/IDE are you using? Visual Studio? Have you tried Visual Studio's debugger (assuming it's Visual Studio you're using)?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this