Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 -;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;
                            //adding up the additive ratio and scale
                    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;
share|improve this question
up vote 0 down vote accepted

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.

share|improve this answer
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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.