Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Question about timers

Author
Message
ChEeZeBaLL
12
Years of Service
User Offline
Joined: 5th Sep 2012
Location:
Posted: 26th May 2013 05:53
A year or so ago I wrote a Pacman clone using Allegro 4. In my collision / direction change logic, I have to move Pacman and the ghosts 1 pixel at a time (can't use float for x/y value or move them more than 1 pixel per loop).

In order to achieve Pacman and the ghosts moving at different speeds I set up a few different Allegro timers and had Pacman, the ghosts, and the frame rate on separate timers.

Is it possible to do something like this in AppGameKit? It seems that the main game loop is locked at 60 ticks per second. Would it possible to leave the frame rate at 60 fps but have logic timers for pacman and the ghosts running faster?
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 26th May 2013 10:08
Well if you're talking about timers as some kind of callback, then no, but timer based movement is always a good way of implementing things anyway as you can't predict exactly how fast the main loop will run.

AGK has the Timer() function which will return a floating point value of the number of seconds the machine has been running, and so you can use this to work out timings. However you cannot use this method to run faster than the main loop, because the code would have to be placed in the main loop. Mind if I ask why you can't move more than 1px at a time?

Anyhow the code for timer based movement would be something like:

Note that that code is untested. Hope that helps.

ChEeZeBaLL
12
Years of Service
User Offline
Joined: 5th Sep 2012
Location:
Posted: 26th May 2013 17:27
I can't move forward more than 1 pixel at a time because of the way direction changes work.

When you're moving forward and have an arrow key held down, my game checks to see if you're able to turn in that particular direction. You have to be dead center in an intersection to be able to turn.

If you're moving forward more than 1 pixel at a time, sometimes you'll skip over the center area and won't be able to change direction.
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 26th May 2013 20:14
I see. Well first off, I don't think AppGameKit loops guarantee a certain speed; rather, I think the loop tends to just go as fast as it can. Therefore you need to use the kind of timer based movement shown above anyway to ensure you move at a consistent speed. However that method can only guarantee the maximum speed, not the minimum. Obviously I've not seen your code so I don't know exactly how it works. However surely something along the lines of the following pseudocode should get around the main problem.



That way, you can check whether the player has passed over the opening in the last step, rather than just whether they are exactly there. Hope that helps.

Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 28th May 2013 20:24
Why not just make a check if it is dead center? if it's not, then move the player back to the center before changing direction

----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
ChEeZeBaLL
12
Years of Service
User Offline
Joined: 5th Sep 2012
Location:
Posted: 2nd Jun 2013 01:34
Maybe it's because I'm using the visual studio template, but my main loop is running at 60 iterations per second.

If I declare a counter integer and increase it by 1 on every iteration through the main loop and print the value divided by 60, it counts up 1 every second.
Naphier
14
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 2nd Jun 2013 05:40
I'm pretty sure that's typical unless you use SetSyncRate(0,1)
That's what I recall from tier 1.
That'll go down if you put a lot of load on the CPU, but you might not notice until you run something on an older device.
If you don't use physics you should be OK.
A single pixel is very difficult to land on. I'd suggest a zone of allowed direction change and then align the pacman at the same time as the direction change. It should be fairly imperceptible.

Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 2nd Jun 2013 10:13
Quote: "...but my main loop is running at 60 iterations per second."

I think this could be the stumbling block.

By attempting bend the program to fit this "objective" and rejecting any solution which doesn't, you are making the task more difficult than it needs to be.

Quote: " I have to move Pacman and the ghosts 1 pixel at a time (can't use float for x/y value or move them more than 1 pixel per loop)."

These are limitations imposed upon yourself because of the objective above and the existing code you are converting.

The parameters worked for you at a fixed 60fps, and you are reluctant to change because you want to keep the 60fps.

So...

I suggest starting with the assumption "I can't possibly get my main loop to run at 60fps on every device...", then a a whole host of options are available.

Login to post a reply

Server time is: 2024-11-24 13:41:22
Your offset time is: 2024-11-24 13:41:22