Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to handle rotation when its magnitude is 0 ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 ryt   Members   -  Reputation: 294

Like
0Likes
Like

Posted 04 November 2013 - 02:43 PM

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 ?



Sponsor:

#2 Ravyne   GDNet+   -  Reputation: 7409

Like
1Likes
Like

Posted 04 November 2013 - 02:58 PM

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.

 

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


Edited by Ravyne, 05 November 2013 - 12:33 PM.


#3 ryt   Members   -  Reputation: 294

Like
0Likes
Like

Posted 04 November 2013 - 03:18 PM

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.



#4 Ravyne   GDNet+   -  Reputation: 7409

Like
2Likes
Like

Posted 04 November 2013 - 04:32 PM

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



#5 ryt   Members   -  Reputation: 294

Like
0Likes
Like

Posted 05 November 2013 - 02:31 PM

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) ?



#6 Ravyne   GDNet+   -  Reputation: 7409

Like
1Likes
Like

Posted 05 November 2013 - 02:50 PM

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).



#7 ryt   Members   -  Reputation: 294

Like
0Likes
Like

Posted 06 November 2013 - 11:59 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS