Rotating a vector using Matrix.rotateM
I have made a simple class called Vector3. It's a 3 dimensional vector with some basic math implementions. Now i want to 开发者_如何学Cbe able to rotate this single vector, but i get an exception.
I have this:
private static final float[] matrix = new float[16];
private static final float[] inVec = new float[4];
private static final float[] outVec = new float[4];
public Vector3 rotate(float angle, float axisX, float axisY, float axisZ)
{
inVec[0] = x;
inVec[1] = y;
inVec[2] = z;
inVec[3] = 1;
Matrix.setIdentityM(matrix, 0);
Matrix.rotateM(matrix, 0, angle, axisX, axisY, axisZ);
Matrix.multiplyMM(outVec, 0, matrix, 0, inVec, 0);
x = outVec[0];
y = outVec[1];
z = outVec[2];
return this;
}
And i call i by making this:
Vector3 v = new Vector3(1f, 1f, 1f);
v.rotate(90f, 0f, 1f, 0f);
What i get is an IllegalArgumentException at:
Matrix.multiplyMM(outVec, 0, matrix, 0, inVec, 0);
It says that
length - offset < n
Does anyone have a clue about what i am doing wrong?
I didn't wrote this Vector3 function from the beginning, it's borrowed from the book "beggining android games"
You're using multiplyMM method that mutiplies 2 matrices and return a matrix instead of using multiplyMV (MV stands for matrix-vector) that multiplies your rotation matrix with your vector, returning the rotated vector.
精彩评论