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

Author Topic: Algorithm for moving the ball in pong game  (Read 3252 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Algorithm for moving the ball in pong game
« on: August 17, 2013, 03:24:03 pm »
Hello

I am working on a pong game and I need some clue on how to make the ball move automatically. Like, when the game starts, the ball is stationary and at the center of the screen and when the player hits space bar, it starts t move towards the player's paddle and after colliding, moves automatically according to the angle of collision.

Thank You

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Algorithm for moving the ball in pong game
« Reply #1 on: August 17, 2013, 03:56:08 pm »
You could look at the Pong SFML Example provided with SFML sources.

It's really easy to do, you just have to start the ball to move when space is pressed and then change the ball direction/angle each time it encounters a wall or a player

datMoka

  • Newbie
  • *
  • Posts: 48
  • Wooo!
    • View Profile
Re: Algorithm for moving the ball in pong game
« Reply #2 on: August 17, 2013, 07:19:35 pm »
All you need is a velocity for the x and y and then move the ball by the velocity. Simply reverse the velocity when the ball hits the bat or wall. As for pressing space, just initialize the velocity as 0 then set it to whatever speed you want :)