Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


[java] Timing, GC, Threads...

This topic is 5825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I've been developing a little java-game for a while now, and it's slowly starting to get ready, but there are some problems that i haven't been able to solve by myself. So maybe someone here could help me out.. The game is implemented as an applet (java 1.1) and theres somekind of half-ready version of the game at http://www.ratol.fi/~tanderss/test/index.html. You're supposed to find the landing platform as fast as possible and land on it. There are bonus items (+time, -time, shields, nitros, etc) lying around the map which could help/make things worse. There are 2 crappy test maps, the 3rd wone won't work yet. Turn the ship with the left/right arrow keys, press up-key to accelerate and if you get nitros, press SPACE to use them. I've mostly tested it on MSIE and appletviewer on Win2K, so dunno how it works on other platforms. Tell me if it bugs like hell Now, i've been trying to make it as frame-rate independant as i could. Which is i alter the ship speed and other factors based on the current FPS. What i've been wondering, is that on my IE, when i for example pick up some items in the game (a new thread for effect timing is created each time), the FPS goes down so that you can easily see it until the effects wear off and the thread gets terminated.. I've set the threads to MIN_PRIORITY but it still does the thing. Guess there's nothing i can do about it? This problem is only on IE, not appletviewer. Another thing that i was wondering, what that if the Garbage Collection gets done while the game is running, it can slow things down for a few seconds too. Anything i can do about this? Prevent it from running or something? Again, on appletviewer no 1-2 sec slowdowns seem to come up? Then: I've got random items around the map. I'm just using Math.random() each time a random item is picked up, and for each item in the map, it seems to return the same value every round (when you select 'play again' from the menu after crashing/landing). If you load the map again, it seems to give new random values which again are the same until you reload the same map. This is pretty weird, as it makes me thing the random generates the seed based on the ship x,y coords which i don't think should be the case? And the last problem at the moment: The gravity and inertia values are not effected by the FPS. So as the ship calculations are performed every frame, the gravity and inertia seem to be smaller on slow computers, and higher on fast ones. Currently i just add the gravity to the ship's power-vector y component, and i was wondering that how could i calculate the gravity per frame? With the speed it's easy, just doing SpeedPerFrame = SpeedPerSecond/FPS, but for the gravity and inertia this results in weird results. Probably because those forces are kinda acceleration forces (rising ^2?). It's a little weird as on Appletviewer the game runs quite smooth (getting a constant ~90 fps), but on IE i get about 70FPS and it seems to jump up and down pretty much. Could the MS JVM do something so different? AFAIK it SHOULD run faster on IE than appletviewer. Sorry if I've forgot to mention something or haven't been precise enough.. Please note if i forgot to mention something essential. Thanks [edited by - fovster on December 5, 2002 3:06:46 PM]

Share this post

Link to post
Share on other sites
A few things.

1. You shouldn''t be creating and destroying threads in the middle of your game. Thread creation/destruction are expensive even in the non-VM world, so if you really need a separate thread (and in this case, I would think about whether you really need the new thread or not - it sounds to me like you don''t) then I would create it at the start of the level and leave it running.

2. The generally accepted way of stopping garbage collection in the middle of your run, is to not allocate any new memory. Use object pools, pre-allocate as much as you can, and just avoid new as much as possible.

3. Gravity is a funny thing. If you want to use the "proper" Newtonian equation, it''s something like:

F = ---

So, G is the gravitation contsant, and is generally a very small number (Work out the best constant for your game depending on your units and the relative scale of things). m is the mass of object 1 (usually the smaller object), M is the mass of the other object (usually the planet or whatever and r is the distance between the two objects centre of mass.

To get from F to a (acceleration), you use Newton''s second (I think?) law F=ma, or a = /m. When you substitute this formula into the one above, you get:

a = --

So you can see that acceleration only depends on the mass of the other object. Anyway, you go from acceleration to velocity by using v += a/FPS each frame.

And there you go! Realistic gravitation!

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

Share this post

Link to post
Share on other sites
I must say, I like it Only one problem that I could see. Nullpointer when selecting map03. Smooth gameplay with Sun´s 1.4.1 JVM, win9x, 1GhZ.

Share this post

Link to post
Share on other sites
Thanks for the feedback. Nice that you liked it Aenimal

Yeah, think you''re right at the thread thing. At the start of every map i anyway start a timing thread, which could probably be used for timing the item effects and short text displays. Just got to make up a smart way to follow stuff. Like when you eat a mushroom, and take another one a little later, it should restart the effect

I''ve already tried to get rid of instantiating new objects while running the game, but somehow i seemed to forget about the threads. Or thought i really needed more of them. Think it will do much already if i get rid of the ones not really needed.

Now i''m still wondering about the random thing. The case is like:

new Map():
Math.random() when a random item picked returns 0.75 for example.
Math.random() on another random item returns: 0.2315.

Then, if you take the 1st random item again, the Math.random() returns the same value. Same thing with the 2nd one. But if you create a new instance of the Map, you get different values for that instance again. Just to make it clear.. Any ideas?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!