Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Detecting when an object is no longer visible to the user  (Read 2807 times)

0 Members and 1 Guest are viewing this topic.

DClipp

  • Newbie
  • *
  • Posts: 2
    • View Profile
Detecting when an object is no longer visible to the user
« on: September 02, 2011, 05:36:59 pm »
I'm trying to create a very simple Pong game on my own before I get into anything more complex. However, I can't seem to get the ball to bounce off of the sides of the screen. No matter what I've tried, it just goes offscreen every time. Can anyone help a noob out? The ball is 20x20px so I wanted it to bounce when its center is within 20 units of the edge of the screen. In the code below I've only (unsuccessfully) implemented a bounce for when it goes past the y-maximum as that is where it goes by default in the example so far.

Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create main window
srand ( time(NULL) );
sf::RenderWindow App(sf::VideoMode(640, 480), "Pong");
sf::Image playerimg;
if (!playerimg.LoadFromFile("paddle.png"))
{
// Error...
}

sf::Image computerimg;
if (!computerimg.LoadFromFile("paddle.png"))
{
// Error...
}

sf::Image ballimg;
if (!ballimg.LoadFromFile("ball.png"))
{
// Error...
}

int ball_x=300;
int ball_y=220;
int ball_start = 2;
int ball_count = 1;

sf::Sprite player;
sf::Sprite computer;
sf::Sprite ball;
player.SetImage(playerimg);
computer.SetImage(computerimg);
ball.SetImage(ballimg);
player.SetPosition(0,160);
computer.SetPosition(625,160);
ball.SetPosition(ball_x,ball_y);

bool ballIsVisible = true;
if (ball.GetPosition().y > 460) {
ballIsVisible == false;
}


    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

float ElapsedTime = App.GetFrameTime();
if (App.GetInput().IsKeyDown(sf::Key::Up))    player.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down))  player.Move(0,  100 * ElapsedTime);
if (ball_start==2)  ball.Move(150 * ElapsedTime,  150 * ElapsedTime);
if (ballIsVisible==false) ball.Move((-1 * (150 * ElapsedTime)),  (-1 * (150 * ElapsedTime)));



        App.Clear();

App.Draw(player);
App.Draw(ball);
App.Draw(computer);
App.Display();
    }

    return EXIT_SUCCESS;
}

Haikarainen

  • Guest
Re: Detecting when an object is no longer visible to the use
« Reply #1 on: September 02, 2011, 06:39:53 pm »
Quote from: "DClipp"

Code: [Select]
  if (ball.GetPosition().y > 460) {
      ballIsVisible == false;
   }



You gotta have that in your main loop.
The rest goes as follows;
Code: [Select]

if(ball.y < 0){
 bouncedown
}
if(ball.x < 0){
bounceright
}

if(ball.x > 640){
bounceleft
}

[/code]

DClipp

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Detecting when an object is no longer visible to the use
« Reply #2 on: September 02, 2011, 07:49:14 pm »
Quote from: "Haikarainen"
Quote from: "DClipp"

Code: [Select]
  if (ball.GetPosition().y > 460) {
      ballIsVisible == false;
   }



You gotta have that in your main loop.
The rest goes as follows;
[code]
if(ball.y < 0){
 bouncedown
}
if(ball.x < 0){
bounceright
}

if(ball.x > 640){
bounceleft
}

[/code

You, sir, are a genius. Thanks!! :D

Valeour

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Detecting when an object is no longer visible to the use
« Reply #3 on: September 04, 2011, 11:26:28 am »
Quote from: "DClipp"

Code: [Select]
  if (ball.GetPosition().y > 460) {
      ballIsVisible == false;
   }



Wait, can you use the '==' operator to assign stuff too when it's not in an "if" statement?

I thought it was purely for if statements...
Check out my blog; Technical Daze

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Detecting when an object is no longer visible to the user
« Reply #4 on: September 04, 2011, 12:21:44 pm »
Quote
Code: [Select]
ballIsVisible == false;

Should be marked with a warning from the compiler (with the right option, like -Wall) because the result is unused.

Quote
I thought it was purely for if statements...
Yes and no. You can us it in a while statement or more generally in any statement; for example the following is valid.

Code: [Select]
bool result = true == true;
SFML / OS X developer

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Detecting when an object is no longer visible to the use
« Reply #5 on: September 04, 2011, 12:22:16 pm »
Quote from: "Valeour"
Quote from: "DClipp"

Code: [Select]
  if (ball.GetPosition().y > 460) {
      ballIsVisible == false;
   }



Wait, can you use the '==' operator to assign stuff too when it's not in an "if" statement?

I thought it was purely for if statements...
No you can't, it's an equality operator, it'll return true or false, and not assign anything. You can use it anywhere you want. Though like that, it won't actually do anything other than create a temporary bool variable.

 

anything