使用旋转一个Matrix.rotateM矢量 [英] Rotating a vector using Matrix.rotateM
问题描述
我已经做了Vector3类型叫做简单类。
这是一些基本的数学implementions一个3维向量。
现在我希望能够旋转这个单一的载体,但我得到一个异常。
I have made a simple class called Vector3. It's a 3 dimensional vector with some basic math implementions. Now i want to be able to rotate this single vector, but i get an exception.
我有这样的:
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;
}
和我通过此事致电我:
Vector3 v = new Vector3(1f, 1f, 1f);
v.rotate(90f, 0f, 1f, 0f);
我得到的是一个IllegalArgumentException为:
What i get is an IllegalArgumentException at:
Matrix.multiplyMM(outVec, 0, matrix, 0, inVec, 0);
它说,
长度 - 偏移< ñ
length - offset < n
没有任何人有一个关于什么线索,我做错了?
Does anyone have a clue about what i am doing wrong?
的我没有写从一开始这个功能的Vector3,它是从书借beggining Android游戏的
推荐答案
您正在使用multiplyMM方法mutiplies 2矩阵,并返回,而不是使用multiplyMV(MV代表矩阵向量),与您乘以旋转矩阵,矩阵的矢量,返回旋转矢量。
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.
这篇关于使用旋转一个Matrix.rotateM矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!