# How to handle rotation when its magnitude is 0 ?

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

## Recommended Posts

Lets say we have two vectors, Vector a (1, 0, 0) and b (0, 1, 0). If we want to rotate from a to b we need to find dot product for angle like angle = cos-1 (a.b) and we need to take a cross product for rotation vector that is (0, 0, 1).

But what if the two vectors are collinear, like for eg. both are (0, 1, 0). Then the cross product is (0, 0, 0) that is its magnitude is 0. So its no rotation, but how do we need to handle this in code ?

Should we test for this case or we can just leave it with its magnitude 0 ?

##### Share on other sites

Its not really that there's no rotation necessarily. The vectors are co-linear only if they point in exactly the same direction or exactly opposite directions. In the first case, no rotation is needed to get from a to be since they're the same, in the second case you need to rotate 180 degrees -- what the cross product is really telling you here is that all axes of rotation are equally good -- there is no shortest rotation from a to b, so the choice is arbitrary. You can handle that particular rotation as a special case and choose a fixed axis (probably the one linearly independent of the two primary axes of play, if applicable), choose any suitable axis of rotation at random, or one which best reflects any constraints of the physical object in question, if applicable.

 Strikethrough: Realized a fixed axis isn't going to cut it for arbitrary rotations.

Edited by Ravyne

##### Share on other sites

Ok, lets say we have the first case, that two vectors are (0, 1, 0). How do we handle this ?

Do we just let the code pass and try to rotate around a cross that is (0, 0, 0) and an angle of 0 ?

For the second case if vector a is (0, 1, 0) and b is (0, -1, 0), how can we find a perpendicular vector to lets say vector a ? I thought maybe to take a cross product with some other vector like up vector and then normalize the result. But in this case we have vector a same as up so again our cross is 0. We could take then another vector and cross it again but that way we could arrive at the same thing if our vector a was something else.

##### Share on other sites

In the first case there is no rotation: if(a == b), just do nothing and return -- unless you mean that a == b might correspond to any integer multiple of 360 degree rotation that you actually want to happen, then you need to handle that too in some way, but you can get an axis of rotation the same as in the second case.

In the second case you can get a perpendicular vector just by dropping one component of the three, swapping the other two, and negating one of them. The particular vector depends on the input vector, of course, but its an easy, arbitrary vector when all choices are otherwise equally good.

If you want the whole set of perpendicular vectors, see the answer here, which explains better than I'll be able to: http://math.stackexchange.com/questions/137362/how-to-find-perpendicular-vector-to-another-vector

##### Share on other sites

Thank you very much, its a lot more clear to me now.

For the first case you sad just do nothing and return. The thing I'm asking this is because I use quaternion for rotation and if my rotation vector (result form cross a and b) is (0, 0, 0) and angle is 0, will I then get any unexpected rotation or an error since quaternion will then look like (1, 0, 0, 0) (for w = 1 and x, y, z = 0) ?

##### Share on other sites

You probably shouldn't attempt to do the rotation at all when you know the current and intended direction are the same (that is, there is no rotation) -- you might need to detect this occurrence at a higher level where you can skip the quaternion rotation, though.

An alternative, like I said before, is to use the same arbitrary perpendicular vector from the second case as I described before -- its already perpendicular so you don't need to do the cross product at all in either of these special cases. But you'll at least have a non-zero vector for the axis of rotation, and rotating by 0 degrees will leave the object unchanged, which I think is what you want. This way may be easier than detecting a 0-degree rotation and skipping it where the quaternion is applied.

If you don't need to support rotations of greater than 360 degrees (e.g. where 540 degrees is 1.5 turns) then in the case that the current and intended direction are identical (but not opposite) you can actually choose any vector at all to "rotate" around, because you rotate by 0 degrees anyhow, and the result is the same for any vector (modulo potential floating point error, I suppose, but should be too small to notice).

##### Share on other sites

I haven't realized before but quaternion (1, 0, 0, 0) is a unit quaternion so rotating around that I assume it would be ok, actually the rotation would remain the same.

Anyway thank you again.

×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!