Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:54:38+00:00 2026-06-07T14:54:38+00:00

I am baffled as to why android’s matrix class’s multiplyMV method seems to be

  • 0

I am baffled as to why android’s matrix class’s multiplyMV method seems to be rotating my vector coordinate clockwise, when I thought it would be counter-clockwise.

In this code pos is the vector coordinate and set to be <0.0f, 5.0f ,0.0f>, and the matrix will rotate the coordinate vector by -45 degrees about the Z axis. I would expect the result vector coordinate in the <+,+> quadrant, namely <3.535534, 3.535534, 0.0>. But instead, it rotates the coordinate in the opposite direction, <-,+> quadrant, namely <-3.535534, 3.535534, 0.0>.

Matrix4 mtxRot = Matrix4.InitRotateEulerXYZ(0.0f, 0.0f, -45f);
pos.Set(0.0f, 5.0f ,0.0f);
mtxRot.TransformCoordVec(pos);

Here’s my in Matrix4.InitRotateEulerXYZ

public static Matrix4 InitRotateEulerXYZ(float x, float y, float z)
{
    Matrix4 rotMatrix = new Matrix4();

    /*  XYZ = | cz*cy,   sz*cx + cz*sy*sx,  sz*sx - cz*sy*cx |
              | -sz*cy,  cz*cx - sz*sy*sx,  cz*sx + sz*sy*cx |
              | sy,     -cy*sx,             cy*cx            |  */

    // Convert from degrees to radians
    x = MathHelper.DegreesToRadians(x);
    y = MathHelper.DegreesToRadians(y);
    z = MathHelper.DegreesToRadians(z);

    rotMatrix.GetArray()[0] =  MathHelper.Cos(z) * MathHelper.Cos(y);
    rotMatrix.GetArray()[1] = -MathHelper.Sin(z) * MathHelper.Cos(y);
    rotMatrix.GetArray()[2] =  MathHelper.Sin(y);

    rotMatrix.GetArray()[4] =  (MathHelper.Sin(z) * MathHelper.Cos(x)) + (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Sin(x));
    rotMatrix.GetArray()[5] =  (MathHelper.Cos(z) * MathHelper.Cos(x)) - (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Sin(x));
    rotMatrix.GetArray()[6] = -(MathHelper.Cos(y) * MathHelper.Sin(x));

    rotMatrix.GetArray()[8 ] = (MathHelper.Sin(z) * MathHelper.Sin(x)) - (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Cos(x));
    rotMatrix.GetArray()[9 ] = (MathHelper.Cos(z) * MathHelper.Sin(x)) + (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Cos(x));
    rotMatrix.GetArray()[10] =  MathHelper.Cos(y) * MathHelper.Cos(x);

    return rotMatrix;
}

And here is my Matrix4.TransformCoordVec method

public Vector3 TransformCoordVec(Vector3 vec3)
{
    Matrix4.inVec[0] = vec3.X;
    Matrix4.inVec[1] = vec3.Y;
    Matrix4.inVec[2] = vec3.Z;
    Matrix4.inVec[3] = 1.0f; // homogeneousCoord

    Matrix.multiplyMV(Matrix4.outVec, 0, this.matrix, 0, Matrix4.inVec, 0);     

    vec3.X = Matrix4.outVec[0]; vec3.Y = Matrix4.outVec[1]; vec3.Z = Matrix4.outVec[2];     
    return vec3;
}

Any help is greatly appreciated!

FIXED

InitRotateEulerXYZ and my Quaternion ToMatrix() methods needed to be transposed in order to make rotation counter-clockwise for positive angles. Here are the corrected methods.

Quaternion.ToMatrix

/**Converts a quanternion to its equivilant matrix form**/
public Matrix4 ToMatrix()
{       
    // First, lets check if we need to re-normalize our quaternion
    if(normalRegenerationCount <= 1000)
    {
        Normalize();
    }

    float x2 = x * x;
    float y2 = y * y;
    float z2 = z * z;
    float xy = x * y;
    float xz = x * z;
    float yz = y * z;
    float wx = w * x;
    float wy = w * y;
    float wz = w * z;

    Matrix4 result = new Matrix4();

    // This calculation would be a lot more complicated for non-unit length quaternions
    // Note: The constructor of Matrix4 expects the Matrix in column-major format like expected by
    //   OpenGL
    result.Set_11(1.0f - (2.0f * (y2 + z2)));
    result.Set_12(2.0f * (xy + wz)); 
    result.Set_13(2.0f * (xz - wy));        
    result.Set_14(0.0f);        

    result.Set_21(2.0f * (xy - wz));  
    result.Set_22(1.0f - (2.0f * (x2 + z2)));
    result.Set_23(2.0f * (yz + wx));
    result.Set_24(0.0f); 

    result.Set_31(2.0f * (xz + wy)); 
    result.Set_32(2.0f * (yz - wx)); 
    result.Set_33(1.0f - (2.0f * (x2 + y2))); 
    result.Set_34(0.0f);

    result.Set_41(0.0f);
    result.Set_42(0.0f);        
    result.Set_43(0.0f);        
    result.Set_44(1.0f);

    return result;
}

Matrix.InitRotateEulerXYZ

public static Matrix4 InitRotateEulerXYZ(float x, float y, float z)
{
    Matrix4 rotMatrix = new Matrix4();

    // Convert from degrees to radians
    x = MathHelper.DegreesToRadians(x);
    y = MathHelper.DegreesToRadians(y);
    z = MathHelper.DegreesToRadians(z);

    rotMatrix.matrix[0] =  MathHelper.Cos(z) * MathHelper.Cos(y);
    rotMatrix.matrix[1] = (MathHelper.Sin(z) * MathHelper.Cos(x)) + (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Sin(x)); 
    rotMatrix.matrix[2] = (MathHelper.Sin(z) * MathHelper.Sin(x)) - (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Cos(x));

    rotMatrix.matrix[4] =  -MathHelper.Cos(y) * MathHelper.Sin(z);
    rotMatrix.matrix[5] =  (MathHelper.Cos(z) * MathHelper.Cos(x)) - (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Sin(x));
    rotMatrix.matrix[6] =  (MathHelper.Cos(z) * MathHelper.Sin(x)) + (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Cos(x)); 

    rotMatrix.matrix[8 ] =  MathHelper.Sin(y); 
    rotMatrix.matrix[9 ] = -(MathHelper.Cos(y) * MathHelper.Sin(x));
    rotMatrix.matrix[10] =  MathHelper.Cos(y) * MathHelper.Cos(x);

    return rotMatrix;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:54:40+00:00Added an answer on June 7, 2026 at 2:54 pm

    Simplifying the matrices you have created, by setting X and Y to 0 as arguments in InitRotateEulerXYZ, and multiplying by (0, 5, 0, 0) you get:

    | cos(θ) sin(θ)    0     0 | | 0 |   | 5*sin(θ) |
    |-sin(θ) cos(θ)    0     0 | | 5 |   | 5*cos(θ) |
    |    0     0       1     0 | | 0 | = |     0    |
    |    0     0       0     0 | | 0 |   |     0    |

    Substituting θ=-π/4, the resulting vector is given by:

    | 5*sin(-π/4) |   | -5*sin(π/4) |
    | 5*cos(-π/4) |   |  5*cos(π/4) |
    |      0      | = |       0     |
    |      0      |   |       0     |

    Which is exactly the answer you have been seeing. So, I suggest it’s not a problem with the Android matrix multiplication routines. You should recheck the derivation of your matrix multiplications or simply choose one of those already derived at another site and adapt your code accordingly.

    Alternatively, you can use the built in setRotateEulerM method and have the system generate it for you. You just need to make certain the order of the matrix multiplications is consistent with your intent, as there are a variety of ways to generate these matrices.

    One other thing to consider is that using Euler Rotation matrices are generally a poor method for generalized rotations as they are subject to things like gimble lock. You may want to consider one of the Axis/Angle methods such as Rodrigues’ method, or by using quaternions. The Android library appears to have native implementations of axis/angle routines if you would prefer to use one of those instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm baffled. I'm trying to find suitable colors for a logo that would look
I am currently baffled to why my method is not running. I placed a
I'm baffled by the results I'm getting from FFT and would appreciate any help.
I find myself a little baffled here. I'm fairly new to android/java development so
Baffled. class Test { void Main() { F(() => ); // ok F(named: ()
I am baffled with this questions. I want to believe that through some Javascript
this has baffled me for a couple of hours now. Why do i get
This question has baffled myself and my cohorts. In the program I had written
I am baffled. I have a ready function, from jQuery, which sets an HTML5
It has always baffled me that if a folder has read and write permissions,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.