Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (754)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  I don't use an "elapsed time" for enemy/player movement. it's an error?  (Read 5121 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie

« Posted 2006-09-22 11:16:21 »

Hi, I've seen many game that use elapsed time to update the movement for player/enemies.

move(long elapsedTime)

My game call move method from gameloop and entity change their position without using "elapsedTime".

Is this an error or I'm doing right?

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline Anon666

Junior Devvie

aka Abuse/AbU5e/TehJumpingJawa

« Reply #1 - Posted 2006-09-22 12:24:02 »

Depends on whether you want fixed or variable time steps.
Each is suitable for different types of games.

There is no harm in giving your interfaces the capability of variable time steps, but having the implementation support only a fixed time step.
Offline darkprophet

Senior Devvie

Go Go Gadget Arms

« Reply #2 - Posted 2006-09-22 12:27:08 »

so your doing:

position.x += 0.1;

instead of:

position.x += 10*elapsedTime;


What you need to understand is that there are reasons for both ways. Your way, you must throttle the loop to get it around 60ish (or whatever) and ensure that the FPS doesn't drop below that. If it does drop below that, then your going to get speed ups in movements...

The elapsedTime method can change the movement with the frame rate so that it always looks the same; unfortunetly, its horrendous with physics as the step time changes from frame to frame resulting in not having a deterministic physics response....

what you could do for the time being, is code using the elapsedTime technique, but pass a constant (like 1/60) so that you can simulate constant time step which you can easily change later with a timer...


Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dishmoth
« Reply #3 - Posted 2006-09-22 12:41:57 »

If your game runs at a fixed frame rate (e.g., 30 frames per second) then you don't need to worry about movement based on "elapsed time" since the elapsed time will always have the same value (e.g., one 30th of a second).  I think that that's the case for a lot of games.

If your frame rate varies (in particular, if the game occasionally runs at a slightly lower frame rate than you're intending) then updating the game based on the elapsed time should help to make the game play smoothly.  (Of course, if the game slows down severely, then there's nothing you can do to make it feel smooth, and using the elapsed time may make it appear even more jerky.)

A simpler alternative to using elapsed time may be to increase the number of updates per frame if the frame rate drops.  For example, if you can do { move(); draw(); } 30 times a second then that's good, but if the game starts running slow then you could switch to doing { move(); move(); draw(); } 15 times a second instead, in which case the game will appear to play at the same speed.

Finally, I'd say that while updating the game based on the elapsed time is fairly common for 3D games, it isn't nearly as useful for 2D (sprite-based) games since the movement and animation is often a lot more rigid.


Offline Graziano Mesina

Senior Newbie

« Reply #4 - Posted 2006-10-03 09:14:28 »

How I can know If  my game runs at a fixed frame rate and if it is 30..40 etc fps?

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline dishmoth
« Reply #5 - Posted 2006-10-03 11:49:06 »

How I can know If  my game runs at a fixed frame rate and if it is 30..40 etc fps?

The fact that you're asking the question rather suggests that your game's running at a variable frame rate.  For a fixed frame rate, you need to add some code to your game loop to make it wait for a little while between frames.  If you want to run at (for example) 50 frames per second, then each cycle through your game loop needs to last for 1000/50 = 20 milliseconds.  If you measure how long a cycle actually takes (using System.nanoTime() for instance, if you're using Java 1.5) and it turns out to only have taken 8 milliseconds, then you can pause for 20-8 = 12 milliseconds to make the loop last for correct length of time.

(This can all get quite fiddly, but I'd still say that it's easier than updating the game based on elapsed time between frames.)

I believe that this is discussed at some length in the first few chapters of Killer Game Programming in Java (which you can read online here:  Also if you search these forums I'm sure you'll be able to find plenty of old threads on the subject.


Offline ravenger

Senior Newbie

« Reply #6 - Posted 2006-10-03 16:14:09 »

i rather not make movement time dependable, but just set a fixed number of frames i wanna do each second. It allows me to decide on what to do each frame, and if im short on time i skip a frame render (you could maximize the number of skips before a render too to keep it under control, and if its skipping frames often you could allways lower the number of frames you wanna have per second). This way my movement is more predictable and saves me trouble when doing collisions for example, because im gonna know in front if im gonna hit something rather than finding it out after ive moved, at least thats my experience.
Offline Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

« Reply #7 - Posted 2006-10-03 21:46:59 »

It's certainly faster to set fixed frames per second, which is what I personally do most of the time, but having non-fixed fps means that you can more easily judge how much processor your game eats, what the minimum system requirements, should be, etc. Essentially it gives you much more information on your efficiency than fixed fps does, but then you have to worry about moving things only as much as a fraction of the timestep.

See my work:
OTC Software
Offline zingbat

Senior Devvie

Medals: 1

Java games rock!

« Reply #8 - Posted 2006-10-06 10:28:23 »

Using a fixed amount for elapsed time can create gameplay problems, the game will run too slow or too fast depending on the computer it runs on. The basic stuff to do is to use nanoTime method from the Java api to probe the time before and after you have rendered the frame and update the game logic, then compute you elapsed time as the remaining time for the next frame. If your game rendering time exceeds a frame you only update the game logic in the next frames (skip one render update) until you get back on track.

The problem with this is that either the method used to waste time is imprecise (you tell him to wait 10 milliseconds and it waits 15 instead) or the method used to probe time is imprecise. I know that nanoTime is very precise but the wait method is just aceptable for games that run around 60fps, which will cause some frames to be skiped after a certain period of time. To minimize this problem without using complicated solutions i usualy multiply the elapsedTime value by 2/3 and it will ensure, with a low 60 fps that the frame rate is at least what im expecting.
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #9 - Posted 2006-10-06 10:58:05 »

Fixed vs. variable rate logic and fixed vs. variable rendering is an old topic thats been done to death on these forums, a quick search would be a good idea. Basically theres advantages and disadvantages to both, depending on your game requirements one may be a better choice than the other.

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CommanderKeith
« Reply #10 - Posted 2006-10-07 02:22:58 »

See this thread, there's an argument about fixed vs variable time:

If you use variable time, you need to know a bit of basic physics.  But with a variable time update(float secondsElapsed) method you can just use fixed time anyway, so go with a variable time setup.

Also, you can change game speed really easily by multiplying the secondsElapsed by whatever to speed up & slow down  Cheesy


Pages: [1]
  ignore  |  Print  

DesertCoockie (20 views)
2018-05-13 18:23:11

nelsongames (68 views)
2018-04-24 18:15:36

nelsongames (65 views)
2018-04-24 18:14:32

ivj94 (748 views)
2018-03-24 14:47:39

ivj94 (79 views)
2018-03-24 14:46:31

ivj94 (555 views)
2018-03-24 14:43:53

Solater (94 views)
2018-03-17 05:04:08

nelsongames (168 views)
2018-03-05 17:56:34

Gornova (338 views)
2018-03-02 22:15:33

buddyBro (998 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!