# glScalef - game math issue

I can't get my head wrapped around this issue. The issue is my latest code is making the camera zoom in and out really quickly.

My approach is built on -

http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847?tag=content;siu-container

The opengl scale will be getting the variable - scale.

``````gl.glScalef(scale, scale, 1);
``````

the distance is obtained by between two fingers, old distance (initial touch points), and new distance (dragging touch points). The zooming in and out works well. However, it would reset glScalef each time the user start using pinch zoom.

``````scale = newDistance / oldDistance;
``````

I tried calculating by additive ratio. The oldtscale handles the previous distance, if it is same, then it doesn't need to add up anything to scale. The zooming is really quick, I moved the fingers closer by mere 1 cm to 5 cm, zoom goes down or up fast. I think additive ratio is a bad solution. I think it might be incomplete solution. I'm trying to figure out what's wrong with it.

``````//additive ratio
tscale = (newDistance / oldDistance) - 1;
if(oldtscale == tscale) {
oldtscale = tscale;
tscale = 0;
}
else {
oldtscale = tscale;
}
tscale = scale + tscale;
//checking tscale for limiting the maximum/minimum scale
if(tscale >= 2) {
tscale = 2;
}
else if(tscale <= 1) {
tscale = 1;
}
//supply scale
scale = tscale;
``````
-

It's not clear if your problem is the lacking of smoothness or something else. Does the code work as it is? Also, you're saying that additive ratio is a bad solution, why?

You are doing an inner ratio to get a scale, with the possibility of getting either: 0/oldScale or newScale/0

The div by zero is an obvious error, but the scaling by zero is wrong too. In your case, the zoom won't be smooth since you are applying a div result, that could be a sudden huge zoom if newScale >> oldScale.

If the initial values are something like 15/10 (1.5x scale), later it could get 20/15 (now it's ~1.3x) with the same amount of movement. Unless you want to confuse the users, an additive zoom level using (newDistance-oldDistance)*constMultiplier with a fixed min and max scale will result in a better feeling.

-
The issue is my code made the 'camera' would zoom in and out quickly. The code isn't working what I expected. I'm doing the inner ratio to add up to scale. The initial values would be always start with 1. I subtract the 1 to get the remaining value to add up to scale. Edited my question. – Tashu May 26 '12 at 22:34
It's too quick for the reasons I explained in my answer: you are using new/old. It could result in a sudden 10x zoom. The last line in my answer should give you a smoother result, and also something that will remain stable for the same amount of movement, regardless the positioning on the screen. – Darkwings May 26 '12 at 22:52
I tried doing your solution, it works well. Also it gives me some control over the speed of zoom. Thanks again. – Tashu May 26 '12 at 23:54

Not very clear what your problem is, but you almost definitely don't want to scale the Z by zero. Anything multiplied by 0 is 0, so you're effectively making the depth of any object 0 . You probably want to scale Z by 1.0 instead, which would leave its depth unchanged.

-
This game is based on 2D. I didn't know about that. I didn't see anything wrong but I changed zero to 1 in scale method. I don't see any difference. I edited my question to make it clearer. – Tashu May 26 '12 at 21:12