Sign in to follow this  

SFML floatrect.intersect() Not Working

Recommended Posts

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int _tmain(int argc, _TCHAR* argv[])
const float PI = 3.14159f;
sf::RenderWindow App(sf::VideoMode(800,600,32), "PONG");
sf::Image RedPaddle;
sf::Image BluePaddle;
sf::Image GreenBall;
if (!RedPaddle.LoadFromFile("images/RedPaddle.png"))
if (!BluePaddle.LoadFromFile("images/BluePaddle.png"))
if (!GreenBall.LoadFromFile("images/GreenBall.png"))
sf::Sprite sRedPaddle(RedPaddle);
sf::Sprite sBluePaddle(BluePaddle);
sf::Sprite sGreenBall(GreenBall);
float ballspeed = 400.f;
float ballx = 0;
float bally = 100;
int x = 3;
sf::FloatRect rGreenBall;
sf::FloatRect rBluePaddle;
sf::FloatRect rRedPaddle;

while (App.IsOpened())
sf::Event Close;
while (App.GetEvent(Close))
if (Close.Type == sf::Event::Closed)
float gElapsedTime = App.GetFrameTime();
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;
sGreenBall.Move(ballx * gElapsedTime, bally * gElapsedTime);
if (rGreenBall.Intersects(rRedPaddle))
ballx = -ballx;
bally = -bally;
if (rGreenBall.Intersects(rBluePaddle))
ballx = -ballx;
bally = -bally;
float ElapsedTime = App.GetFrameTime();
if (App.GetInput().IsKeyDown(sf::Key::Left)) sRedPaddle.Move(-200 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) sRedPaddle.Move( 200 * ElapsedTime, 0);
return 0;
It doesn't work, even though the sprite should start moving the other way whenever the ball hits the paddle, but it doesn't work.

Share this post

Link to post
Share on other sites
Is it possible your ball is moving so fast, one frame it's on one side of the paddle, and the next frame it's on the other side, and never actually is [u]on top of[/u] the paddle?
Intersect() only checks if an object currently intersects, not if it moved past. In this situation, think of your movement as actually teleportation. Any time you change the position of something, even one pixel, you are teleporting it to the new position, not moving it. Any [i]movement [/i]is an illusion generated from many small steps of teleportation. Is your ball teleporting too far, and in a single frame teleporting to the other side of the paddle?

I doubt Intersect() is broken, but you can test it yourself by making a minimalistic example that doesn't contain any moving objects.

Share this post

Link to post
Share on other sites
[quote name='superman3275' timestamp='1347917595' post='4981032']
Well it still isn't working?!?!?! I did everything you said?
I asked questions, I didn't give answers. Debugging code is the art of asking questions of yourself and then answering them to narrow down where the problem is.
You said you think Intersect() isn't working.
Have you tried testing the individual function, like I suggested?

[b]The Problem:[/b] Your ball is going through your paddle without colliding.
[b]Your guess: [/b]That the API is doing something wrong, and Intersect() isn't working.
[b]My question to validate or dismiss your guess: [/b]Writing a piece of code where Intersect() is working with no moving objects (the ball is stationary and the paddle is stationary), and is guaranteed to collide (the ball is actually inside the paddle), is Intersect() still returning false? If yes, it means Intersect() is broken. If no, it means Intersect() works fine and the cause of the problem isn't what you were guessing.

[b]My guess: [/b]The API is fine, and your code is the part that is wrong. My guess is that your ball in a single frame skips over the paddle entirely, thus never actually colliding with it.
[b]My question to validate or dismiss my guess:[/b] Does the ball move more in a single frame then the thickness of the paddle? One one frame, when you print out the numbers (or use a debugger), is its position entirely on one side of the paddle without intersecting, and on the next frame is it entirely on the other side without intersecting?

The actual work resides with you. You need to use these guesses, and the ways to prove them false or true, and actually work on your code. We can't tell you magically what is wrong with your code, all we can do is debug your code for you... which is actually unhelpful, because then you never debug your own code, and never learn how to become a programmer. So instead of "fixing" your code (which is us programming your game for you), we can actually really help by asking questions of your code to you, and you go test those questions and answer them (which is you programming and fixing your own game).

Programming isn't typing in code that [i]someone else[/i] gives you, it is [i]understanding [/i]code, coming up with your own code to write, and then asking questions to figure out what the problem is, when it doesn't work. It's fine that you don't understand code when learning, but the solution to not understanding is to ask more questions and answer them yourself by testing the results of the code, which leads to understanding.

I am helping by even telling you what questions to ask. But you have to answer those questions yourself, by trying them out in your program. If you're having trouble, what part of the question is giving your difficulty? We're happy to help you, but not to entirely take over and do it for you.

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