Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Time based movement / deltaTime / Event Timing ?  (Read 728 times)
0 Members and 1 Guest are viewing this topic.
Offline ddfh

Junior Member


Medals: 1



« Posted 2013-04-12 09:14:39 »

Hi,

I'm really confused. The app is faster or slower on some devices. I had some small problems with libgdx. BUT this is the biggest problem that I've had.

First I thought I understood deltaTime. But that is not so.

Why is that so hard. I made a small program, where is a 3d cube is moving from left to right.
And then the 3d cube is moving backwards.

I've tried so many things. NOTHING WORKS.
I also added the deltaTime (test += steps*deltaTime) to the movement. Nothing helps. On my smaller smartphone the movement is faster.

I don't know what to do. On my bigger smartphone is it slower.

I have read all of this. (some things I did not understand):
http://gafferongames.com/game-physics/fix-your-timestep/

http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=1845&p=10046&hilit=step+deltatime#p10046

http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=4185&p=20139&hilit=deltaTime#p20139

Has somebody not a great solution for this. How can I do it, that the render() renders at the same speed on all devices?

I read about accumulate or something like that. Is this the answer?

Info: I'm not using box2d.

Has anybody solved this problem. It would be great, when somebody can post a working example code.

My program looks a little bit like this:
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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
public class ObjectScreen implements Screen {
   Game game;


   public ObjectScreen (Game game) {
      this.game = game;
     
      cam = new PerspectiveCamera(45, VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
      ...
   }
...

@Override
public void render (float delta) {
   update(delta);
   draw(delta);
}

public void update (float deltaTime) {
   // ...
}

public void draw (float deltaTime) {      

   GL10 gl = Gdx.graphics.getGL10();

   gl.glClearColor(0.0f, 0.0f, 0.0f, 1);

   gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
   ...
   
   // 3d Object moving
  ...
}
}



Thank You!
Offline Grunnt

JGO Wizard


Medals: 66
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #1 - Posted 2013-04-12 11:25:49 »

I can't really say anything unless you provide us with more of your code, specifically whats inside the update() function.

Offline 20thCenturyBoy

Senior Member


Medals: 3


So much to learn, so little time.


« Reply #2 - Posted 2013-04-12 15:13:49 »

How are you calculating deltaTime?

"I have never done unit testing and I don’t find it a very useful concept" - Jonathan Blow
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ddfh

Junior Member


Medals: 1



« Reply #3 - Posted 2013-04-12 15:27:32 »

I'm not calculating deltaTime. I generated a standard LibGDX Project. I get it from LibDX.

When I output deltatime on my bigger smartphone I get this:
0.016456..., or 0.152546... etc.

On my smaller smartphone I get this:
0.011254..., or 0.102546... etc.

Online Phased
« Reply #4 - Posted 2013-04-12 15:37:50 »

Not all phones are equal, I don't think its really worth even trying to make every phone render at the same time.

Your "bigger smart phone" may have more background app's running then your "smaller smart phone", this would alter the speed as your phone would be running some background tasks, try closing everything but your game on both devices.

The hardware in the "smaller phone" could be better then in your "bigger phone" or your smaller phone just requires less processing time for its general things, (maybe less things need updating in the smaller phone).

Conclusion: If you try testing the delta time on different computers, you will also notice that every computer will give a different number, Delta is there to help fix the speed of the game for slower computers, the faster your computer is going, the less the delta value will be, meaning if you had a free loop, the delta value SHOULD make the movement of objects stay constant or pretty close between devices, despite the value of delta being greater or lower on one computer.

EDIT: Maybe your screen is a different resolution, your "bigger smart phone" probably has a higher resolution then your smaller phone.

Meaning the cube in the "smaller smart phone" has less distance to travel to the other side of the screen.
Offline ddfh

Junior Member


Medals: 1



« Reply #5 - Posted 2013-04-12 16:32:07 »

Here is an example.

In my app, there is a red box. It's moving from left to right. The app starts, when you touch the screen.
Now, I installed this on my big and small smartphone. I touch the both devices at the same time.
And now I see, that the red box on my smaller smartphone touches the right side of the screen first.

In my app, there is a ease function from Robert Penner and I added Aspect ratio.

So, first the images:
(This is my libgdx.png file)


(this is, where the red box starts)


(and this is, where the red box touches the right side)


And this is the code:
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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class MyGdxGame implements ApplicationListener {
   
    private static final int VIRTUAL_WIDTH = 800;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
   
    private Rectangle viewport;
   
   private OrthographicCamera camera;
   private SpriteBatch batch;
   private Texture texture;
   private Sprite sprite;
   
   private float x = 0f;
   private float y = 0f;  
   
   private boolean start = false;
   
    private float diff = 200f;

    private float minTime = 0;
    private float maxTime = 770f;
   
    private float iEaseOut = 0;
    private float zTime = 0;
       
   @Override
   public void create() {      

      camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
      batch = new SpriteBatch();
     
      texture = new Texture(Gdx.files.internal("data/libgdx.png"));
      texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
     
      TextureRegion region = new TextureRegion(texture, 2, 2, 32, 32);
     
      sprite = new Sprite(region);
   }

   @Override
   public void dispose() {
      batch.dispose();
      texture.dispose();
   }

   @Override
   public void render() {      
     
      float deltaTime = Gdx.graphics.getDeltaTime();
     
      if (Gdx.input.isTouched()) {
         start = true;
      }      
     
      if (start) {

         if (iEaseOut < diff) {
            zTime = expoEaseOut(iEaseOut, minTime, maxTime, diff);

            float deltaAdd = zTime*deltaTime;            
            zTime += deltaAdd;              
           
            x = zTime;        

            iEaseOut++;      
         } else {
            start = false;
         }
         
      }
     
      System.out.println(Gdx.graphics.getDeltaTime());
     
     
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
     
      camera.update();
     
        Gdx.gl.glViewport((int) viewport.x, (int) viewport.y, (int) viewport.width, (int) viewport.height);  
     
      batch.setProjectionMatrix(camera.combined);
      batch.begin();
      sprite.setPosition(x-VIRTUAL_WIDTH/2, y);
      sprite.draw(batch);
      batch.end();
   }
     
   public static float expoEaseOut(float t,float b , float c, float d) {
      return (t==d) ? b+c : c * (-(float)Math.pow(2, -10 * t/d) + 1) + b;  
   }

   @Override
   public void resize(int width, int height) {
       // calculate new viewport
       float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
       
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        viewport = new Rectangle(crop.x, crop.y, w, h);
   }

   @Override
   public void pause() {
   }

   @Override
   public void resume() {
   }
}
Offline actual

JGO Coder


Medals: 23



« Reply #6 - Posted 2013-04-12 17:01:16 »

Have you tried something simple like make small program that when you touch the screen it starts counting the number of seconds?
This way you could better isolate if the issue is with your time step or with the render/easing function.
Offline ddfh

Junior Member


Medals: 1



« Reply #7 - Posted 2013-04-13 13:16:55 »

I solved it now. Thanks to everybody.

This two lines solved my problem:
1  
2  
      float addDelta = 100*Gdx.graphics.getDeltaTime();
      speed = addDelta;


I tried that before. But without success.

Now it's working. It's unbelievable. Smiley
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 (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

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

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

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

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

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

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

danieldean (42 views)
2014-07-17 23:41:23
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!