We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have created a Processing sketch which supports dragging the whole display as well as zooming it in. Here are the methods I use to drag and zoom:
public void mousePressed(){
dragBeginX = (mouseX - offsetX);
dragBeginY = (mouseY - offsetY);
}
public void mouseDragged(){
if (mousePressed) {
offsetX = (mouseX - dragBeginX);
offsetY = (mouseY - dragBeginY);
}
public void zoomTo(float sc){
translate(width/2, height/2);
scale(sc);
translate(-width/2, -height/2);
//actual zooming handled in separate thread for smoothness
};
and in before doing the graphic stuff:
translate(offsetX, offsetY);
zoomTo(currentZoom);
There is no actual bug in that code, but it doesn't quite work the way I want - when the application is zoomed in, the dragging speed remains the same which means that the graphics move much faster than the cursor. Opposite applies when application is zoomed out. I have tried inserting the zoom value in the dragging math so that the drag speed would depend on the zoom, but I can't find where exactly it needs to be.
To sum it up, I need to make it so that when zoomed in, the object moving speed matches with the cursor dragging speed.
Any help is appreciated.
EDIT: If it was unclear, offsetX/Y is the current dragged position of the screen;
Answers
Probably (untested):
You need to scale your offset updates by the current zoom rate.
...actually, looking again, you probably also need to accumulate the offset -- you can't just reset it each time.
Check these:
https://forum.processing.org/two/discussion/20813/zooming-and-panning-headache#latest
https://forum.processing.org/two/discussion/20853/scale-translate-ab-comparission#latest
Kf
Hello again! In case someone is reading this, I managed to solve my problem in a very simple way - need to call translate BEFORE scale. (yes, it was like that in my posted code but it was my mistake) everything works like a charm now :)