Latest Uploads
Extraterre ... .0.1 (zip)

dantheman363

Monty Teas ... Screenie 1

steve_ancell

Santa Clau ... ed his bag

waroffice

manic_platdude.png

spinal

Tetris Clone

steve_ancell

Super blues bros.

spinal

Forum Home

Time in Java

UserMessage
Posted : Sunday, 15 July 2012, 14:38 | Permalink | Mark Here
jedimastersterling


I know most of you guys do something with game writing. I'm writing a physics simulation to demonstrate AI, but there isn't much difference there. Games use governing rules to determine outcomes and AIs to work as characters.

I have no problem writing the physics, but time is a problem. I want to make the simulator give video feed rather than just run the program and give the outcome. Does anyone know how to tell Java to wait say .05 seconds between frame changes so that I can get a smooth video?
Posted : Sunday, 15 July 2012, 15:11 | Permalink | Mark Here
JL235


WW Entries : 7
So you just want to sleep for half a second? You can use the sleep method in the Thread class to sleep the current thread.

-->

However sleep will only sleep for an approximate amount of time, not the exact amount given. Depending on your needs, this might be acceptable, and in an ideal world this is the best strategy to use. Half a second is a very long time for a computer, so it should be accurate enough.

If that doesn't work for you, an alternative to sleeping is to just spin, wasting CPU cycles, until enough time has passed. This gives a lot more accuracy, but shouldn't be used for long duration. It also has the downside that the CPU will be using full load during this time, and so will affect other threads on single or dual core machines.

Sometimes this is acceptable, for example Half Life 2 uses this strategy internally in a few places. There are also some commercial games written in Java which just spin when they have no work to do.

-->

You can also try mixing the two, so you sleep for long periods, and then use a spin lock for the last short duration. For example:

-->

Note that I've not tested the above code, that's just a general idea on a couple of approaches. I'd advise playing around with those, depending on your needs.

Even today, sleeping is still a hard problem.

-----
PlayMyCode.com - build and play in your browser, Blog, Twitter.
Posted : Sunday, 15 July 2012, 19:59 | Permalink | Mark Here
jedimastersterling


That helps enormously. For my current purposes that works just fine. However as my simulations and AI begin taking up more time they could take up as much time as the sleep. Assuming that calculations take up an appreciable amount of the 20th of a second I want between frames is there a way to make the program work to completion and then wait the remainder of the sleep time before displaying the next frame or beginning the next calculation iteration.

for example if I want the program to wait .05 seconds between frames and the AI takes .02 second to reach a conclusion it would be preferable to sleep only .03 seconds making the total time between frames .05 rather than sleeping a constant .05 seconds and ending up with .07 seconds between frames.
Latest Posts
Musical Tastes
Dabz Fri 22:31
Coder's Block
Jayenkai Thu 09:01
Consumer Lockout
9572AD Thu 06:09
Professor Oak, Honest!!
spinal Wed 23:24
Position in Mind
steve_ancell Wed 20:11
Sonic Adventure v0.x
Jayenkai Wed 07:08
FIRST!!!
steve_ancell Wed 03:55
Noel's Graduation
waroffice Wed 02:55
Monkey vs iOS tweaks
spinal Tue 22:51
Fucking Pound Sign Unicode Bullshit Bollocks
Dabz Tue 13:00
More

Latest Items
News : Newsletter #176
Jayenkai Sat 04:49
News : Newsletter #175
Dabz Tue 09:38
Blog : Snow: More Material Junk
Cower Sat 23:17
Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversion
rockford Fri 13:14
Techy : AppleTV
Jayenkai Thu 09:40
Blog : Graphviz
steve_ancell Sat 14:17
Pets : Top-Down Shadow Hack
Jayenkai Tue 05:52
Snippet : JNKrunch v1.0
Jayenkai Sat 07:20
News : Newsletter #173
waroffice Fri 04:47
Blog : Material Loading
Cower Fri 02:08
Pets : I Done Won A Thing
shroom_monk Sun 11:31
Pets : Repurposing A Lexer
Cower Mon 22:06
Bah : Feeling a Little Angry
spinal Mon 11:26
News : Newsletter #170
Dabz Sat 00:34
Showcase : sbfgen
Cower Sat 16:57
More

Who's Online
Jayenkai
Sat, at 03:18
CodersRule
Sat, at 03:04
spinal
Sat, at 03:00
shroom_monk
Sat, at 00:54
Mog
Sat, at 00:26
Evil Roy Ferguson
Fri, at 23:20
blanko1324
Fri, at 23:06
Cower
Fri, at 23:06
Dabz
Fri, at 22:41
lawandcontradiction
Fri, at 20:39
Link to this page
Site : Jayenkai 2006-Infinity |
MudChat's origins, BBCode's former life, Image Scaler.