0
\$\begingroup\$

For a game that I am developing I moved from Bullet physics to NVidia Physx. The problem that I have is the following. I used to have a translation from bullet's rigid body orientation quaternion to Front , Right and Up vector for each object on the screen. This was working fine but after I moved to Physx I noticed that there is only one quaternion in the transform of the object (probably representing rotation) and no orientation quaternion. Here is the code that I was using in Bullet to get the 3 vectors translated to Physx :

physx::PxQuat ori = mBody->getGlobalPose().q;

auto Orientation = glm::quat(ori.x, ori.y, ori.z, ori.w);

glm::quat qF = Orientation * glm::quat(0, 0, 0, 1) * glm::conjugate(Orientation);
glm::quat qUp = Orientation * glm::quat(0, 0, 1, 0) * glm::conjugate(Orientation);
front = { qF.x, qF.y, qF.z };
up = { qUp.x, qUp.y, qUp.z };
right = glm::normalize(glm::cross(front, up));

This apparently doesn't work for Physx. Is there another way to retrieve the 3 vectors from the rigid body ?

\$\endgroup\$
4
  • \$\begingroup\$ Your code seems to be mixed up about which argument to glm::quat() is the real component (w) and which are the imaginary components (x, y z). glm::quat(ori.x, ori.y, ori.z, ori.w) this says the w component is last, but this lm::quat qF = Orientation * glm::quat(0, 0, 0, 1) * ... says the w component is first, and the last argument must be z. \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2022 at 14:47
  • \$\begingroup\$ Good observation, but still the orientation of the body is wrong :(. Thanks though \$\endgroup\$
    – Mike F
    Commented Feb 18, 2022 at 15:55
  • \$\begingroup\$ Can you edit your question to show us 1) your modified code 2) a description of what specific wrong behaviour you get when you use that code 3) a test case of the form "I set the rotation like this, and I read the quaternion and get this, and then I get these basis vectors, but I expected these other basis vectors instead" \$\endgroup\$
    – DMGregory
    Commented Feb 18, 2022 at 16:47
  • \$\begingroup\$ I confirm your suggestion is the one that fixes the problem ( I have more problems with other calculations at the moment and got confused). I need to set glm::quat(w,x,y,z). If you make it an answer I will accept it as correct \$\endgroup\$
    – Mike F
    Commented Feb 18, 2022 at 16:49

1 Answer 1

0
\$\begingroup\$

Based on this line:

glm::quat qF = Orientation * glm::quat(0, 0, 0, 1) * glm::conjugate(Orientation);

It looks like glm::quat() is taking arguments in the order w, x, y, z (ie. real component first, then the three imaginary components), so that glm::quat(0, 0, 0, 1) represents a pure imaginary quaternion (a 3D vector) pointing along the z+ axis.

But this line puts w last instead of first:

auto Orientation = glm::quat(ori.x, ori.y, ori.z, ori.w);

This should probably be like this instead, to match the convention used elsewhere in your code:

auto Orientation = glm::quat(ori.w, ori.x, ori.y, ori.z);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .