使用 Matrix.rotateM 旋转向量
我创建了一个简单的类,名为 Vector3。 它是一个带有一些基本数学实现的 3 维向量。 现在我希望能够旋转这个单个向量,但我得到了一个例外。
我有这个:
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 at:
Matrix.multiplyMM(outVec, 0, matrix, 0, inVec, 0);
它说
长度-偏移量<
有谁知道我做错了什么吗?
这个Vector3函数并不是我一开始就写的,它是从《begining android games》一书中借来的
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.
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是multiplyMM方法,该方法将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.