Rotate 3D vector around axis

I want to rotate a vector around a given axis and I am looking for the most convenient way to do this.
Coming from OpneGL and GLM I expected the Vec3 class to have a rotate function, taking a rotation axis and an angle as arguments, but I couldn’t find anything like that in the API.
The best way to rotate a vector, that I can currently think of, is by implementing my own function, but I’d really like to avoid that. Especially if there is already an easier way to do this in Panda.

So is there anything like a vector rotate function?

When dealing with rotations, it’s probably best to use quaternions, so you could use Quat.setFromAxisAngle:

from panda3d.core import *

axis = Vec3(-5.4, 7.3, 1.8)
# the axis vector must be unit length
axis.normalize()
# the angle is given in degrees
angle = 34.5
quat = Quat()
quat.setFromAxisAngle(angle, axis)
vec = Vec3(2.6, -3.1, 4.9)
rotated_vec = quat.xform(vec)
2 Likes