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

Author Topic: Ball movement and bouncing in the Pong example  (Read 13221 times)

0 Members and 1 Guest are viewing this topic.

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Ball movement and bouncing in the Pong example
« on: August 07, 2011, 11:35:59 pm »
I am trying to recreate pong to help me along with my learning on C++ and SFML, but I've ran into a few problems when trying to make the ball move and bounce.

I managed to get the ball to move on its own, and to bounce off the walls and the paddles easily, but the problem was that it didn't seem "realistic". The ball was always bouncing at the same angle, as what I did was this:

Code: [Select]
float velX = 0.15f;
float velY = 0.35f;

// Move the ball in the game loop
Ball.Move(velX * App.GetFrameTime(), velY * App.GetFrameTime());

// When the ball hits the top or bottom of the screen
velY = -velY;

// When the ball hits a paddle
velX = -velX;


Doing it like this meant that the user has no control over where the ball goes, as it would just bounce around at the same angle and be pretty boring.

I reasoned that to do this I'd need to use trigonometry, so I attempted that.

Correct me if I'm wrong, but this is what I did to try and work it out:

where x is the angle I want the ball to move, and hyp is the ball's speed?

So to move the ball I would do:
Code: [Select]
Ball.Move(App.GetFrameTime() * ballSpeed * cos(ballAngle), App.GetFrameTime() * ballSpeed * sin(ballAngle));

Is this correct? And excuse the cos and sin bits there, I don't know how to do those in C++ yet.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #1 on: August 08, 2011, 04:51:31 am »
you need to convert to radians, so

instead of cos(ballAngle)
cos(ballAngle * (3.14159265 / 180))

otherwise i (think) you're good

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #2 on: August 08, 2011, 05:39:38 am »
You're good with the sine and cosine, just be sure to #include <cmath>
I use the latest build of SFML2

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #3 on: August 08, 2011, 01:26:54 pm »
Thanks a ton for that guys, was a big help :) I now have it how I want, but there's a new problem now that I've started to do it this way.

The ball's movement across the screen is no longer smooth and it jitters and jerks instead, which is not easy on the eyes :(

Edit: I could try to record it moving I suppose, if that helps you understand what I mean somewhat.

Here's my full code, in hopes of an easy solution:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <cmath>

int main()
{
// Create the App of the application
   sf::RenderWindow App(sf::VideoMode(640, 480, 32), "SFML Pong");
App.SetFramerateLimit(60);

sf::Shape Ball = sf::Shape::Circle(0, 0, 15, sf::Color::Black);
Ball.SetPosition(320, 240);

// Define PI
float PI = 3.14159265f;

// Ball properties
float ballSpeed = 0.2f;
float ballAngleRad = 90 * (PI / 180);

   while (App.IsOpened())
   {
      // Handle events
      sf::Event Event;
      while (App.PollEvent(Event))
      {
         // App closed or escape key pressed : exit
         if (Event.Type == sf::Event::Closed)            
            App.Close();          
      }

if (Ball.GetPosition().y + 15 > App.GetHeight())
{
ballAngleRad = -ballAngleRad;
}

if (Ball.GetPosition().y - 15 < 0)
{
ballAngleRad = -ballAngleRad;
}

Ball.Move(App.GetFrameTime() * ballSpeed * std::cos(ballAngleRad), App.GetFrameTime() * ballSpeed * std::sin(ballAngleRad));

      // Clear the App
App.Clear(sf::Color::White);

// Draw the ball
App.Draw(Ball);
       
      // Display things on screen
      App.Display();
   }

   return 0;
}

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #4 on: August 08, 2011, 10:47:25 pm »
That should be working perfectly. From your description, it sounds like your program is lagging, which doesn't make sense because of how simple it is. Are your drivers up to date?
I use the latest build of SFML2

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #5 on: August 09, 2011, 12:30:05 am »
They should be, but honestly I don't do most of that stuff on the pc I'm on at the moment. I'll take a look.

Edit : Derp. I ran it in release mode, seems to have fixed it. :)

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #6 on: August 09, 2011, 03:59:16 pm »
I have another quick question, so I just thought I'd post it here.

When bouncing the ball off of the paddle, this does not work:

Code: [Select]
ballAngleRad = -ballAngleRad;

The problem with this is that the ball's angle does not seem to change, and just passes right through.

I looked at the pong example which is so kindly provided, and tried this:

Code: [Select]
ballAngleRad = PI - ballAngleRad;

It fixes the problem, why is this?

jkalmeij

  • Newbie
  • *
  • Posts: 8
    • View Profile
Ball movement and bouncing in the Pong example
« Reply #7 on: August 09, 2011, 06:00:52 pm »
A full circle is 2 * PI radians.

With this knowledge, draw a circle, take an arbitrary angle and play around with it.

See also: Unit Circle