From d74af2f1a741386f646c8522eb1644fb6c554b0e Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Thu, 30 Jan 2025 14:11:16 +0100 Subject: [PATCH] Use matrix4::getRotationRadians --- irr/src/CAnimatedMeshSceneNode.cpp | 2 +- irr/src/CXMeshFileLoader.cpp | 2 -- src/client/clientenvironment.cpp | 4 ++-- src/client/game.cpp | 7 ++++--- src/client/hud.cpp | 2 +- src/client/hud.h | 12 +++++++++--- src/raycast.cpp | 6 +++--- src/server/unit_sao.h | 2 +- src/unittest/test_irr_matrix4.cpp | 23 ++++++++++++----------- src/unittest/test_irr_rotation.cpp | 2 +- 10 files changed, 34 insertions(+), 28 deletions(-) diff --git a/irr/src/CAnimatedMeshSceneNode.cpp b/irr/src/CAnimatedMeshSceneNode.cpp index 6facfcd06..09d83038b 100644 --- a/irr/src/CAnimatedMeshSceneNode.cpp +++ b/irr/src/CAnimatedMeshSceneNode.cpp @@ -619,7 +619,7 @@ void CAnimatedMeshSceneNode::animateJoints(bool CalculateAbsolutePositions) // Code is slow, needs to be fixed up - const core::quaternion RotationStart(PretransitingSave[n].getRotationDegrees() * core::DEGTORAD); + const core::quaternion RotationStart(PretransitingSave[n].getRotationRadians()); const core::quaternion RotationEnd(JointChildSceneNodes[n]->getRotation() * core::DEGTORAD); core::quaternion QRotation; diff --git a/irr/src/CXMeshFileLoader.cpp b/irr/src/CXMeshFileLoader.cpp index c09f2e481..ed8c18350 100644 --- a/irr/src/CXMeshFileLoader.cpp +++ b/irr/src/CXMeshFileLoader.cpp @@ -1526,8 +1526,6 @@ bool CXMeshFileLoader::parseDataObjectAnimationKey(SkinnedMesh::SJoint *joint) os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING); } - // core::vector3df rotation = mat.getRotationDegrees(); - AnimatedMesh->addRotationKey(joint, time, core::quaternion(mat.getTransposed())); AnimatedMesh->addPositionKey(joint, time, mat.getTranslation()); diff --git a/src/client/clientenvironment.cpp b/src/client/clientenvironment.cpp index 42a31ae8c..ed9a23ce3 100644 --- a/src/client/clientenvironment.cpp +++ b/src/client/clientenvironment.cpp @@ -441,8 +441,8 @@ void ClientEnvironment::getSelectedActiveObjects( GenericCAO* gcao = dynamic_cast(obj); if (gcao != nullptr && gcao->getProperties().rotate_selectionbox) { gcao->getSceneNode()->updateAbsolutePosition(); - const v3f deg = obj->getSceneNode()->getAbsoluteTransformation().getRotationDegrees(); - collision = boxLineCollision(selection_box, deg, + const v3f rad = obj->getSceneNode()->getAbsoluteTransformation().getRotationRadians(); + collision = boxLineCollision(selection_box, rad, rel_pos, line_vector, ¤t_intersection, ¤t_normal, ¤t_raw_normal); } else { collision = boxLineCollision(selection_box, rel_pos, line_vector, diff --git a/src/client/game.cpp b/src/client/game.cpp index 8ff7050cd..968cddf01 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -3239,9 +3239,10 @@ PointedThing Game::updatePointedThing( hud->setSelectionPos(pos, camera_offset); GenericCAO* gcao = dynamic_cast(runData.selected_object); if (gcao != nullptr && gcao->getProperties().rotate_selectionbox) - hud->setSelectionRotation(gcao->getSceneNode()->getAbsoluteTransformation().getRotationDegrees()); + hud->setSelectionRotationRadians(gcao->getSceneNode() + ->getAbsoluteTransformation().getRotationRadians()); else - hud->setSelectionRotation(v3f()); + hud->setSelectionRotationRadians(v3f()); } hud->setSelectedFaceNormal(result.raw_intersection_normal); } else if (result.type == POINTEDTHING_NODE) { @@ -3261,7 +3262,7 @@ PointedThing Game::updatePointedThing( } hud->setSelectionPos(intToFloat(result.node_undersurface, BS), camera_offset); - hud->setSelectionRotation(v3f()); + hud->setSelectionRotationRadians(v3f()); hud->setSelectedFaceNormal(result.intersection_normal); } diff --git a/src/client/hud.cpp b/src/client/hud.cpp index e0b4e83fc..aa9544486 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -880,7 +880,7 @@ void Hud::drawSelectionMesh() core::matrix4 translate; translate.setTranslation(m_selection_pos_with_offset); core::matrix4 rotation; - rotation.setRotationDegrees(m_selection_rotation); + rotation.setRotationRadians(m_selection_rotation_radians); driver->setTransform(video::ETS_WORLD, translate * rotation); if (m_mode == HIGHLIGHT_BOX) { diff --git a/src/client/hud.h b/src/client/hud.h index aa4d53cda..b42435f25 100644 --- a/src/client/hud.h +++ b/src/client/hud.h @@ -74,9 +74,15 @@ public: v3f getSelectionPos() const { return m_selection_pos; } - void setSelectionRotation(v3f rotation) { m_selection_rotation = rotation; } + void setSelectionRotationRadians(v3f rotation) + { + m_selection_rotation_radians = rotation; + } - v3f getSelectionRotation() const { return m_selection_rotation; } + v3f getSelectionRotationRadians() const + { + return m_selection_rotation_radians; + } void setSelectionMeshColor(const video::SColor &color) { @@ -129,7 +135,7 @@ private: std::vector m_halo_boxes; v3f m_selection_pos; v3f m_selection_pos_with_offset; - v3f m_selection_rotation; + v3f m_selection_rotation_radians; scene::IMesh *m_selection_mesh = nullptr; video::SColor m_selection_mesh_color; diff --git a/src/raycast.cpp b/src/raycast.cpp index c84cb01e7..fa6ad9cbd 100644 --- a/src/raycast.cpp +++ b/src/raycast.cpp @@ -124,13 +124,13 @@ bool boxLineCollision(const aabb3f &box, const v3f start, return false; } -bool boxLineCollision(const aabb3f &box, const v3f rotation, - const v3f start, const v3f dir, +bool boxLineCollision(const aabb3f &box, v3f rotation_radians, + v3f start, v3f dir, v3f *collision_point, v3f *collision_normal, v3f *raw_collision_normal) { // Inversely transform the ray rather than rotating the box faces; // this allows us to continue using a simple ray - AABB intersection - core::quaternion rot(rotation * core::DEGTORAD); + core::quaternion rot(rotation_radians); rot.makeInverse(); bool collision = boxLineCollision(box, rot * start, rot * dir, collision_point, collision_normal); diff --git a/src/server/unit_sao.h b/src/server/unit_sao.h index 8cc27c967..6930cd583 100644 --- a/src/server/unit_sao.h +++ b/src/server/unit_sao.h @@ -30,7 +30,7 @@ public: v3f res; // First rotate by m_rotation, then rotate by the automatic rotate yaw (core::quaternion(v3f(0, -m_rotation_add_yaw * core::DEGTORAD, 0)) - * core::quaternion(rot.getRotationDegrees() * core::DEGTORAD)) + * core::quaternion(rot.getRotationRadians())) .toEuler(res); return res * core::RADTODEG; } diff --git a/src/unittest/test_irr_matrix4.cpp b/src/unittest/test_irr_matrix4.cpp index 92938dfed..be6c6aa08 100644 --- a/src/unittest/test_irr_matrix4.cpp +++ b/src/unittest/test_irr_matrix4.cpp @@ -84,20 +84,20 @@ SECTION("getScale") { } } -SECTION("getRotationDegrees") { - auto test_rotation_degrees = [](v3f deg, v3f scale) { +SECTION("getRotationRadians") { + auto test_rotation_degrees = [](v3f rad, v3f scale) { matrix4 S; S.setScale(scale); matrix4 R; - R.setRotationDegrees(deg); - v3f rot = (R * S).getRotationDegrees(); + R.setRotationRadians(rad); + v3f rot = (R * S).getRotationRadians(); matrix4 B; - B.setRotationDegrees(rot); + B.setRotationRadians(rot); CHECK(matrix_equals(R, B)); }; SECTION("returns a rotation equivalent to the original rotation") { - test_rotation_degrees({100, 200, 300}, v3f(1)); - Catch::Generators::RandomFloatingGenerator gen_angle(0, 360, Catch::getSeed()); + test_rotation_degrees({1.0f, 2.0f, 3.0f}, v3f(1)); + Catch::Generators::RandomFloatingGenerator gen_angle(0.0f, 2 * core::PI, Catch::getSeed()); Catch::Generators::RandomFloatingGenerator gen_scale(0.1f, 10, Catch::getSeed()); auto draw = [](auto gen) { f32 f = gen.get(); @@ -109,11 +109,12 @@ SECTION("getRotationDegrees") { }; for (int i = 0; i < 1000; ++i) test_rotation_degrees(draw_v3f(gen_angle), draw_v3f(gen_scale)); - for (f32 i = 0; i < 360; i += 90) - for (f32 j = 0; j < 360; j += 90) - for (f32 k = 0; k < 360; k += 90) { + for (f32 i = 0; i < 4; ++i) + for (f32 j = 0; j < 4; ++j) + for (f32 k = 0; k < 4; ++k) { + v3f rad = core::PI / 4.0f * v3f(i, j, k); for (int l = 0; l < 100; ++l) { - test_rotation_degrees({i, j, k}, draw_v3f(gen_scale)); + test_rotation_degrees(rad, draw_v3f(gen_scale)); } } } diff --git a/src/unittest/test_irr_rotation.cpp b/src/unittest/test_irr_rotation.cpp index 1da0462fe..0aa9c9573 100644 --- a/src/unittest/test_irr_rotation.cpp +++ b/src/unittest/test_irr_rotation.cpp @@ -100,7 +100,7 @@ SECTION("matrix-euler roundtrip") { test_euler_angles_rad([](v3f rad) { matrix4 mat, mat2; mat.setRotationRadians(rad); - v3f rad2 = mat.getRotationDegrees() * core::DEGTORAD; + v3f rad2 = mat.getRotationRadians(); mat2.setRotationRadians(rad2); CHECK(matrix_equals(mat, mat2)); });