First of all, you are developing only on the emulator, or you have an android device ? On the emulator the performance is really nasty so the smooth movement can only be achieved on a real device.
Anyway, look at the following code:
At the beginning of each frame we compute the time interval betwen frames,
dt.
Using java Syntax Highlighting
public int dt = 0;
public float currentZ = -1.0f;
public long timePreviousUpdate = 0;
public long timeNow;
public boolean bZoomIn = false, bZoomOut = false;
public void onDrawFrame(GL10 gl)
{
timeNow = System.currentTimeMillis();
if(timePreviousUpdate == 0)
timePreviousUpdate = timeNow;
dt = (int) (timeNow - timePreviousUpdate);
timePreviousUpdate = timeNow;
if (dt > 500)
dt = 1;
Parsed in 0.031 seconds, using
GeSHi 1.0.8.4
With dt computed, we use it in every movement calculations to obtain constant movement on all devices, on different hardware/phones. We limit the dt at half of a second (500) to prevent bing jumps in movement after special events (like loading a level, which can take several seconds and dt is really big).
Right now i don't know how you want the object to zoom in/out (by finger or automatically). I used 2 boolean flags for zoomin/out. These must be set true or false in your activity class, at
onTouchEvent. The movement is simple. I multiplied the dt with 0.0005 (very small value) because dt is between 1 and 1000 (1000 = 1 second). This can be written more ellegant this way: dt/1000.0f * 0.5f.
Using java Syntax Highlighting
if(bZoomIn)
currentZ += dt*0.0005f;
if(bZoomOut)
currentZ -= dt*0.0005f;
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, currentZ);
// draw object (...)
}
Parsed in 0.031 seconds, using
GeSHi 1.0.8.4
If the game runt at 10 FPS: dt = 100, the movement step will be: 100*0.0005f =
0.050If the game runt at 20 FPS: dt = 50, the movement step will be: 50*0.0005f =
0.025So when the fps doubles, the new step for movement is half the first one.
This way, in a second, at 10 fps and at 20 FPS we will move the same distance, 0.5 GL units:
1 sec = 10 frames at 10FPS with dt=0.050 => 0.050 * 10 =
0.5 GL units
1 sec = 20 frames at 20FPS with dt=0.025 => 0.025 * 20 =
0.5 GL units
This means constant time-based movement, FPS independent movement, which is critical for any game.