开发者

Whats wrong with my Matrix Rotation?

开发者 https://www.devze.com 2023-01-28 06:34 出处:网络
I\'m trying to rotate a model by (float) Math.atan2(-camX.getXf() * padX, -camDir.getZf() * padY) Y and -MathUtils.HALF_PI

I'm trying to rotate a model by

(float) Math.atan2(-camX.getXf() * padX, -camDir.getZf() * padY)

Y

and

-MathUtils.HALF_PI

Z

But

model.setRotation(new Matrix3(1,0,0,
                              0,(float) Math.atan2(-camX.getXf() * padX, -camDir.getZf() * padY),0,
                              0,0,-MathUtils.HALF_PI));

It rotates on the y axis (Though it's sideways because it's a md2 model) but rotating the Z axis doesn't make it right side up. Any idea why?

Each variable is in it's respective area of the matrix.

EDIT: alright, now I'm using this code:

float x = 0;
            float y = (float) Math.atan2(-camX.getXf() * padX, -camDir.getZf() * padY);
            float z = (float)开发者_如何转开发 -MathUtils.HALF_PI;

            float a = (float) Math.sin(x);
            float A = (float) Math.cos(x);
            float b = (float) Math.sin(y);
            float B = (float) Math.cos(y);
            float c = (float) Math.sin(z);
            float C = (float) Math.cos(z);

            Matrix3 m = new Matrix3(A*b, -(B*a),b,
                                   (C*a)+(A*b*c), (A*C)-(a*b*c), -(B*c),
                                   (a*c)-(A*C*b), (A*c)+(C*a*b), B*C); 

But now none of the axis are rotating correctly.

This is how the matrix is set up:

xx, xy, xz, 
yx, yy, yz, 
zx, zy, zz


Rotation matrices don't work this way. Angles don't go into matrices! Instead I assume that Java handles a rotation matrix just like any other transformation matrix in cartesian coordinates. Since I think you don't want to input the rotation matrix by hand, you are probably better off starting with a new Matrix3 (I hope it is automatically initialized at the identity matrix), and then successively rotating it using rotateX(float x), rotateY(float y) and rotateZ(float z), where x, y, z are the angles you want to rotate about. (In case you are using com.threed.jpct.Matrix, at least.) Note that the result does depend on the succession of the three rotations.


Here is a typical tutorial on how to use rotation matrices http://www.euclideanspace.com/maths/algebra/matrix/orthogonal/rotation/index.htm. The order of applying rotations round the three axes is critical. Alternatively you can rotate about an arbitrary axis. Also you may want to explore quaternions.


This is what a rotation matrix looks like in 2D; it rotates a point in (x,y) space about the z-axis in the counterclockwise direction.

http://en.wikipedia.org/wiki/Rotation_matrix

0

精彩评论

暂无评论...
验证码 换一张
取 消