x


Zooming in and out on iPhone

What is the theory behind the pinch zoom in and out technique used on the iPhone?? How would one do this? Do you get the distance between 2 touch points and measure them? can someone point me in the right direction :)

Thanks :P

Here is the end code, it works for zooming in and out as well as moving around the iPhone, good usage in IOS games

//Author DOMINIC OBOJKOVITS
var check : boolean = false;
var OPinchDist : float;
var currentDist : float;
var counter : float ;
var touchP : Vector2;
var thing : Transform;
var speed : float = 0.1;
var Osize : float;

function Start(){

    iPhoneKeyboard.autorotateToPortrait = false; 
    iPhoneKeyboard.autorotateToPortraitUpsideDown = false; 
    iPhoneKeyboard.autorotateToLandscapeRight = false; 
    iPhoneKeyboard.autorotateToLandscapeLeft = false;

}

function Update () {

    var count = Input.touchCount;

    //Limits
    Osize = Camera.main.orthographicSize;
    if (Osize >= 12){
        Osize = 12;    
    }
    if (Osize <= 4){
        Osize = 4; 
    } 
    speed = 0.05 * (Osize/5);

    //Zooming Factor
    if (count >= 2){

       if (check == false){
         OPinchDist = Vector2.Distance(Input.GetTouch(0).position,Input.GetTouch(1).position);
         check = true;
       }
       currentDist = Vector2.Distance(Input.GetTouch(0).position,Input.GetTouch(1).position);
       var zoomFactor = ( (OPinchDist) / (currentDist) );

       Camera.main.orthographicSize = Osize * zoomFactor;

    }
    if (count <= 1){
       check = false;    
    }

    //Moves the camera around
     if (Input.touchCount == 1 && 
      Input.GetTouch(0).phase == TouchPhase.Moved) {
        var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
        thing.Translate (-touchDeltaPosition.x * speed,-touchDeltaPosition.y * speed, 0);
    }


}
more ▼

asked Nov 19 '11 at 11:04 PM

AtomicMarine gravatar image

AtomicMarine
504 42 49 70

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Basically, you calcualte and store the distance between touches at the start of the zoom, and compare it to the distance between them to determine the zoom level along the way.

Calculating the distance between two touches is easy...

var pinchDistance = Vector3.Distance(touchPos1, touchPos2);

Then you'll calculate the basic scaling factor something like this...

var zoomFactor = (currentPinchDistance/originalPinchDistance);

this can be applied in several different ways depending on your project. In a a third-person perspective view, you might use this factor to adjust the distance of the camera from the object. With an orthographic camera, you would apply it to the camera's orthographicSize. In a first-person game for a sniper scope, you'd probably want to apply it to the camera's field of view. In most cases you could probably apply by just multiply it with the original value of those varibles before the pinch zoom started, ex, for orthographic camera...

Camera.main.orthographicSie = originalOrthoSize * zoomFactor;

Hope this helps!

more ▼

answered Nov 20 '11 at 02:35 AM

WillTAtl gravatar image

WillTAtl
697 2 4 9

This worked perfectly! Thank you so much, once you showed the code to get the distances between to touches everything else fell into place quite well :D

Nov 20 '11 at 03:25 PM AtomicMarine

Dominic, would you mind posting that code? For others, it doesn't fall into place just that well :) Thanks!

Mar 04 '12 at 08:53 AM rsrhcp

@rsrhcp Sure thing, ill add it in the question.. :P am working on some amazing game algorithms if you need other stuff

Mar 04 '12 at 03:56 PM AtomicMarine
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3456
x1999
x1957
x183

asked: Nov 19 '11 at 11:04 PM

Seen: 2053 times

Last Updated: Mar 04 '12 at 03:59 PM