Hi, I am trying to impliment the nanoTimer in java5, instead of using the GAGE timer. But i know this is pretty noob my issues, but for some reason, i know this is wrong, but i need help on trying to make the nano time work in the game loop. when i try to run this it says press any key, i press it, and the game is already over because its too fast, i tried another way, and the game just shows grey with a null pointer exception. so there is a thread problem, and is it bad to use a thread for game loops, because i was taught that originally, to use it.

// keep looping round til the game endswhile (gameRunning) {// work out how long its been since the last update, this// will be used to calculate how far the entities should// move this looplongdelta = System.nanoTime() - lastLoopTime;lastLoopTime = System.nanoTime();

// remove any entity that has been marked for clear upentities.removeAll(removeList);removeList.clear();

// if a game event has indicated that game logic should// be resolved, cycle round every entity requesting that// their personal logic should be considered.if (logicRequiredThisLoop) {for (inti=0;i<entities.size();i++) {Entityentity = (Entity) entities.get(i);entity.doLogic(); }

logicRequiredThisLoop = false; }

// if we're waiting for an "any key" press then draw the // current message if (waitingForKeyPress) {g.setColor(Color.white);g.drawString(message,(800-g.getFontMetrics().stringWidth(message))/2,250);g.drawString("Press any key",(800-g.getFontMetrics().stringWidth("Press any key"))/2,300); }

// finally, we've completed drawing so clear up the graphics// and flip the buffer overg.dispose();strategy.show();

// resolve the movement of the ship. First assume the ship // isn't moving. If either cursor key is pressed then// update the movement appropraitelyship.setHorizontalMovement(0);

// we want each frame to take 10 milliseconds, to do this// we've recorded when we started the frame. We add 10 milliseconds// to this and then factor in the current time to give // us our final value to wait fortry { Thread.sleep(lastLoopTime+10-System.nanoTime()); } catch (Exceptione) {} } }

Hi, I am trying to impliment the nanoTimer in java5, instead of using the GAGE timer. But i know this is pretty noob my issues, but for some reason, i know this is wrong, but i need help on trying to make the nano time work in the game loop.

// we want each frame to take 10 milliseconds, to do this// we've recorded when we started the frame. We add 10 milliseconds// to this and then factor in the current time to give // us our final value to wait fortry { Thread.sleep(lastLoopTime+10-System.nanoTime()); } catch (Exceptione) {} } }

I don't know about the rest but this is clearly wrong. In comments it says that you want to sleep 10ms, but you are using nano timer and you add 10 ns in sleep() so sleep time here probably goes negative. Convert 10ms to ns, it's * 1 000 000. Scale goes mili - micro - nano. So * 1000 gives micro and another * 1000 gives mili.

huh? could you explain a little bit or give a link to an article? I've heard about time calculating wrong on 2 core cpus since one core returns the time while other one is working with that code and time may become negative, is this it?

i did, but there is like a good chance there is a nullpointer problem(an error i usually get if theres a thread problem) even after the tweak, and when it works its still too fast, or the aliens move out of proportion.

I don't have time now for checking your code, but first you could eliminate everything else except sleep timing, in Thread.sleep() put some constant and see if your game runs good then, like put Thread.sleep(20) for 50fps. If it works fine then go through all sleep code and understand it, if you understand it you wouldn't have this problem.

// keep looping round til the game ends while (gameRunning) { // work out how long its been since the last update, this // will be used to calculate how far the entities should // move this loop long delta = System.nanoTime() - lastLoopTime;

The delta value in the original version is in milliseconds - so if a second had passed between game loops the value would be 1000. This is then used to work out how far to move the aliens/ship/fpscounter. However, since you've changed it to nanoTime() you'll get 1000000000 in a second I think. So you need to scale this delta value down to milliseconds or adapt every usage of the delta value. I'd go for the first one - divide delta by 1000000 (1 x 10^6) to fix the value.

Only had a couple of minutes to look but it looks like the most likely issue.

Well ok, they seem to move at a fair speed, and they animate. But the problem is with the fps counter I think, it starts with saying im getting like 500fps, then it slowly raises to like 1.5k fps, lol. So somethings wrong there. And there is a chance that after i executed the program once theres a null pointer problem with the thread, but every other time after compilization it works again.

this is ok... you increase fps variable each time you render a loop and after a second has passed you have the resaults and reset everything. Show us the part where you increase fps. btw. I don't personally like this way of counting fps since you have resaults every second, I like the one that can calculate fps after each pass through game loop.

why +100000 (0.1ms)? I see in your movement that you are dividing with 1 second, why?I think this should be +10000000 in sleep() and / 10000000 in x and y.

It is clear that you don't understand this, so I'll try to explain it. First don't just copy the code from tutorials, try to learn from it. Every line of code you write you need to know why are you writing that line. For example, the sleep method. Comment pretty much says it all. You want your game to run at 100 updates a second, so you want to sleep for 10ms. You then remember that your game loop takes some time, like if it is 4ms then you should sleep 6ms. So you try to calculate this. At start of your game loop you record time with nanoTime(). Lets say it is 1000000000. At the end of your game loop you record time again to see how much your game loop was running. It returns 1002000000. So your game loop ran for 2ms and you know you need to sleep for 8ms. That formula you have in sleep could be written like this also, maybe you'll understand this one better:

And there is a chance that after i executed the program once theres a null pointer problem with the thread, but every other time after compilization it works again.

a chance for you to get null pointer is probably one thread using some reference that needs to be initialized by another thread and sometimes first thread goes ahead of second one and tries to use it before second one initialized it. Initialize references before you start other threads that might use them.

Well, ok its gotten a little better. My program seems to display the real fps. It says like 65fps, but it sounds right because usually my programs are capped at 65fps in Eclipse IDE for some reason, but if i run it in jBuilder it goes to 95-100 fps like it should.

But the problem is with my entity movements, in order for the movement to be normal, it have to divide it by one second for some reason or its too fast. And my question about System.nanoTime(), is it that all it does is give you the CURRENT time from the system clock in nanoSeconds of when it was used. So it all makes sense now in that way. But my entity movements are whack. And also im not sure if my placment of afterTime and startTime is entirely correct.

// keep looping round til the game endswhile (gameRunning) {// work out how long its been since the last update, this// will be used to calculate how far the entities should// move this looplongdelta = System.nanoTime() - startTime;startTime = System.nanoTime();

// remove any entity that has been marked for clear upentities.removeAll(removeList);removeList.clear();

// if a game event has indicated that game logic should// be resolved, cycle round every entity requesting that// their personal logic should be considered.if (logicRequiredThisLoop) {for (inti=0;i<entities.size();i++) {Entityentity = (Entity) entities.get(i);entity.doLogic(); }

logicRequiredThisLoop = false; }

// if we're waiting for an "any key" press then draw the // current message if (waitingForKeyPress) {g.setColor(Color.white);g.drawString(message,(800-g.getFontMetrics().stringWidth(message))/2,250);g.drawString("Press any key",(800-g.getFontMetrics().stringWidth("Press any key"))/2,300); }

// finally, we've completed drawing so clear up the graphics// and flip the buffer overg.dispose();strategy.show();

// resolve the movement of the ship. First assume the ship // isn't moving. If either cursor key is pressed then// update the movement appropraitelyship.setHorizontalMovement(0);

// we want each frame to take 10 milliseconds, to do this// we've recorded when we started the frame. We add 10 milliseconds// to this and then factor in the current time to give // us our final value to wait fortry { Thread.sleep(10 - loopTime); } catch (Exceptione) {} } }

EDIT: Why is it that with Eclipse IDE, I sometimes get the proper fps. Like i just tryed it right now and it runs at 93 fps. And the movement seems to not move faster than it did at 60 fps, and i tryed it again with even a lower sleep than 10ms, and they move at the same rate, so it seems that the movement alogorithim is working. I also tryed it again with a 60ms delay in sleep, and the game runs at 23 fps and the movement is the same still, and the slow down looks like how 20 fps would look in a game, where its "kinda laggy" so this is a good sign that it works in that sense.

Well, ok its gotten a little better. My program seems to display the real fps. It says like 65fps, but it sounds right because usually my programs are capped at 65fps in Eclipse IDE for some reason, but if i run it in jBuilder it goes to 95-100 fps like it should.

Never heard of eclipse doing that... or can think how it could happen.

But the problem is with my entity movements, in order for the movement to be normal, it have to divide it by one second for some reason or its too fast. And my question about System.nanoTime(), is it that all it does is give you the CURRENT time from the system clock in nanoSeconds of when it was used. So it all makes sense now in that way. But my entity movements are whack. And also im not sure if my placment of afterTime and startTime is entirely correct.

your afterTime and startTime are good.No, System.nanoTime() dosen't have anything to do with current system time, as in "date and time". System.nanoTime() returns nanosecond value of some timer which has a random start value. All you can do with nanoTime() is compare to another nanoTime() value to see how much nano seconds has passed between 2 calls, nothing more.

from main game loop I see that delta is nano time passed since 2 game updates, so that is loopTime + sleepTime, so that is ~10ms (= 10 000 000ns). Dx and dy are probably speeds that entety moves, in pixels per second. That is why you need to divide by one second.

Let's say 10th of second had passed and if you move 100pix/s (dx = 100) then entety should move 10pixels. 10th of a second is 100ms, which is 100 000 000ns. Formula says:x += (100 000 000 * 100) / 1000 000 000 = 10 000 000 000 / 1000 000 000 = 10So as you see it's all about understanding what are variables storing and what they are for. If you anything of this is unclear to you ask again and say what part.

I'll try to assume some of things that confused you:dx and dy are speed, why are they named dx and dy? I think it comes from physics as difference over x and difference over y.why am I dividing by 1s? It's becouse your speed in dx and dy are measured in pixels per second, so if you know how much time has passed since last update (delta) you can calculate how much entety should move to compasate for that time (when you were sleeping, becouse in theory when you are traveling with some speed you never stop moving and since your program sleeps then it should move entety where it should be by calculating where it would end up if it didn't stop moving... this may be unclear to you right away, but if you understand those formulas then you should understand this)

EDIT: so I guess your game loop, movement and fps counter are good now, that is all you had problem with I guess, good luck with learning

Ok, cool. The reason I asked about the division of the movements by one second, was becase before you told me to divide it by a lower number, unless you though i was trying to do something else. Because through testing, dividing by one second was the only was it worked, but i just wanted to make sure that my after time and start time were not wrong to see if my movement divisons were wrong. I do know why we are dividing by higher number, because of the nano second scale of course.

I also just synced the animation frames for the nanosecond time, because before they were animating too fast, so its fixed now there too. The reason those dx and dy variables were named that, was well, that was what it is called straight from the tutorial. So ya the movements are synched, the fps seems to tell the truth, and the threads work. Life is all good now

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