I'm trying to scale around the point using approach:
It looks like:
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = detector.getScaleFactor();
float focusX = detector.getFocusX();
float focusY = detector.getFocusY();
Matrix.translateM(modelMatrix, 0, -focusX, -focusY, 0);
Matrix.scaleM(modelMatrix, 0, scaleFactor, scaleFactor, 0);
Matrix.translateM(modelMatrix, 0, focusX, focusY, 0);
But when I create result MVP matrix like:
Matrix.multiplyMM(MVPmatrix, 0,
projectionMatrix, 0,
modelMatrix, 0);
I get incorrect scaling so that it scales but it doesn't scale around the point correctly..
Could you advise what could be the reason and how to scale around the point correctly?
As far as I can tell, the methods in the Android Matrix utility class multiply the newly specified transformations from the right. I don't see it specified in the documentation, but the source code (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/opengl/Matrix.java) clearly suggests that it works this way.
This means that when you combine a transformation matrix from a sequence of sub-transformations, the last method you invoke specifies the sub-transformation that is applied to your points first. In other words, you specify the matrices in the reverse of the order that they are applied to your points.
For the rotation around your point (focusX, focusY), you first want to apply the translation by (-focusX, -focusY, 0.0f) to your points, then the rotation, then the translation by (focusX, focusY, 0.0f). Since the call sequence is the reverse of this, it should be:
Matrix.translateM(modelMatrix, 0, focusX, focusY, 0.0f);
Matrix.scaleM(modelMatrix, 0, scaleFactor, scaleFactor, scaleFactor);
Matrix.translateM(modelMatrix, 0, -focusX, -focusY, 0.0f);
I also changed the last argument of scaleM() here. Since you had 0 for the scale factor in z-direction, you would be flattening the entire geometry into the x-y plane.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With