2d pong movement

Started by
3 comments, last by silverphyre673 18 years, 4 months ago
I'm making 2d Pong with C++ and the SDL library. I just now wrote my own collision system so now thats working. My question is how do I control the movement of the ball. I'm used to really basic languages like Dark Basic in which you just rotate the image and move the image in the direction it's facing. I'm guessing it doesn't work like that here [cool] So how exactly would I control the movement and rotation of the ball? Would I have a ballxpos, ballypos, and ballangle variables. If so, how would I make the actual ball rotate and move in the new direction? I've been trying to figure this out myself for a while but after about a week I'm fresh out of ideas. Thanks, -Mason-
Advertisement
Is the problem that you dont know how to handle the graphical movement, or that you dont know how to do the physics?

I'm assuming that the question is graphical since you do have the collision detection up and running.

I'm sure you can find some good samples on the SDL site, I myself dont know SDL, but basically what you want is a "Render function" in your game loop that draws the bats and ball sprites (at their current position) to a backbuffer and the swaps them to the primary.
// Jens
I wouldn't have a ball angle. Instead, create a two-D vertex for its X and Y velocities. This will make it much more intuitive to change the direction and position of the ball. You will also need to store its position (another two-D vertex), and radius (for collision detection).

You need to make some sort of timer (SDL_GetTicks() returns the current time in milliseconds) for the purposes of updating the speed of the ball and paddles. For Pong, you can just multiply the speed of the ball by the number of milliseconds that have passed, and add this value to its position, like this:

position_x += speed_x * time_passed
position_y += speed_y * time_passed

When the ball hits a wall, generally (if the paddles are on the left and right sides of the screen) you reverse its Y velocity and keep the X velocity the same. Do the reverse when it hits a paddle. However, you might want to randomly add or substract from its Y velocity when it hits a paddle, to make it look like the ball is bouncing off in a different direction.

Collision detection can be as simple as seeing if the ball's position plus its radius is within the bounds of the paddles, and then moving it in the opposite direction of the paddle to a position where they are no longer touching, and then changing its speed so it's moving away from the paddle.

If you're wondering about graphics movement, you simply render the ball at an offset equal to its x and y position.

Hope this helps, otherwise clarify the questions you have further.
my siteGenius is 1% inspiration and 99% perspiration
Quote:Original post by silverphyre673
Instead, create a two-D vertex for its X and Y velocities.


READ: Vector. ;)

Oops! I meant vector! That's what you get for posting at 2:30 in the morning. Yeah...
my siteGenius is 1% inspiration and 99% perspiration

This topic is closed to new replies.

Advertisement