# Rotate vector around axis (3D)

This topic is 2298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Continuing with the brainstorming questions.

In the following figure, there is a vector which is basically the normal to the vertex base point (where it departs from).
I'd like to rotate it around the axis that is orthogonal to it, by some angle alpha.

Thanks to my unlimited math skills, I decided to proceed this way:

1) calculate the orthogonal axis, to be used as axis of rotation, doing the cross product between the vector and 0,-1,0.
2) rotate the vector around such axis.

Here is how did in code:

 vector3_t Vector1 = (Vector[0]-Vector[1]).Normalize(); // calculate the normalized vector P0-P1 vector3_t vDown(0,-1,0); // down vector vector3_t vDir = Vector1 ^ VDown; // this is the orthogonal axis that will be used as a rotation axis for (int p=0; p<POINTS; p++) { Q rotation = rotation_from_angle_and_axis(angle, vDir); vector3_t vDiff=Vector-Vector[0]; // the segment from actual point and base point 0 qRotateV(rotation, vDiff); // perform the rotation Vector

=vDiff+Vector[0]; } 

 typedef boost::math::quaternion<float> Q; Q rotation_from_angle_and_axis(float angle, vector3_t myVector) { float half_angle = angle*0.5f; float cosine_of_half_angle = std::cos(half_angle); float sine_of_half_angle = std::sin(half_angle); return Q(cosine_of_half_angle, sine_of_half_angle * myVector.x, sine_of_half_angle * myVector.y, sine_of_half_angle * myVector.z); } void qRotateV(Q rotation, vector3_t &myVector) { Q v(0.0f, myVector.x, myVector.y, myVector.z); Q v_prime = rotation * v * conj(rotation); myVector.x = v_prime.R_component_2(); myVector.y = v_prime.R_component_3(); myVector.z = v_prime.R_component_4(); } 

It works at some extents, meaning the points are rotated, but the initial length is not respected: some lines are shorter, some are longer, while instead they should all maintain the original length.
Certainly I screwed something.

Maybe some kind soul can take a look at this, or teach me a better way to perform this kind of rotation.
Many thanks. Edited by Alessandro

##### Share on other sites

In the following figure, there is a vector which is basically the normal to the vertex base point (where it departs from).
I'd like to rotate it around the axis that is orthogonal to it, by some angle alpha.

You lost me at "the axis that is orthogonal to it". In 3D, there are many axes that are orthogonal to any given direction.

If you are OK with picking any of them, taking the cross product with some vector that is not aligned with the original vector is a reasonable way to obtain a perpendicular vector. Note that the result of the dot product does not necessarily have unit length, so you may need to normalize it.

##### Share on other sites
Hi Alvaro, thanks for the hint about the normalization of the cross product, I'm going to try that.

About the "You lost me at "the axis that is orthogonal to it". In 3D, there are many axes that are orthogonal to any given direction.", if you have a vector(0,1,0) and a vector(0,0,1), isn't the cross product giving you the one and only orthogonal axis represented by 1,0,0 ?

##### Share on other sites
If you have the vector (0,1,0), any vector of the form (x,0,z) is orthogonal to it. If you only want to consider vectors of length 1, you still have a 1-parameter family of orthogonal vectors (cos(a),0,sin(a)).

##### Share on other sites
I believe the orthogonal term I used then it's wrong. It's difficult for me to explain in words, so I made another image that perhaps should explain more.
Basically you have the vector normal for a certain point (in red); I'd like to rotate such vector around another one (represented in green), which is orthogonal (no that's the wrong term, then probably I should say that they are perpendicular to each other?) by some degree.

The light blue vector is the rotated one. You can see from the top view that the rotation has to occur around the green axis (vector).

Hope this is a bit clearer... Edited by Alessandro

##### Share on other sites
Whether you call it orthogonal or perpendicular, the situation is the same. The green axis in your diagram seems to be the tangent vector along the parallel. There is also a tangent vector along the meridian, which is also orthogonal to the red vector.

Perhaps you can describe what you are trying to do in more detail, and then it should become obvious whether you can use any orthogonal vector or if you need to use the one along the parallel.

By the way, what happens when the red vector is pointing straight up (at the North pole)?

##### Share on other sites

By the way, what happens when the red vector is pointing straight up (at the North pole)?

I guess in that case it won't rotate at all.
However, in the above code, I normalized the cross product (you suggested to normalize the dot product, but there are none there, so I think you meant the cross product?), and now rotations seem to occur properly.
 vector3_t vDir = (Vector1 ^ VDown).Normalize(); // this is the orthogonal axis that will be used as a rotation axis 
In the attached screenshot a "working example" of the operation I need to get done. Basically the hair are just the normal vectors to each mesh vertex. I need to rotate those vectors by some angle (in the example I performed a 45* rotation).
You see that the rotated hair are somewhat "coherent" with initial orientation.

##### Share on other sites
Ooops! Yes, I meant cross product. Sorry about that.

Yes, if you want hair to go down by a certain angle, it makes sense to use the direction of the parallel as the point of rotation. If the original model has hair pointing downwards it would do something funny, though.

The really cool thing to do in this case would be to use Physics to affect hair direction. Use a damped "angular spring" (which tries to keep the angle close to the original pose) plus gravity, you should get a nice effect. If you can do it in real time, the hair would also react to other accelerations, not just gravity, and you can make it move with wind as well, plus it would have a bit of bounciness.

##### Share on other sites

Ooops! Yes, I meant cross product. Sorry about that.

No problem. Instead, many thanks because your hint solved the issue. Now points rotate properly.

The really cool thing to do in this case would be to use Physics to affect hair direction. Use a damped "angular spring" (which tries to keep the angle close to the original pose) plus gravity, you should get a nice effect. If you can do it in real time, the hair would also react to other accelerations, not just gravity, and you can make it move with wind as well, plus it would have a bit of bounciness.

That would indeed cool. Now that I can rotate points, I'm going to test hair intersection (the model is subdivided in groups, like head, neck, torso etc.). I'll first perform point intersection to bounding boxes to get the group actually intersected, and then perform a more accurate intersection on the polygons of the intersected group.

The ultimate goal would be, as you also suggested, to apply gravity or forces in general so that the hair will respond to those and adjust accordingly. I don't know where to start with it: I know there are some free physics engine SDK's that can be possibly integrated, but I'd prefer to code my own. I'll search for some information around.
Don't you all be surprised if one day there will be a thread of mine called "Applying physics to strings". Edited by Alessandro

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633731
• Total Posts
3013582
×