Incorrect RigidBody rotation angle around the axes (Euler angles) #358
-
Hello, I'm writing a retro game engine, I chose your physics engine as it is quite lightweight and productive for old systems. But when testing a simple scene with a floor and a cube falling on it rotated around its axis, the falling cube behaves very strangely. I attach a video, it shows the strange behaviour of the cube, when the cube rises, I give it acceleration on the Y axis. In the terminal, the video shows the cube rotation angle and rotation coefficient on one line, and the cube position on the Y axis on the other line. Sorry for bad English, I use a translator. video.mp4Code for the creation of physical bodiesPhysicsCommon physicsCommon;
PhysicsWorld* physicsWorld = physicsCommon.createPhysicsWorld();
Vector3 floor_body_vec(0, 0, 0);
Quaternion floor_body_quat = Quaternion::fromEulerAngles(0, 0, 0);
Transform floor_body_trans(floor_body_vec, floor_body_quat);
RigidBody* floor_body = physicsWorld->createRigidBody(floor_body_trans);
BoxShape* floor_col_shape = physicsCommon.createBoxShape({50.0f, 1.0f, 50.0f});
Collider* floor_col = floor_body->addCollider(floor_col_shape, Transform::identity());
Vector3 rbody_body_vec(0, 6, 0);
Quaternion rbody_body_quat = Quaternion::fromEulerAngles(1, 0, 3.9);
Transform rbody_body_trans(rbody_body_vec, rbody_body_quat);
RigidBody* rbody_body = physicsWorld->createRigidBody(rbody_body_trans);
BoxShape* rbody_col_shape = physicsCommon.createBoxShape({1.0f, 1.0f, 1.0f});
Collider* rbody_col = rbody_body->addCollider(rbody_col_shape, Transform::identity());
Vector3 rbodyPosVect = floor_body->getTransform().getPosition(); Code for outputting the angle and rotation of the cube meshphysicsWorld->update((float)delta);
decimal dec;
Vector3 vec;
rbody_body->getTransform().getOrientation().getRotationAngleAxis(dec, vec);
std::cout << dec * vec.x * 180.0 / M_PI << " | " << vec.y * dec * 180 / M_PI << " | " << vec.z * dec * 180 / M_PI<< " | " << dec << "\n";
Vector3 rbodyPosVect = rbody_body->getTransform().getPosition();
std::cout << "Rbody Y pos: " << rbodyPosVect.y << "\n";
mesh1.setPosition(rbodyPosVect.x, rbodyPosVect.y, rbodyPosVect.z);
mesh1.setRotation(vec.x * dec * 180.0f / M_PI,
vec.y * dec * 180.0f / M_PI,
vec.z * dec * 180.0f / M_PI); |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
First, you should try to compute a correct inertia tensor for your body (as described here) using the following method after you have added the collider.
Secondly, I am not sure I understand. Are you manually rotating/accelerating the body or is it simply falling down on the floor? |
Beta Was this translation helpful? Give feedback.
-
Sorry but I really have a hard time understanding what is going on in your simulation and what you are trying to do. |
Beta Was this translation helpful? Give feedback.
-
I rotate it in the beginning before it drops, and then I have no effect on it at all. I did some additional testing and noticed that Example:Quaternion quaternion = Quaternion::fromEulerAngles(1, 0, 1);
decimal angle;
Vector3 rot_axis;
quaternion.getRotationAngleAxis(angle, rot_axis);
std::cout << rot_axis.x * angle << " | "
<< rot_axis.y * angle << " | "
<< rot_axis.z * angle << " Angle: "
<< angle << "\n"; Console output:
|
Beta Was this translation helpful? Give feedback.
-
This method return the axis of rotation and angle of rotation (in radians) of a quaternion. What is strange about those values? |
Beta Was this translation helpful? Give feedback.
-
Ah, now I get it, I thought this method was returning Euler angles. Sorry for wasting your time, I'll try to convert quaterion to Euler angles or use quaterion at all. |
Beta Was this translation helpful? Give feedback.
This method return the axis of rotation and angle of rotation (in radians) of a quaternion. What is strange about those values?