Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Breakout physics

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Breakout physics
Evert
Member #794
November 2000
avatar

Malinus said:

Yup that part i understand (just like light). But what when the ball hits the paddle?

Same.
To make things more interesting, you can add an impulse to the ball in the direction the paddle is moving at the time, or allow the paddle to be tilted.

Malinus
Member #11,332
September 2009

Evert said:

Malinus said:
Yup that part i understand (just like light). But what when the ball hits the paddle?

Same.
To make things more interesting, you can add an impulse to the ball in the direction the paddle is moving at the time, or allow the paddle to be tilted.

Well I think I will do as they did in the old good arkanoid (or newer DX-ball). Just divide the paddle into 6 zones. 3 left zones and 3 right zones. With the little twist that every of the 6 angles will have a little random interval, so no "stuck-ball" occurs.

I´m glad that games don´t have to follow the rules of real world physics ;D

OnlineCop
Member #7,919
October 2006
avatar

Some versions of Breakout provide some sort of enhancement for the player's paddle (or for the game itself), like "slower ball", "stick on paddle until mouse-click", "shoot lasers to also pop tiles", etc.

You could do the same kind of thing: turn on gravity, make the paddle "bounce" the ball back (like bouncers in pinball machines), change from a 6-angle paddle (like you were just talking about) to a totally flat paddle, etc.

But that's just for your TODO list down the road, once everything else gets up and going.

Malinus
Member #11,332
September 2009

Thanks for the ideas OnlineCop.

Now that the "engine" of the game is working I found a other problem... When having a fps about 60, and a 800x600 resolution, you have to set the speed to about 5 before the ball moves fast. Setting speed to 5 will make the ball look like it jumps. When only updating the ball once pr. frame how can I avoid this problem?

The only way I can see to work around this problem is setting the fps to 300 and then having the speed on 0.5-1.5...

OnlineCop
Member #7,919
October 2006
avatar

How is your logic being updated, according to time? Do you pass in the delta time, the current number of "ticks", or are you simply calling your update() function without using any sort of "time measurement"?

Also, logic updates and screen updates rarely need to be set at the same speed. You may only want to do logic updates a few times (10 or 20 times for your 60-fps setup), and then draw every single frame (drawing is actually quite CPU-intensive, so that may not be so great), or fiddle with some of your settings.

I have one game where you can moderate your FPS. Below 20 FPS, the game really starts to stutter noticeably. Stepping up from below 20 FPS to around 30 is when you can tell that the "jumping" gets smoothed to almost non-existent. Between 30 and 60 FPS, there's not major difference, and above that (up to 200 FPS, where I cap it), there's no change at all (though it eats up more CPU).

Working with an actual "delta_time" being passed into my update() function was the best thing for me. At 200 FPS, my delta time was in the order of "1/200th" of a second (well, due to processing time, maybe "1/198th").

Check out how I have the code (at least the updating system) in the attachment on this thread. While I didn't go with the Allegro timers (I used the BZflag's code), the way that the game gets updated is with delta times, and you can change the FPS with the '-/+' keys.

Malinus
Member #11,332
September 2009

OnlineCop said:

How is your logic being updated, according to time? Do you pass in the delta time, the current number of "ticks", or are you simply calling your update() function without using any sort of "time measurement"?

I´am simply using the standart mainloop model that looks something like this:
Wiki mainloop.

OnlineCop
Member #7,919
October 2006
avatar

The trouble with the design presented on the wiki is, as mentioned earlier, that you don't get any sort of delta to compare your timing against. It essentially assumes that all updates are the same, regardless of framerate. And from experience, I have personally seen most programs jump between a single tick, to multiple ticks, each time the logic gets updated. That's due to other programs hogging the CPU (good old Firefox, IE 8, Safari, Opera, and any other tabbed browser that likes to refresh its tabs on a regular interval), which throws off the exact "tick" count in odd places.

The wiki shows this kind of construct:

#SelectExpand
1volatile int ticks; 2void timer(void) {ticks++;} 3 4... 5 6install_int(timer, 1000.0 / FPS); 7 8... 9 10while (1) { 11 if (game_ticks < ticks) { 12 poll_input(); 13 handle_game_tick(); 14 game_ticks++; 15 need_redraw = true; 16 } 17 else if (need_redraw) { 18 render_last_frame(); 19 need_redraw = false; 20 } 21 22 rest(1); 23}

However, I'd suggest modifying that a little.

(Warning: untested)

#SelectExpand
10int old_ticks = ticks; 11while (1) { 12 double delta_time = (ticks - old_ticks) / (1000.0 / FPS); 13 old_ticks = ticks; 14 15 if (game_ticks < ticks) { 16 poll_input(); 17 18 handle_game_tick(delta_time); 19 20 game_ticks++; 21 need_redraw = true; 22 } 23 else if (need_redraw) { 24 render_last_frame(); 25 need_redraw = false; 26 } 27 28 rest(1); 29}

With this, since the timer was set initially to "(1000.0 / FPS), then to get the delta time, you take the difference between the current and old ticks, and divide by that value. So if your FPS is set to 60, then the allegro timer will update your "ticks" once every (1000/60)th of a second (or roughly every 1/16th of a second). If the difference of (ticks - old_ticks) is "0", then your logic is simple: the ball's velocity gets moved "0" pixels. If the difference is "1", then the delta_time is around "0.06" seconds, meaning that your ball will move (velocity * 0.06 * [cos(angle) | sin(angle)]) pixels (depending on x- or y-direction).

Does that work in your program?

SiegeLord
Member #7,827
October 2006
avatar

Firstly, that's delta timing which is highly flawed. Secondly, you are implementing it using allegro timers which are not nearly precise enough to support it properly, even if there was a reason to use it.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Malinus
Member #11,332
September 2009

What do you suggest then?

OnlineCop
Member #7,919
October 2006
avatar

Personally, I stole the timing routine used in BZ Flag's sources and used that. They've already made it compile on Mac, Windows, and Linux, and it uses Mutexes (if needed), Windows' GetTickCount() (or whatever it is), and so on. 8-) The only change I made to it was to get rid of about 3-4 lines of code that was specific only to their game, and then change their header from "#include <windows.h>" to #include <winalleg.h>. (I'm not tooting my own horn here, but in this post, I've attached a game that has this already working properly. You can rip the code out of there as you'd like. Just be sure to give them credit if you leave it in your game.)

Malinus
Member #11,332
September 2009

So what you guys are saying, is that it is impossible to make timing for a breakout game, using the allegro4 timers only? Or at least without forcing 100% CPU because of having to update logics 300/s...

OnlineCop
Member #7,919
October 2006
avatar

Only that timing precision isn't wonderful when using Allegro 4. It gets close, sure, but since the library doesn't take an aggressive approach to getting the current "computer ticks since epoch", it's at the mercy of other background processes also competing for CPU time.

To get really good precision on the architecture you're targeting, you basically need to ask the device/OS/hardware to provide you with its total number of "ticks" that have occurred. Since hardware itself updates this, even if you have a computer hogging 100% CPU time, it's still getting updated consistently.

But if you don't need "super-accurate" timing, the allegro timers will be just fine.

gnolam
Member #2,030
March 2002
avatar

Malinus said:

So what you guys are saying, is that it is impossible to make timing for a breakout game, using the allegro4 timers only? Or at least without forcing 100% CPU because of having to update logics 300/s...

300 updates/s is way too much. First off, the timers are too coarse (10 ms by default under Windows) to properly handle update frequencies that high. But more importantly, there is absolutely no point of running updates at a higher frequency than your display - which at best goes up to around 100 Hz. 50 Hz should be more than enough for your needs.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Malinus
Member #11,332
September 2009

gnolam said:

Malinus said:
So what you guys are saying, is that it is impossible to make timing for a breakout game, using the allegro4 timers only? Or at least without forcing 100% CPU because of having to update logics 300/s...

300 updates/s is way too much. First off, the timers are too coarse (10 ms by default under Windows) to properly handle update frequencies that high. But more importantly, there is absolutely no point of running updates at a higher frequency than your display - which at best goes up to around 100 Hz. 50 Hz should be more than enough for your needs.

I know, but when using 60Hz you will need a x- and yspeed that are higher then 2 if you want to have you ball go fast (at 800x600). This will of course lead to the ball "jumping", which looks very bad. So the only solution I can see is to force a very high FPS, which is possible in a simple game, but of course VERY inefficient... How can I get a smooth ball and having 60bps?

SiegeLord
Member #7,827
October 2006
avatar

You do know that if your monitor only refreshes at 60Hz and your ball moves faster than 1px per 1/60 seconds you are going to get jumping no matter how high your FPS goes?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Malinus
Member #11,332
September 2009

SiegeLord said:

You do know that if your monitor only refreshes at 60Hz and your ball moves faster than 1px per 1/60 seconds you are going to get jumping no matter how high your FPS goes?

Well it somehow still seams more smooth..

But my question still remains..

OnlineCop
Member #7,919
October 2006
avatar

You mean this question?

Malinus said:

So what you guys are saying, is that it is impossible to make timing for a breakout game, using the allegro4 timers only?

Breakout clones have been done many times in the forums. Some better than others, admittedly. Search through the last few years worth of posts and you should find some good pointers and tips on how, when others who have done it, everything was set up to allow for correct timing and updating (so it didn't look choppy/jumpy).

Malinus
Member #11,332
September 2009

OnlineCop said:

Breakout clones have been done many times in the forums. Some better than others, admittedly. Search through the last few years worth of posts and you should find some good pointers and tips on how, when others who have done it, everything was set up to allow for correct timing and updating (so it didn't look choppy/jumpy).

Well it´s not that I havn´t been looking through other clones source codes, or looking at forums etc. etc.... But nowhere I was able to find a solution to this rather simple problem. I really can´t see how it can be possible to make ball go smooth AND fast (over 1pixel pr. update ). So the max speed of a ball can´t be more then 60 pixels/s ("1" it´s not a real definition - just a guiding number it propably wont look jumping with 80pixels/s either but at 200 it propably will). So all I ask for, is if someone will be willing to give me some guiding lines how to do a PROPER timing for a game with a ball as the main object.

OnlineCop
Member #7,919
October 2006
avatar

With what you've currently coded up, are you still seeing jerky movement? If so, could you post your code? We may be able to spot where the jerkiness comes from, and post better feedback once we know how to best respond based on your needs.

 1   2 


Go to: