I have a problem i can’t get my head around.
I use a sphere, when the user touch anywhere on the sphere i get a point in form of a 3D vector(x, y, z) all in opengl coordinates.
I also have a defined point on the sphere to begin with, also a 3D vector.
For example:
real V x: -0.2881811, y: 0.25460157, z: 0.9231087
Where “real V” is the point defined to begin with. (Sphere has a readius of 1.0)
After i’ve rotated the sphere using gl.glRotatef i want to pin out the new point with OpenGL coordinates of “real V”.
By doing this i use something like this:
real.rotate(-model.getRotationX(), 1f, 0f, 0);
real.rotate(-model.getRotationY(), 0f, 1f, 0);
Where “getRotationX and Y is the total rotation of the sphere (that i know is correct).
and real.rotate looks something like this:
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.multiplyMV(outVec, 0, matrix, 0, inVec, 0);
x = outVec[0];
y = outVec[1];
z = outVec[2];
return this;
}
When doing this and also translating vector “real (V)” by adding origin-vector of spehere to “real (V)” vector i would believe i would get the correct new point on the spehere where i would be able to for example set a flag or similar at.
The problem is that something isn’t getting right after the vectors rotation.
If i don’t rotate the sphere and thereby not rotating the vector everything becomes the way i want it. But when i rotate it, the more the rotation the more of i come from the correct point.
Following is a outline from LogCat:
real V x: -0.2881811, y: 0.25460157, z: 0.9231087
rotated V x: 0.10823092, y: 0.79800075, z: 0.5928582
added V x: 0.10823092, y: 0.79800075, z: -3.4071417
subtracted V x: 0.10823092, y: 0.79800075, z: 0.5928583
backRotated V x: -0.5257477, y: 0.21287462, z: 0.8235738
As you can se, when backrotated it’s not the same as the original for some reason.
Vector3 real = Result.getRealVector(location);
Log.d(TAG, "real V "+ real.toString());
real.rotate(-model.getRotationX(), 1f, 0f, 0);
real.rotate(-model.getRotationY(), 0f, 1f, 0);
Log.d(TAG, "rotated V " + real.toString());
real.add(model);
Log.d(TAG, "added V " + real.toString());
real.sub(model);
Log.d(TAG, "subtracted V " + real.toString());
real.rotate(model.getRotationX(), 1f, 0f, 0f);
real.rotate(model.getRotationY(), 0f, 1f, 0f);
Log.d(TAG, "backRotated V " + real.toString());
It’s not that far of, the point is always somwhere near where it should be, but still…
I know it may be hard to understand my problem, somewhat difficult to explain.
What i am hoping for is someone seeing any obvious problem.
With matrix operations order is very important. If you rotate separately around each axis, in order to undo those operations you will need perform the inverse operations in the reverse order to get your original vector back.
To put it mathematically:
(where A and B are matrices, A-1 and B-1 are there respective inverse matrices, and I is the identity matrix)
A * B * B-1 * A-1 == I
however
A * B * A-1 * B-1 != I
I suggest studying a little bit if linear algebra if you have not already, as a good basic understanding is very helpful when doing graphics programming.