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

Started by
4 comments, last by superman3275 11 years, 7 months ago
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?

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Advertisement
This maybe?


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


rRedPaddle.Right = rRedPaddle.Top + sRedPaddle.GetSize().x;
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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Im coding it from scratch. #IgoogledMyMistakesIhaveTooMany (I wonder if thats a real twitter handle?)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !


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)?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Yes I do use visual studio, and you made me start using the debugger. TY :)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

This topic is closed to new replies.

Advertisement