简体   繁体   中英

Rotation matrix for direction vector

I've been playing with some algorithms on the internet for a while and I can't seem to get them to work, so I'm tossing the question out here;

I am attempting to render a velocity vector line from a point. Drawing the line isn't difficult: just insert a line with length velocity.length into the graph. This puts the line centered at the point in the y-axis direction. We need to get this now in the proper rotation and translation.

The translational vector is not difficult to calculate: it is half the velocity vector. The rotational matrix, however, is being exceedingly elusive to me. Given a directional vector <x, y, z> , what's the matrix I need?

Look; 看; if you don't understand the question, you probably won't be able to give me an answer.

Here is what I currently have:

Vector3f translation = new Vector3f();
                    translation.scale(1f/2f, body.velocity);

                    Vector3f vec_z = (Vector3f) body.velocity.clone();
                    vec_z.normalize();

                    Vector3f vec_y; // reference vector, will correct later
                    if (vec_z.x == 0 && vec_z.z == 0) {
                        vec_y = new Vector3f(-vec_z.y, 0f, 0f); // could be optimized
                    } else {
                        vec_y = new Vector3f(0f, 1f, 0f);
                    }
                    Vector3f vec_x = new Vector3f();
                    vec_x.cross(vec_y, vec_z);
                    vec_z.normalize();

                    vec_y.cross(vec_x, vec_z);
                    vec_y.normalize();
                    vec_y.negate();

                    Matrix3f rotation = new Matrix3f(
                        vec_z.z, vec_z.x, vec_z.y,
                        vec_x.z, vec_x.x, vec_x.y,
                        vec_y.z, vec_y.x, vec_y.y
                    );

                    arrowTransform3D.set(rotation, translation, 1f);

based off of this article . And yes, I've tried the standard rotation matrix (vec_x.x, vec_y.x, etc) and it didn't work. I've been rotating the columns and rows to see if there's any effect.

Apologies about the rude wording of my comments.

So it looks like there were a combination of two errors; one of which House MD pointed out (really bad naming of variables: vec_z was actually vec_y , and so on), and the other was that I needed to invert the matrix before passing it off to the rendering engine (transposing was close!). So the modified code is:

Vector3f vec_y = (Vector3f) body.velocity.clone();
                    vec_y.normalize();

                    Vector3f vec_x; // reference vector, will correct later
                    if (vec_y.x == 0 && vec_y.z == 0) {
                        vec_x = new Vector3f(-vec_y.y, 0f, 0f); // could be optimized
                    } else {
                        vec_x = new Vector3f(0f, 1f, 0f);
                    }

                    Vector3f vec_z = new Vector3f();
                    vec_z.cross(vec_x, vec_y);
                    vec_z.normalize();

                    vec_x.cross(vec_z, vec_y);
                    vec_x.normalize();
                    vec_x.negate();

                    Matrix3f rotation = new Matrix3f(
                        vec_x.x, vec_x.y, vec_x.z,
                        vec_y.x, vec_y.y, vec_y.z,
                        vec_z.x, vec_z.y, vec_z.z
                    );
                    rotation.invert();

应该是你

Dupe.

The question there involves getting a rotation to a certain axis, whereas I'm concerned with getting a rotation matrix.

Gee, I wonder if you could turn convert one to the other ?

BTW, your current solution of picking an arbitrary y axis and then reorthogonalising should work fine; it looks bugged though, or at least badly written. ' z_vec ' is not a good variable-name for the y-axis. What's with the 'z,x,y' ordering, anyway?

If it still doesn't work, try making random changes until it does - transpose the matrix, negate vectors until you have an even number of sign errors, that kind of thing.

Also your tone of voice comes across as sort-of rude, given that you're asking strangers to spend their time helping you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM