I have a problem with my rotation on my 3D object (It's in java but it doesn't really matter)
The background:
I have a simple 3d model, and you are a first person player on it, you move your mouse up to look up (I.E Rotate by the 3D's x-axis) and move your mouse down to look down (Rotate in opposite direction)
But:
I also have the left and right arrow keys to turn left/right (Which rotates the 3D's y-axis)
Now the problem is, when I have turned, when I rotate by the x-axis it no longer turns as expected i.e if you turn 180 degrees, by moving your mouse down you actually look up and if you move your mouse up you actually look down.
What rotation can I perform on the x/y axis to fix this?
-So no matter how much/little I have turned, moving mouse up will look up and moving mouse down will look down.
Sorry I can't explain it better, but if you need any more info just make a comment.
Thanks alot,
It's probabaly a simple transformation, but i can't think :(
Some of Java rotation code:
Mouse Up/Down:
public void rotateXY(double radians, double Yradians)
{
vpTrans.getTransform(T3D);
Transform3D test = new Transform3D();
Transform3D testX = new Transform3D();
Transform3D testY = new Transform3D();
translate.set(lastXcord, lastYcord, lastZcord);
testX.rotX(radians);
testY.rotY(Yradians);
test.mul(testX, testY);
test.setTranslation(translate);
vpTrans.setTransform(test);
//System.out.println(test);
} // end of rotateXY()
Left: (right is just similar but with minus instead of plus on angle changes)
public void run() {
Transform3D test = new Transform3D();
Transform3D rotX = new Transform3D();
Transform3D rotY = new Transform3D();
vpTrans.getTransform(T3D);
translate.set(lastXcord, lastYcord, lastZcord);
angle += 0.05;
trueangle += 0.05;
rotX.rotX(angle);
rotY.rotY(Yangle);
test.mul(rotX, rotY);
test.setTranslation(translate);
vpTrans.setTransform(test);
}
What transformations/rotations I need to add to that so no matter my y-axis, camera will look up when mouse is moved up and down when mouse is moved down??
It appears you have a problem with the order of transformations. Unlike numerical multiplication, matrix multiplication is not commutative. 3D applications are very sensitive to the order of transformations.
In English:
if
x
andy
are numbers, thenx * y
=y * x
However, if
x
andy
are matrices, thenx * y
≠y * x
!With that in mind, let's look at what you are doing.
To visualize what is happening with this multiplication, imagine you have a straight line coming out of your head. We'll call this
LineUp
. Now look down at your keyboard, imagineLineUp
tilting with you. Now turn your head 90 degrees onLineUp
. That is what is happening to your rotation matrix. Crazy huh?Now let's reverse the multiplication like so:
test.mul(rotY, rotX);
So sit straight up and look forward, and turn your head/body/chair. Now look down a bit. You rotated on a true
LineUp
axis first, then applied the up/down transformation. Fun yeah?A great way to think about matrix transformations is like an instruction set that has to be in order.
rotY
)rotX
)translate
)Changing the order of one operation changes all the subsequent instructions in funny and outrageous ways.
What you are building is a Camera, so I highly recommend reading how to build one.
A great exercise to understand matrix transformations is building a solar system, complete with orbiting moons and spinning planets. It will be eye opening and very informative.