Closed brizzly closed 4 years ago
Hello. Thanks for your question.
If you are not familiar with quaternions, I suggest you to read this article that explains how a quaternion can be used to represent a rotation around a given axis in 3D.
In ReactPhysics3D, the Quaternion class has a fromEulerAngles() method that can help you to create a quaternion that rotates around the X, Y and Z axis as you can see in the following example. Note that the angles must be specified in radians.
rp3d::Quaternion rotation = rp3d::Quaternion::fromEulerAngles(angleX, angleY, angleZ);
Thank you very much.
I understand better how to setup a quaternion from scratch from identity with euler angles, but I need to know how to add radian values to an existing quaternion, I mean how to read the euler angles from quaternion? This is definitly not a tricky part, and needed for games imagine you need to rotate a character (and collision box body) or any mesh from user keyboard inputs how do we do that I dont know. My code below seems not really working for all axies.. Thank you in advance for your work and help.
rp3d::Quaternion orr = currTransform.getOrientation();
rp3d::Matrix3x3 R = orr.getMatrix();
float sy = sqrt( R[0][0] * R[0][0] + R[1][0] * R[1][0] );
bool singular = sy < 1e-6; // If
float x, y, z;
if (!singular)
{
x = atan2(R[2][1], R[2][2]);
y = atan2(-R[2][0], sy);
z = atan2(R[1][0], R[0][0]);
}
else
{
x = atan2(-R[1][2], R[1][1]);
y = atan2(-R[2][0], sy);
z = 0;
}
vec3_t player_rot;
player_rot[0] = Mathematics::RadToDeg( x );
player_rot[1] = Mathematics::RadToDeg( y );
player_rot[2] = Mathematics::RadToDeg( z );
player->SetRot(player_rot);
As explained there, a quaternion represents a rotation, if you want to add more rotation, you simply need to multiply the quaternion of a second rotation with the quaternion of the first rotation.
Note that you should avoid working too much with Euler angles. You should try to work only with quaternions and matrices. Take a look at this article for more information. Quaternions are used a lot in computer graphics and they can save you from many problems and bugs that you can have with Euler angles.
You can find in this article how to convert a quaternion to Euler angles if needed.
Note that the Quaternion class in ReactPhysics3D contains a getRotationAngleAxis() method that can help you extract the rotation axis and angle (in radian) of a given quaternion.
Ok I get it, that is indeed very interesting , It seems your explained me everything I needed. Thank you so much!
You are welcome. I am closing this issue but do not hesitate to get back if something is not clear.
Hi, thanks for the great work.
I struggle and can't find how to rotate a collision body, it's indeed pretty unclear how to rotate from a Quaternion.
I would like to rotate left or right around Y axis a collision body like a character when I press a key, and also to be able to read the current angular X Y Z values in order to update my model orientation matrix before rendering it.
Moving a collision body is straight forward and very easy, and well documented but rotation I just can't find how to do it even testbed project doesn't really help because of many matrix..
Let me know and maybe update documentation with a few lines of code ? Thanks a lot!
Documentation :
My code (not working well..) :
Other code (not so good) :
@DanielChappuis Maybe you can steer me in the the right direction and update the documentation on how to read and force a new XYZ orientation from Euler radian values? Thank you very much great work!