zhengwenxin Posted May 11, 2017 Posted May 11, 2017 Hi everybody, I want to control the player rotate, I use the function like below: Unigine::PlayerPtr player = Unigine::Game::get()->getPlayer(); Unigine::Math::mat4 originalMat = player->getWorldTransform(); Unigine::Math::mat4 transform = originalMat * Unigine::Math::rotateY(rot.y) * Unigine::Math::rotateX(rot.x); player->setWorldTransform(transform); The rot.y and rot.x is variables that I pass. If only change one variable the perform is right, but if the both variable have changed it perform the axis Z also rotate. I just want to rotate the axis X and Y , but the Z rotate too. Is there anybody know this is why? Thanks!
alexander Posted May 11, 2017 Posted May 11, 2017 Hi, zhengwenxin! "I use the function like below" Do you use these four lines in each update()? Or the first two in the init(), and the second in the update()? "The rot.y and rot.x is variables that I pass" Are these absolute angles or relative to the previous frame? If you use this in each update() and variables are relative: Rotations are extremely order-dependent. Doubly so when you're composing rotations in local space (so the axes you're rotating around are themselves rotating from one frame to the next). As an extreme example, imagine that you start facing -z, and in one frame you pitch (x rotation) 90 degrees up. In the next frame, you yaw (y rotation) 90 degrees left. Since you're already looking straight up, your local y axis is pointing "back" along the world z axis, so this leaves you looking along the -x axis, with your local y axis still pointing "back" along world z. This is the same as yawing 90 degrees left and then rolling (z rotation) 90 degrees left. So: composing local x & y rotations, compounded from frame to frame, can turn into z rotations. One way to fix this is to always pitch along your local x axis, but always yaw around the world z axis (if you always want the camera "up" vector facing up): mat4 transform = translate(originalMat.getTranslate()) * rotateZ(rot.y) * rotation(originalMat) * rotateX(rot.x); Best regards, Alexander
zhengwenxin Posted June 13, 2017 Author Posted June 13, 2017 Hi Alexander, Thanks for your reply. I find use your way the rotation angles(pan,tilt,roll) data all change. Is there a way to make it only the axis angle change which I give it value(for example I give x axis 2.0 degree, the x axis rotate 2 degree). Regards, wenxin
alexander Posted June 26, 2017 Posted June 26, 2017 Hi! If you want to add some rotation, use: player->setRotation(player->getRotation() * quat(x_axis_angle, y_axis_angle, z_axis_angle)); // local rotation player->setRotation(quat(x_axis_angle, y_axis_angle, z_axis_angle) * player->getRotation()); // global/world rotation If you want to set some rotation, use: player->setRotation(quat(x_axis_angle, y_axis_angle, z_axis_angle)); If you want to get some rotation in euler angle values, use: vec3 euler = decomposeRotationXYZ(mat3(player->getRotation())); Best regards, Alexander
Recommended Posts