简体   繁体   中英

Matrix and vector multiplication in libdx

I am trying to rotate a vector with a rotation matrix using LibGdx vector and matrix classes. However when I tried to multiply the vector by the matrix it say that The method mul(float) in the type Vector3 is not applicable for the arguments (Matrix3). I know his supposed to work barbecue I check LibGdx documentation: Vector3 mul(Matrix3 matrix) Left-multiplies the vector by the given matrix. So, it should work but I get the error mention above. At first I thought it was matrix dimension error but that should not be case the matrix is a 3 by 3 and the vector should be a 3 by 1. I think that I have more of syntax problem as I have not found any examples doing vector and matrix multiplication. Any help pointing in the right direction or a better way to rotate the vector would be really appreciated. Anyhow, Here is my code:

private Vector3 unrotatedPositon;
private Vector3 rotatedPosition;
private Matrix3 rotationMatrix;
unrotatedPositon = new Vector3(asteroid1.getX(), asteroid1.getY() , 0);
rotationMatrix = new Matrix3();
rotationMatrix.setToRotation(45);
rotatedPosition = unrotatedPositon.mul(rotationMatrix);

There's a simpler way to rotate the Vector3 , try the method:

rotate(float degrees, float axisX, float axisY, float axisZ);

According to your code it would become:

unrotatedPositon.rotate(45f, 0f, 0f, 1f);

unrotatedPositon now it's rotated. Maybe you should pick other name.

There's no need for these variables:

private Vector3 rotatedPosition; 
private Matrix3 rotationMatrix;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM