Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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 3265 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie





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

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

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 Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #1 - Posted 2006-09-22 14: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 Member




Go Go Gadget Arms


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

so your doing:

1  
position.x += 0.1;


instead of:

1  
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...

DP

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 14: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.

Simon

Offline Graziano Mesina

Senior Newbie





« Reply #4 - Posted 2006-10-03 11: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 13: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: http://fivedots.coe.psu.ac.th/~ad/jg/).  Also if you search these forums I'm sure you'll be able to find plenty of old threads on the subject.

Simon

Offline ravenger

Senior Newbie





« Reply #6 - Posted 2006-10-03 18: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


Game Engineer


« Reply #7 - Posted 2006-10-03 23: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 Member




Java games rock!


« Reply #8 - Posted 2006-10-06 12: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: 56
Projects: 11


Monkey for a head


« Reply #9 - Posted 2006-10-06 12: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.

[ TriangularPixels.com - 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 04:22:58 »

See this thread, there's an argument about fixed vs variable time:   http://www.java-gaming.org/forums/index.php?topic=13469.0

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

Keith.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!