Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  dewitters Game Loop Attempt  (Read 2793 times)
0 Members and 1 Guest are viewing this topic.
Offline sleeping_emu

Senior Newbie





« Posted 2012-06-27 01:43:12 »

So I decided to try my hand at making a simple air hockey game that implements the last game loop described in the dewitters article here: http://dewitters.koonsolo.com/gameloop.html (The loop titled Constant Game Speed independent of Variable FPS).  However my results were disappointing because my game runs at different speeds on different computers and sometimes on the same computer, despite my update method seemingly running 25 times per second consistently (maybe my method for checking this is wrong).  Anyway, here is the code in question: https://gist.github.com/2997312  Please let me know if you have questions and thanks in advance for your help.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-06-27 03:17:28 »

Are you 100% sure there isn't any logic performed in the render-code?

You're game-loop looks fine, so the problem lies in the code you're not showing us Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #2 - Posted 2012-06-27 03:43:27 »

I call the checkCollisions() method on line 30 which is outside of the update method and update loop.  This method checks for collisions between either of the two paddles and the puck. I did this because, in the update method, the puck would just move through the paddle and the collision wouldn't be detected.  Would checkCollisions() cause the speed variation?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-06-27 04:03:07 »

Well, I don't know, I don't have any code...

What you do want, however, is keeping all your logic in your update method. Moving any logic outside it, effectively coupling it to the framerate, will make your game non-deterministic, and might result in the problems you're having, but again, I don't know.


If the puck has varying speed when not colliding with anything, then you probably have (even more) logic in your render method.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #4 - Posted 2012-06-27 04:28:38 »

I'm sorry here is that method: https://gist.github.com/3001409  And here is my render method also: https://gist.github.com/3001434
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-06-27 04:35:00 »

Well, if your physics are not accurate enough at 25Hz, why don't you increase the update-rate to 50 or maybe even 100?

If you really want low tick-rates and high velocity collisions, you need more advanced physics-calculations, like working with swept circles, or calculating (either projecting or backtracking) where exactly the collision occurs, bounce right there, and advance movement from there.

Simplest solution really would be to ramp up the update-rate drastically.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline tberthel
« Reply #6 - Posted 2012-06-27 05:37:11 »

You are creating string objects every frame.  Don't do that.

Offline sleeping_emu

Senior Newbie





« Reply #7 - Posted 2012-06-28 18:06:57 »

So i fixed the string object issue, moved all of the logic to the update method and changed the updates per second to 100.  That solved the collision issues I was having, but I still experience game speed slowdowns and speed-ups.  Could it be my interpolate methods?  I'm not sure I handled them right.  Here is the code for the puck's interpolation: https://gist.github.com/3012973  I handled the other rendered objects in a similar manner.  Thanks to Riven and tberthel for your help so far.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-06-28 19:22:18 »

That is nothing like interpolation code.

When you do:
1  
2  
3  
puck.interpolate(0.1f);
puck.interpolate(0.1f);
puck.interpolate(0.1f);
you're currently advancing the puck's position. It should stay the same.

And once again, you're doing logic-updates during rendering, by altering the puck.xPos and puck.yPos during interpolation.


You might want to lookup what interpolation actually is. You have 2 values (A and B), and a value that determines what the ratio between both values is.

A ratio of 0.0 means the result is equal to A
A ratio of 1.0 means the result is equal to B
A ratio of 0.5 means the result is equal to (A/2)+(B/2)

A ratio of n means the result is equal to (A*(1-n)) + (B*n), or more elegant: A + n*(B-A)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #9 - Posted 2012-06-28 20:02:04 »

I was under the impression that the interpolation value (in the case of a game loop) was the ratio between the set time between each update (in the case of 25 updates per second each update should take 40ms) and the actual amount of time that has passed.  In other words if each update should take 40ms and it has been 20ms since the last update, then your interpolation value would be 0.5.  Is that incorrect?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-06-28 20:31:53 »

I was under the impression that the interpolation value (in the case of a game loop) was the ratio between the set time between each update (in the case of 25 updates per second each update should take 40ms) and the actual amount of time that has passed.  In other words if each update should take 40ms and it has been 20ms since the last update, then your interpolation value would be 0.5.  Is that incorrect?
that is correct, but if you call pick.interpolate(0.5f) three times, it should set all fields to the same values each time.

Just imagine this pseudocode:
1  
2  
3  
4  
update();
interpolatedRender(20ms since last update);
interpolatedRender(20ms since last update);
interpolatedRender(20ms since last update);
You can see how it should render exactly the same frame multiple times.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #11 - Posted 2012-06-28 21:01:10 »

Well my render method is only called once.  I call three different interpolate methods for the three objects that move on screen.  Each of these methods is similar to the one i posted earlier and calculate the new position of the object based on the interpolation value.  Then the render method is called and everything is rendered at the interpolated location.  Is this incorrect?  I can see how the interpolate methods could cause slowdown...can they be placed in the update loop?  I don't see how that would work.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2012-06-28 21:05:16 »

As long as your render code does game logic, it will always be wrong.

I gave you the example of calling interpolate 3 times, on the same object. Whether or not you do that is irrelevant, and that there are 3 moving objects in your case is pure coincidence. I'm just describing how your interpolate code should behave, when called multiple times with the same argument.

Each of these methods is similar to the one i posted earlier and calculate the new position of the object based on the interpolation value.
Render logic must never modify fields like puck.xPos and puck.yPos, as it causes the framerate to influence the logic of the game, which is exactly what you were trying to get rid of.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #13 - Posted 2012-06-28 23:40:58 »

So how does one keep calculating the interpolation value consistently?  It can't be run with the update method because the interpolation needs to be calculated between updates and right before each render.  I'm sorry if I'm missing something here.  I'm trying my best to wrap my head around this.  My interpolation is calculated and applied outside of both my render method and update loop.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2012-06-28 23:51:55 »

Quote
So how does one keep calculating the interpolation value consistently?
You can make your rendering dode lag 1 update-state behind.
Then interpolate between state A and state B of your logic.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
class Vec2
{
   float x, y;
}

class Puck
{
   Vec2 position, speed; // used for logic
  Vec2 prevPosition, renderPosition; // used for interpolation

   public void update() {
      // save previous state
     prevPosition.x = position.x;
      prevPosition.y = position.y;

      // calculate new state
     position.x += speed.x;
      position.y += speed.y;
   }

   public void interpolateForRendering(float ratio) {
      renderPosition.x = prevPosition.x + ratio * (position.x - prevPosition.x);
      renderPosition.y = prevPosition.y + ratio * (position.y - prevPosition.y);
   }
}



My interpolation is calculated and applied outside of both my render method and update loop.
Whether you run your 'interpolation code' inside the update or render method doesn't matter. You're calling it every time you are about to call your render method, so that means that that code is executed at render-time.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #15 - Posted 2012-06-30 02:04:33 »

Are you saying it should be executed at render time?
Online Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2012-06-30 02:18:15 »

Are you saying it should be executed at render time?
The method names are not indicative?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sleeping_emu

Senior Newbie





« Reply #17 - Posted 2012-07-02 00:00:54 »

No they are I was just trying to be thorough.  Anyway, I implemented your suggestions and fixed the game speed issue as well as the collision detection.  Thank you very much for your help Riven.
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.

Dwinin (22 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (55 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (30 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!