I have a basic problem I want to translate all objects (the world) with touch event the problem is if the view matrix is rotated I could not calculate the model matrix translation to move in the correct direction, I want translate on x and z axes . I need the math logic. I have a camera class class and its draw void is:
public void draw() {
Matrix.setIdentityM(viewMatrix, 0);
setLookAtM(viewMatrix, 0, position.getX(), position.getY(), position.getZ(), lookAt.getX(), lookAt.getY(), lookAt.getZ(), 0f, 1f, 0f);
Matrix.setIdentityM(mCurrentRotation, 0);
Matrix.rotateM(mCurrentRotation, 0, mDeltaX, 0.0f, 1.0f, 0.0f);
Matrix.rotateM(mCurrentRotation, 0, mDeltaY, 1.0f, 0.0f, 0.0f);
mDeltaX = 0.0f;
mDeltaY = 0.0f;
float[] mTemporaryMatrix = new float[16];
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentRotation.clone(), 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
Matrix.multiplyMM(mTemporaryMatrix, 0, viewMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, viewMatrix, 0, 16);
}
and a mesh class and its draw void is:
public void draw(float mDeltaX, float mDeltaY) {
Matrix.setIdentityM(getMatrix().getFloatArray(), 0);
multiplyMM(getMatrix().getFloatArray(), 0, containerMatrix.getFloatArray(), 0, geometry.getMatrix().getFloatArray(), 0);
multiplyMM(modelViewMatrix, 0, this.getScene().getCamera().getViewMatrix(), 0, getMatrix().getFloatArray(), 0);
Matrix.setIdentityM(mCurrentTranslation, 0);
Matrix.translateM(mCurrentTranslation, 0, -mDeltaX, 0, -mDeltaX);
float[] mTemporaryMatrix = new float[16];
Matrix.multiplyMM(mTemporaryMatrix, 0, mCurrentTranslation.clone(), 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, mAccumulatedRotation, 0, 16);
Matrix.multiplyMM(mTemporaryMatrix, 0, modelViewMatrix, 0, mAccumulatedRotation, 0);
System.arraycopy(mTemporaryMatrix, 0, modelViewMatrix, 0, 16);
this.getScene().getShaderProgram().setUniforms(modelViewMatrix, this.getScene().getCamera().getProjectionMatrix(), material.getTextureid(), material.getMaterialColor(), material.getEmissiveColor(), material.getShinines());
geometry.getDataBuffer().bindData(this.getScene().getShaderProgram());
glDrawArrays(GL_TRIANGLES, 0, geometry.getDataBuffer().getVerticesCount());
}
and the onTouchEvent
is:
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (mainRenderer != null) {
if(event.getPointerCount()==1){
float deltaX = (x - mPreviousX) / mDensity / 2f;
float deltaY = (y - mPreviousY) / mDensity / 2f;
mainRenderer.getCamera().mDeltaX += deltaX;
mainRenderer.getCamera().mDeltaY += deltaY;
}
else if(event.getPointerCount()==2){
float deltaX = (x - mPreviousX) / mDensity / 2f;
float deltaY = (y - mPreviousY) / mDensity / 2f;
mainRenderer.getScene().mDeltaX += deltaX;
mainRenderer.getScene().mDeltaY += deltaY;
}
}
}
The camera could be rotated very well but I can not translate objects to directions related to the view matrix.
I did the following code
Vector3 camPos=new Vector3();
camPos.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),3);
Vector3 camUp=new Vector3();
camUp.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),1);
Vector3 camForward=new Vector3();
camForward.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),2);
Vector3 camRight=new Vector3();
camRight.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),0);
translateM(geometry.getMatrix().getFloatArray(), 0, mDeltaX*camRight.getX(), 0, mDeltaY*camUp.getY());
but the result is unexpected, I know it is incorrect but I tried to follow your instructions, I get the position, forward, up and right from the view matrix then apply the transaction to the model matrix.
Update this code is near from the right results but not perfect, I get the up, forward, right and position correctly from the view matrix
Vector3 col1=new Vector3();
col1.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),3);
Vector3 col2=new Vector3();
col2.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),1);
Vector3 col3=new Vector3();
col3.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),2);
Vector3 camUp=new Vector3(col1.getY(), col2.getY(), col3.getY());
Vector3 camForward=new Vector3(col1.getZ(), col2.getZ(), col3.getZ());
Vector3 camRight=new Vector3(col1.getX(), col2.getX(), col3.getX());
camRight.setFromMatrixColumn(this.getScene().getCamera().getViewMatrix(),0);
translateM(geometry.getMatrix().getFloatArray(), 0, mDeltaX*camRight.getX(),0,mDeltaY*camForward.getZ());