Rotation matrix for direction vector

I have been playing with some algorithms on the Internet for a while, and I cannot get them to work, so I ask the question here:

I am trying to display the line of the velocity vector from a point. Drawing a line is not difficult: just insert a line with the velocity.length into the graph. This puts a line centered at a point in the y axis direction. We need to get it now in the right rotation and translation.

The translation vector is easy to calculate: it is equal to half the velocity vector. However, the rotation matrix is ​​extremely elusive to me. Given the directional vector <x, y, z> , which matrix do I need?

Edit 1: Look; if you don’t understand the question, you probably won’t be able to give me an answer.

Here is what I have now:

  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 on in this article . And yes, I tried the standard rotation matrix (vec_x.x, vec_y.x, etc.), and it did not work. I rotated the columns and rows to see if there is an effect.

Edit 2:

Apologizes for the rude wording of my comments.

So it looks like there is a combination of two errors; one of which indicated MD MD (a really bad designation of variables: vec_z is actually vec_y , etc.), and the other is that I needed to invert the matrix before passing it to the rendering mechanism (transpose was Close!). Thus, the modified code:

  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 (); 
+4
source share
2 answers

Dupe.

The question is getting rotation to a certain axis, while I'm interested in getting a rotation matrix.

Yes, I wonder if you can turn convert one into another ?

By the way, your current decision about choosing an arbitrary y axis, and then re-orthonalization should work fine; he looks audible, although at least poorly written. ' z_vec ' is not a good variable name for the y axis. What about the ordering of "z, x, y"?

If this still does not work, try to make random changes until this happens - transfer the matrix, neutralize the vectors, until you get an even number of symbolic errors, something like this.

And your tone of voice is somehow rude, given that you are asking strangers to spend their time helping you.

+1
source

That should make you

+1
source

All Articles