Jump to content
  • Advertisement
Sign in to follow this  
superman3275

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

This topic is 2125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.

#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;
}

So what do you think?

Share this post


Link to post
Share on other sites
Advertisement
This maybe?


rBluePaddle.Right = rBluePaddle.Top + sBluePaddle.GetSize().x;


rRedPaddle.Right = rRedPaddle.Top + sRedPaddle.GetSize().x;

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

Im coding it from scratch. #IgoogledMyMistakesIhaveTooMany (I wonder if thats a real twitter handle?)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!