OpenGL ES 2.0: a rotating object around itself on Android

I am trying to rotate a moving object, but it rotates around the center of the coordinate system. How to make him spin around himself while moving? The code:

Matrix.translateM(mMMatrix, 0, 0, -y, 0);
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);
y += speed;
Matrix.translateM(mMMatrix, 0, 0, y, 0); 
+3
source share
3 answers

Do not use the viewing matrix to rotate objects, this matrix is ​​used as a camera for the entire scene. To transform an object, you must use the model matrix. To rotate, if around its own center, you can use the following method:

public void transform(float[] mModelMatrix) {
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, 0, y, 0);
    Matrix.rotateM(mModelMatrix, 0, mAngle, 0.0f, 0.0f, 1.0f); 
}

Remember to use the identity matrix to reset the transforms in each cycle.

I think your code is worng. You must update the value of 'y' before applying any conversion.

public void onDrawFrame(GL10 gl) {
    ...
    y += speed;
    transform(mModelMatrix);
    updateMVP(mModelMatrix, mViewMatrix, mProjectionMatrix, mMVPMatrix);
    renderObject(mMVPMatrix);
    ...
}

updateMVP , :

private void updateMVP( 
        float[] mModelMatrix, 
        float[] mViewMatrix, 
        float[] mProjectionMatrix,
        float[] mMVPMatrix) {

    // combine the model with the view matrix
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

    // combine the model-view with the projection matrix
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix,   0);
}

, render, :

public void renderObject(float[] mMVPMatrix) {

    GLES20.glUseProgram(mProgram);

    ...

    // Pass the MVP data into the shader
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

    // Draw the shape
    GLES20.glDrawElements (...);
}

, .

+2

?

, , , - :

Matrix.translateM(mMMatrix, 0, 0, -y, 0);
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);
y += speed;
Matrix.translateM(mMMatrix, 0, 0, y, 0); 
drawHere();//<<<<<<<<<<<<<<<<<<<

. . :

Matrix.setIdentityM(mMMatrix, 0);//<<<<<<<<added
Matrix.translateM(mMMatrix, 0, 0, -y, 0);
Matrix.setRotateM(mMMatrix, 0, mAngle, 0, 0, 1.0f);
y += speed;
//Matrix.translateM(mMMatrix, 0, 0, y, 0); //<<<<<<<<<removed
drawHere();
0

I just used the presentation matrix instead of the model matrix, and it worked out. For more information on matrixes of models, representations and projections, see .

0
source

All Articles