Sign in to follow this  
scheols

Pong Ball Angles?

Recommended Posts

Alright everything else besides the ball angles for pong I understand, Maybe its because its 12am, not sure, but how do you go about randomly moving the ball in different directions? Is it the "steps" that you increase the "y" point of the ball which changes the angle of the ball? Got a 100 x 100 pixel board for example, start in middle of board 50x50 If the ball goes left and start increasing upward, I'm guessing how many steps you give the ball changes how the angle of the ping ball works (I think I understand it after a cup of cold water[lol what cold water does to the brain]) So going left at 0 (increasing steps by 3), and increasing steps by 3 end up at I say around 16/17 pixels on the y axis then it will collide of the top wall and then go the opposite direction. Am I some what near how thats working out?

Share this post


Link to post
Share on other sites
Quote:
Am I some what near how thats working out?

Your approach is needlessly complicated.

The ball has a position and a velocity, the former expressed as a coordinate pair of offsets onto the field of play (so (50, 50) would be the middle of your 100x100 play area) and the latter expressed in logical game units per frame, for simplicity. (Later you can specify your velocity in logical units per second, then calculate the per-frame displacement based on the current frame rate.)

Each frame, the ball's position is updated as old_position + (velocity * frame_step). In our simplified model, frame_step is always 1, but keep in mind that in properly designed games, you'd receive a time interval since the last frame/computation and need to interpolate your results. Once the new position has been computed, it is tested to see if it is out of bounds. Testing against a vertical barrier requires flipping the horizontal component of the velocity if the ball's colliding or past the barrier, and testing against the horizontal barrier requires flipping the vertical - in idealized mathematical form, you "reflect over surface normal."

That's a lot of words to basically say:
while gameIsRunning:
# compute the new position by adding the displacement per frame - velocity * 1 frame
#
new_position.x = old_position.x + velocity.x
new_position.y = old_position.y + velocity.y

# keep the ball within bounds, and change the velocity direction if necessary
#
if new_position.x < left_barrier:
new_position.x = left_barrier
velocity.x = -(velocity.x)
if new_position.x > right_barrier:
new_position.x = right_barrier
velocity.x = -(velocity.x)
if new_position.y < top_barrier:
new_position.y = top_barrier
velocity.y = -(velocity.y)
if new_position.y > bottom_barrier:
new_position.y = bottom_barrier
velocity.y = -(velocity.y)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this