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

Author Topic: (SOLVED) GetPosition is producing strange results  (Read 1397 times)

0 Members and 1 Guest are viewing this topic.

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
(SOLVED) GetPosition is producing strange results
« on: March 01, 2015, 04:24:06 pm »
I've started to learn SFML. Before I attempt to make my own game, I'm trying to make a simple pong game.

In order to keep my paddles from going off screen, I've done as follows:

Code: [Select]
//we make sure that the paddles cannot move outside the window
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                //we make sure that the paddles cannot move outside the window
                if (position.y < wH - paddleSize.y/2)
                {
                    paddleShape.move(velocity * dt);
                }
            }
            else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                if (position.y > 0 + paddleSize.y / 2)
                {
                    paddleShape.move(-velocity * dt);
                }
            }

And it works great. But I cannot for the life of me figure out how to keep the ball on screen. I'm just trying to make it go up and down in a straight line before I worry about moving along the x axis but I can't do it.

In the SFML pong example they do this:
Code: [Select]
if (ball.getPosition().y - ballRadius < 0.f)
{
        //Stuff
}
if (ball.getPosition().y + ballRadius > gameHeight)
{
        //Stuff
}

I'm trying to do the exact same checks on my ball but it refuses to work. When I try and keep track of my ball's Y value in the debugger, I get some really large values and it fluctuates like crazy. Same goes for the paddles but it works fine there for some reason. I thought getting the Y position would be relative to the window and since the window height is 600 the value would be between 0-600 if I were to stop it from going outside the window. But Even before it leaves the window the Y value reads as over a couple of thousand.
Not even sure if I am making any sense but here's a video.
[vid]http://pahlavan.se/dump/2015-03-01_15-40-35.webm[/vid]
The values being printed are supposed to represent the ball's Y value, but it goes all crazy.

The code for my ball is like this right now:

Code: [Select]
void Ball::movement(float dt, int wW, int wH)
{
    ballShape.move(velocity * dt);

    if (ballShape.getPosition().y - ballSize < 0.f) //Ball starts  going down if true
    {
        velocity = -velocity;
    }
    else if (ballShape.getPosition().y + ballSize > wH)  //Ball starts  going upif true, wH is equal to the window height, in this case 600
    {
        velocity = -velocity;
    }
}

It's just supposed to flip the velocity if it reaches the edge but I've stared at this for long I'm going blind. Something is really wrong since I thought I would be getting nice values from 0 and up when it moves down the screen, but instead I get those crazy values shown in the video. Which for some reason work fine for the paddles but not the ball.

Any help would be greatly appreciated.

And in case you guys would like to take a closer look, you can see all of my awful code at: https://github.com/PeymanT/Pong/tree/master/Pong
« Last Edit: March 01, 2015, 10:37:29 pm by Toby »

Toby

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: (SOLVED) GetPosition is producing strange results
« Reply #1 on: March 01, 2015, 10:37:53 pm »
Never mind, I was being stupid and goofed up a variable in the constructor. Works fine now.

 

anything