ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org

# How to rotate vector by quaternion in python

What is the python tf API to rotate a vector by a quaternion to get another vector?

edit retag close merge delete

Sort by ยป oldest newest most voted

Okay, based on @tfoote's response and this answer to a related question, I think this does the job:

# rotate vector v1 by quaternion q1
def qv_mult(q1, v1):
# comment this out if v1 doesn't need to be a unit vector
v1 = tf.transformations.unit_vector(v1)
q2 = list(v1)
q2.append(0.0)
return tf.transformations.quaternion_multiply(
tf.transformations.quaternion_multiply(q1, q2),
tf.transformations.quaternion_conjugate(q1)
)[:3]

more

2

After a lot of searching, this is what I needed. A comment, the unit_vector conversion might not make sense depending on the application. If you are transforming velocities, you might not want to do the conversion.

( 2018-07-12 10:02:50 -0600 )edit

Can you please explain the math being done here? Also vik748 can you elaborate?

( 2018-10-21 15:06:01 -0600 )edit

and there is no more complicated way?

( 2018-11-02 09:53:07 -0600 )edit

This is what i needed! I just multiplied this resulting vector by my vector length to get the transformation!

( 2019-09-30 02:37:22 -0600 )edit

This answer is concise, matches the code above: https://gamedev.stackexchange.com/que...

Pre-multiply the vector with q and post-multiply it with the conjugate q*

( 2021-11-11 10:11:23 -0600 )edit

We're assuming this is a 3-long vector, right?

( 2021-11-11 13:17:20 -0600 )edit

There's a copy of transformations.py embedded inside tf for linear math operations.

https://github.com/ros/geometry/blob/...

more

1

Right, so which of those transformation rotates a vector by a quaternion, if any?

( 2014-10-28 18:43:41 -0600 )edit
1

Do I need to do matrix multiplications outside of those functions to accomplish this? If so, which? Better yet: where can I find examples of doing this? I've been search online for such an example using python tf, but with no luck.

( 2014-10-28 18:48:59 -0600 )edit

Hi robzz, Have a closer look at the link that tfoote posted; the comments give an explanation and examples.

( 2014-10-28 19:30:22 -0600 )edit

@RickArmstrong Hm I don't see any comments about rotating a vector by a quaternion there. I posted an answer here with some code that seems to work for the few cases that I've tried, using some of those functions. Would you mind taking a look?

( 2014-10-28 19:38:24 -0600 )edit

Or, point me to the location in that 1700 line file that you're referring to :)

( 2014-10-28 19:58:25 -0600 )edit

Yep, that's the ticket. Have a look here to get an idea of /why/ this works.

( 2014-10-29 17:33:39 -0600 )edit