We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Correct Zooming Implementation
Page Index Toggle Pages: 1
Correct Zooming Implementation (Read 1488 times)
Correct Zooming Implementation
May 31st, 2010, 12:39pm
 
Hi there,

I'm just began to do some zooming panning over my project and found that transformations are not my best...

I realized that I'm able to zoom the whole thing by using:
Code:
	public void draw() {
background(0);

// Processing phase
processObjects();
drawDragLine();
drawInfoBox();

pushMatrix();
// Draw phase

translate((this.width/2),(this.height/2));
scale(scale);

centralNode.setPosition(0, 0);
centralNode.drawObject();
popMatrix();
drawObjects();

//
}


The problem is that, as it's normal, when scale is 2.0 the world is twice big. But also the mouseX and mouseY!

So every computation to do hovers and selections are wrong...

I tried to scale every object instead the world. But this makes me recode everything taking into account the selected scale. (Forces, sizes, spaces, etc). That's a mess...


So what's the correct whay to implement zoom and panning?

Thank  you
Re: Correct Zooming Implementation
Reply #1 - May 31st, 2010, 1:04pm
 
I think scale is actually a good solution for this. Can't you just use...

mouseXnormal = mouseX/scale

...to correct the mouseX and Y?
Re: Correct Zooming Implementation
Reply #2 - Jun 4th, 2010, 4:32am
 
Mmmm...

Yessss! Actually I have to deal with pan also. So correct formule seems to be:
Code:
public float getMouseX()
{

return (mainPApplet.mouseX - (mainPApplet.width/2)) / mainPApplet.scale;  
}

public float getMouseY()
{
return (mainPApplet.mouseY - (mainPApplet.height/2)) / mainPApplet.scale;
}



But the question here is how to implement scale/pan correctly.

I find two ways.

  • First: Setup transforms at begining of draw phase.

    This implies that I have to undo transformations for scaling and panning each individual object before rendering them. And then apply again pan and scale. If this is not done strange behaviour is observed.
  • Second: Pass current options to each object and let them to decide how to render based on pan and scale.

    The problem here is that absolutly all must aware of current settings, lines, objects, everything. So it can lead to a mess.



Is there any other way to deal with pan and zoom? Any good tutorial?

Tnx



Re: Correct Zooming Implementation
Reply #3 - Jun 4th, 2010, 6:56am
 
I found that when using global pan and zoom. Objects only scale well when everything is in origin.

So something about transformations scape to my knowledge.

I tried to reset matrix before drawing objects so I can scale them in origin, but then I found objects not to pan...

So something is wrong with the order or transformations I do...
Page Index Toggle Pages: 1