PDA

View Full Version : Pong



seiferalmasy
16-11-2006, 10:54 PM
Yup a simple game, and one which I have done pretty well on considering all code is mine and not 1 bit of research outside on how I would do it. Was very nice to create it myself. I am hoping to maybe make it into a "Final fantasy 7 Pong" complete with FF7 pictures, players etc.

Anyway, problem that surfaced is that if deltatime is large the collision misses and theerfore the ball goes through the paddle.

Now, this isnt an issue on my computer, but on a slower one it would be for certain.

I have made the paddles thicker to compensate but this will always remain an issue.

Is this one game where Framebased movement would be a better alternative or is there a way around this to programatically account for this problem?

Thanks again to all, my other original game complete with music is more or less done. This one is justa bit of fun (as a kid i always wanted to make pong!!)

grudzio
16-11-2006, 11:23 PM
Is this one game where Framebased movement would be a better alternative or is there a way around this to programatically account for this problem?


Before moving the ball, check if the distance between old and new ball's position is bigger then thickness of the paddle.
If no, do standard collision detection.
if yes, divide distance in offsets smaller then paddle's thicknes. Check collison for first offset. If there is no collision then add offset to ball's position and check with second offset and so on.

VilleK
17-11-2006, 08:40 AM
Yes this is a problem with deltatime-based engines.
The method grudzio suggests will work.
A variant on this is to set a MaxDeltaTimeCollision-value to say 0.1 seconds.
So if the main loops DeltaTime is 0.25 on a slow machine, you will loop around the collision code with three times, with Deltas 0.1 , 0.1 and 0.05.

Something like this maybe:

procedure CheckCollisions;
const
//Maximum time step for collision=1/10 second
MaxUpdateStep = 1.0 / 10;
var
TimeLeft,DT : single;
begin
TimeLeft := AppDeltaTime;
while TimeLeft>0 do
begin
if TimeLeft>MaxUpdateStep then
DT := MaxUpdateStep
else
DT := TimeLeft;
DoCheckCollisions(DT);
TimeLeft := TimeLeft - MaxUpdateStep;
end;
end;

czar
17-11-2006, 09:13 AM
That is an interesting approach VilleK - haven't seen that one before I have always used the same method as grudzio's solution.

So where is pong seiferalmasy?

VilleK
17-11-2006, 09:40 AM
Just realized I forgot to say you of course need to update your sprite positions as well:



procedure UpdatePositionsAndCollisionTests;
const
//Maximum time step for updates and collision=1/10 second
MaxUpdateStep = 1.0 / 10;
var
TimeLeft,DT : single;
begin
TimeLeft := AppDeltaTime;
while TimeLeft>0 do
begin
if TimeLeft>MaxUpdateStep then
DT := MaxUpdateStep
else
DT := TimeLeft;
MoveObjects(DT); //Update object positions
DoCheckCollisions(); //Test collisions on current positions
TimeLeft := TimeLeft - MaxUpdateStep;
end;
end;


This is a problem with games with physics simulations too, you need to iteratively call the simulation code with small timesteps.

But a quick cheat :) around all this for a small game like pong is to not allow AppDeltaTime get too large. This will make the game play slower on slow machines.
Another cheat is to make the collision areas larger.

Here is a Pong clone I've made: http://www.emix8.org/static.php?page=zpong

seiferalmasy
17-11-2006, 09:59 PM
Cheers for all that. Will study it:)

Anyone else do it any other way?