Once the mouse clicked on the screen, I record the world coordinate X and Y
Once the mouse start moving, I calculate the difference of current world coordinate X and Y with previous world coordinate X and Y
glTranslated of the difference of world coordinate X and Y
The final result doesn’t fit my expectation, the world doesn’t move along with my mouse(my mouse is moving faster than the world move(the world is moving slower)), and the result gets better when I zoom into my world.
Orthographic or perspective projection? If it’s a perspective projection, the distance will depend upon the Z value chosen. Objects in that Z plane will move the desired distance, nearer objects will move more, farther objects will move less.
Ok, you’r changing the projection to cover a smaller area to get a zoom. So,
if you find a /z-distance/ that provide the ideal response then grab the P_width that your projection uses. When changing the projection again set
GLfloat scaler = new_width/P_width ;
you may have to flip the two.
Scale the drag.dx & .dy
… addition …
You could have used a matrix uniform scale somewhere, to present the world as zoomed, but we may expect you to already have tested how that scale inflicts on the drag() … directly or inversed.
@CarstenT sorry I don’t understand you…
Currently how I zoom my world is by using code below which copied from this forum:
double zoomInFactor = 1.2;
double zoomOutFactor = 1 / zoomInFactor;
double cameraZoom = 1;
double pan_x = 0.0;
double pan_y = 0.0;
double zoom_k = 1.0;
double scale = 1.2;
void pan(double dx, double dy)
pan_x += dx / zoom_k;
pan_y += dy / zoom_k;
void zoom(double factor)
zoom_k *= factor;
void zoom_at(double x, double y, double factor)
//# Move to camera position
glTranslatef(moveX, moveY, 0);
//# Scale camera
glScalef(cameraZoom, cameraZoom, 1);
//# Move back from camera position
glTranslatef(-moveX, -moveY, 0);
This code working fine to zoom towards where my mouse pointed and now I need to drag/pan my world when mouse left clicked and hold the world, any tips for me? Although I already slightly understand there’s screne coordinate, world coordinate and glTranslated should be enough for me to drag/pan the world but the result was not what I expected.
I can hear, that you have a low level of understanding matrixes. You should look online for examples on how and why. Try search “Nate Robins”.
You use a scale-matrix in glScalef(cameraZoom, cameraZoom, 1);
The glTranslatef(moveX, moveY, 0);
is another transformation where a matrix lies behind.
You talk about a pan . That term is usually used for … something else than simply moving the model left or right, up or down.
More to the point. You /are/ scaling the dx, dy
Have you tried in pan_x += dx / zoom_k;
to invert the scale, like pan_x += dx * zoom_k;
… but, no warrenty that it’ll work.
… you are using variables moveX, moveY that are not present elsewhere in the code you provide.
This suggests that the drag is influensed by the scaling/zooming and you relate rationally to this specific problem.
not to be pedantic, but I pointed out one suggestion:
and pointed out that your code may use prober variables moveX, moveY in the place where pan_x , pan_y would be expected. moveX, moveY pops up from nowhere.
The response of your post does not address these issues. Shall I ask if you tested changes to these suggestions, or shall I assume that you tested and did not get the response you need?
Inspite of my wranting on matrix-knowledge and misnamed variables, your code looks absolutely rational.