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

ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |

The site is read-only. Please transition to use Robotics Stack Exchange

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?

add a comment

11

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]
```

2

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*

3

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

1

1

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

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

Asked: ** 2014-10-28 17:58:31 -0500 **

Seen: **11,123 times**

Last updated: **Nov 11 '21**

ROS Answers is licensed under Creative Commons Attribution 3.0 Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.