3

I'm trying to rotate moving object, but it rotates aroud the center of the coordinates system. How to make it rotate around itself while moving? The code is:

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); 
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
nifuki
  • 31
  • 1
  • 1
  • 4
  • possible duplicate of [How to rotate object around local axis in OpenGL?](http://stackoverflow.com/questions/1671210/how-to-rotate-object-around-local-axis-in-opengl) This question (and its answers) may use old GL fixed-function, but the math behind it is exactly equivalent to what you're doing. – Nicol Bolas May 11 '12 at 12:43
  • I actually can't figure out the solution to my problem from that question – nifuki May 11 '12 at 13:35

3 Answers3

2

Don`t use the view matrix to rotate objects, this matrix is used as the camera for all the scene, To transform an object you should 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); 
}

Don`t forget use the identity matrix to reset the transformations in every loop.

I think your code is worng. You shoud update the value of 'y' before to apply any transformation.

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

The updateMVP method, will combine the model, view and projection matrices:

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);
}

And at last the render method, will execute the Shaders to paint the object:

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 (...);
}

I hope this will help you.

Gloomcoder
  • 81
  • 1
  • 5
0

where do you make the object drawing?

I suppose it is after the code you have put up here, something like:

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();//<<<<<<<<<<<<<<<<<<<

Then, the second translate call is the issue. You should either move your draw call before the second translate. or the clean way to do it is:

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();
CuriousChettai
  • 1,862
  • 1
  • 12
  • 10
  • I set the identity matrix when creating the surface. Why the second translate is the issue? I just move to the origin, rotate and then translate to new coords. – nifuki May 11 '12 at 13:46
  • I had tested your code, and it has the issue as you said. I fixed it as in my asnwer above. Now, could you please tell me where you are making the object drawing? – CuriousChettai May 12 '12 at 04:27
0

I just used view matrix instead of model matrix and everything worked out. For details on model, view and projection matrices see.

Community
  • 1
  • 1
nifuki
  • 31
  • 1
  • 1
  • 4