# Vector3f rotation

Hi,

I must admit, it sounds easy. But I didn't get it…

I have a Vector3f (from Camera.getDirection(); ). I am interested in one angle this vector has. And I want to rotate a second Vector3f by this angle.

I don't know if it can be done with Matrix3f, Quaternion or something.

Thanks in advance for putting in the right direction!

Kind regards

Stefan

I think you have to define what you're doing a bit more tightly when you're dealing in 3D in an unknown frame of reference - it's all relative as they say.

A Vector only makes an angle in relation to some other vector. Presumably one of the world Vectors, but which?

Likewise, you can rotate a vector (considering it as a point) by an angle, but you need an axis to rotate it around.

Given those, you have an angleBetween() method to get the angle between the vectors, and you can create a rotation quaternion using a method fromAngleAxis(). Then multiply the Vector you want to rotate by that.

Ok, thank you very much for your help!

With little modifications it is working and a nearly understand why

I strongly need to read a bit more about 3d vector math stuff…

This is the code which works for me:

```         //your new front vector:         //(if front and back are switched, negate this vector)         Vector3f b0 = new Vector3f(camDir.x, 0, camDir.z).normalize();                 //your new side vector:         //(if left and right are switched, negate this vector)         Vector3f b1 = b0.cross(Vector3f.UNIT_Y).normalize();         //float x = t.x * b0.dot(UNIT_X) + t.y * b0.dot(UNIT_Z);         float x = t.x * b0.x + t.z * b0.z;         //float z = t.x * b1.dot(UNIT_X) + t.y * b1.dot(UNIT_Z);         float z = t.x * b1.x + t.z * b1.z;         Vector3f rotatedTranslationVector = new Vector3f(z, 0, x); ```

Thanks again!

Kind regards,
Stefan

I tried to do it like you said, but my problem is not solved yet.

Vector3f camDir = cam.getDirection();
Vector3f betweenHelpVector = new Vector3f(0, camDir.getY(), camDir.getZ());
float angle = camDir.angleBetween(betweenHelpVector);

With this code an angle is calculated, but it's always a positive value. For my purpose it must be positive if the camDir-Vector is on the right side of betweenHelpVector, and negative, if its on the left side of betweenHelpVector. Do you understand what I mean?

Next step, with this angle I create a Quaternion. I did a few tests with spatial rotation, this quaternion seems to be ok.

Quaternion q = new Quaternion();
q.fromAngleAxis(angle, Vector3f.UNIT_Y);

And now I want to rotate a completely different vector with this Quaternion. What I did is the following:

Vector3f rotatedCompletelyDifferentVector = q.mult(completelyDifferentVector);

But this mult()-Operation does not seem to have an effect on rotatedCompletelyDifferentVector...
Do you have an idea?

Thank you very much in advance!

I dont really understand what you want to do. Maybe you could make a drawing or describe it in words? What kind of objects are involved and why, when and where are they supposed to rotate? ^^

Some hints though:

Vector3f betweenHelpVector = new Vector3f(0, camDir.getY(), camDir.getZ());

this is the projection of your cam direction onto the yz-plane. this plane is like a wall in z-direction with its height in y. it is not the wall that is in the middle of your field of view (if that is what you want).

float angle = camDir.angleBetween(betweenHelpVector);

Both vectors have to be normalized! Don’t know, if camDir already is, the other is probably not (only if x=0 in the first place).

This angle lies in the plane created by the camDir and the betweenHelpVector. This plane can be described by basis vectors (1,0,0) and betweenHelpVector. So it basically runs along the x-axis and is rotated around the same. I dont really know what to make of this angle.

Quaternion rotation works like this: v_rotated = q * v * q’

q’ is the conjugate of q. Both v’s are quaternions with the vectors you are interested in written into their imaginary part. if you want to know how this works, check out this page.

If not, use q.toRotationMatrix() and then matrix.mult(completelyDifferentVector).

1 Like

Hm, there is a problem with angleBetween, because it gives you the smallest angle and doesnt say on which side you are (between 0 and 180 or 180 and 360).

If i understand you correctly, you could try it this way:

```        //your new front vector:         //(if front and back are switched, negate this vector)         Vector3f b0 = new Vector3f(camDir.x, 0, camDir.z).normalize();                  //your new side vector:         //(if left and right are switched, negate this vector)         Vector3f b1 = b0.cross(Vector3f.UNIT_Y).normalize();         //float x = t.x * b0.dot(UNIT_X) + t.z * b0.dot(UNIT_Z);         float x = t.x * b0.x + t.z * b0.z;         //float z = t.x * b1.dot(UNIT_X) + t.z * b1.dot(UNIT_Z);         float z = t.x * b1.x + t.z * b1.z;         Vector3f rotatedTranslationVector = new Vector3f(x, 0, z); ```

It's kind of complicated to explain in short terms. It creates a new, rotated coordinate system out of your camDirection. Then it does a transformation of your original translation into this new coordinate system (the commented dot product stuff which can be simplified because of using unit_x and _z as factors). And this should give you the rotated translation vector.

Hope this works. If back/front and/or left/right are the wrong way around, try to .negate() b0 and/or b1. If front and side are switch, you might have to switch b0 and b1... It depends on where you look in case there is nothing to change to your translation vector. (like you already do when you fetch the translation vector (-getY) ). I'm getting a little confused now too ;)

And if performance > precission, you could forget about normalizing b1. It's almost length 1...

Ah, good idea just to switch x and z down there. Glad it works

I've now started to read this, for that I do not have to bother you again with "simple" vector math :)  :

http://chortle.ccsu.edu/VectorLessons/vectorIndex.html#00

By the names of the chapters it looks like a good compilation. Have fun with your newly attained wisdom

PS: Just tried out the Shannonizer, which gave an elaborate translation of my explanation as edited by Dr. Suess:

“It’s kind of peppermint cucumber sausage-paste butter! Oh dear! No! Made of the commented dot product stuff which can be simplified because of your original translation vector -getY). I’m getting a transformation of complicated to wink and side are the light of using unit_x and _z as factors). I’m a little car with their paddles in case there is nothing to .negate( b0 and/or left/right are switch, you sing with ten cats on your original translation vector -getY). And this new, try to explain in a transformation of your original translation into this works. Take a bottle with a Ying. They come along humming, in case there is chewing. Hope this should give you sing with heads in short terms. like you look! They’re like green eggs and b1. Hope this?”

I hope, this clarifies things