SFML floatrect.intersect() Not Working

Started by
5 comments, last by superman3275 11 years, 7 months ago

#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");
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 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)
App.Close();
}
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);
App.Clear();
App.Draw(sRedPaddle);
App.Draw(sBluePaddle);
App.Draw(sGreenBall);
App.Display();
}
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.

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
Decrease the size of bally, to like 10. Also move the "sGreenBall.Move" line above the "rGreenBall.Top = sGreenBall.GetPosition().y;" line.
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 on top of 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 movement 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.
Thank you, I would give both of you guys 1-Up's but I seem to have reached my quota.

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 !

Well it still isn't working?!?!?! I did everything you said?

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 !


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?

The Problem: Your ball is going through your paddle without colliding.
Your guess: That the API is doing something wrong, and Intersect() isn't working.
My question to validate or dismiss your guess: 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.

My guess: 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.
My question to validate or dismiss my guess: 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 someone else gives you, it is understanding 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.
Okay, I figured out the problem. I was making the paddle move AFTER drawing the rectangles and testing for collision. Fixed. Also, TY for the course in asking questions, will definitely use that in my next post.

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