Public Group

# SDL Bouncing Ball?

This topic is 5457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

does any1 have an example or tutorial for a bouncing ball in C++ using SDL? thanks!

##### Share on other sites
Thats a prety broad question. Do you mean like animation, or physics? If you have the core concepts down, animation should be no harder than comming up with the algorithm.

You can either do this by having individual files for the cells, or by making one giant animation strip.

##### Share on other sites
sorry for the broad scope of the question. I mean the physics/code. Im working on a pong clone and have everything working as i want (just 2 paddles that move up and down and stop at the edges of the board.) All i need now is the ball! Tho ive given it thought and I think itll be the hardest part. So once again, can any1 help? :)

##### Share on other sites
hm, pong with physics could be interestng....

unlike with a real physics engine, pong changes the x velocity of the ball depending on how long the game has been going. the y velocity is dependant on where the ball hits the paddle.

distance of the vector component= velocity of the vector component multiplied by the change in time.

the change in time is usualy the time it takes to complete one cycle of the game.

Do a little research on bounding rectangles, that should help when you get to collision detection.

##### Share on other sites
On our SDL Tutorials page, if you scroll down you'll see we have a link to a bouncing penguin demo, with source. You can grab the penguin and chuck him around the box, and he bounces. It's pretty neat :)

Ryan

##### Share on other sites
Or, hes just new with sdl and he doesn't know what to do with the ball. I don't think he needs any of those fancy things with real physics...

##### Share on other sites
class Ball{  public:   int x,y;   int xvel,yvel;   void Update();   bool Collide_W_Paddle();}Ball::Update(){  x += xvel;  y += yvel;    if(Collide_W_Paddle())     xvel = -xvel;}

something like this should give you an idea. make a Ball instance, and each frame call Update() on it. you can search the forums or the articles section to learn how collision works.

##### Share on other sites
maybe i just dont know how to ask questions lol....im making a pong clone and need a ball in it (as you know)....i dont know of any function to draw a ball in SDL. so is there any1 that knows of such a function? collision/physics question has already been answered :-P just need a ball...thanks :) (sorry again for my horrible question asking abilities lol)

##### Share on other sites
Well, do you have paddles? If so, how did you display them? The same method can be used to display your ball. Simply create a bitmap with an image of a ball in it (use a paint program to draw a circle!) and then use SDL_LoadBMP to load the bitmap, and then SDL_BlitSurface to display it.

That's how you display any image, with the standard SDL library.

Ryan

##### Share on other sites
yea i made paddles already. just didnt know if u could draw a circle....i wasnt going to use a bitmap for the circle but will that be the best way?

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 10
• 9
• 15
• 22