1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Move Luanti rotation matrix helpers to own header

This commit is contained in:
Lars Mueller 2025-05-10 20:57:13 +02:00
parent 9b2ee1dd5d
commit ef2bcba56a
11 changed files with 65 additions and 69 deletions

View file

@ -16,6 +16,7 @@
#include "client/meshgen/collector.h" #include "client/meshgen/collector.h"
#include "util/basic_macros.h" #include "util/basic_macros.h"
#include "util/numeric.h" #include "util/numeric.h"
#include "util/rotation_matrix.h"
#include "util/serialize.h" #include "util/serialize.h"
#include "camera.h" // CameraModes #include "camera.h" // CameraModes
#include "collision.h" #include "collision.h"
@ -955,7 +956,7 @@ void GenericCAO::updateNodePos()
getPosRotMatrix().setTranslation(pos); getPosRotMatrix().setTranslation(pos);
if (node != m_spritenode) { // rotate if not a sprite if (node != m_spritenode) { // rotate if not a sprite
v3f rot = m_is_local_player ? -m_rotation : -rot_translator.val_current; v3f rot = m_is_local_player ? -m_rotation : -rot_translator.val_current;
setPitchYawRoll(getPosRotMatrix(), rot); setPitchYawRollRad(getPosRotMatrix(), rot * core::RADTODEG);
} }
} }
} }

View file

@ -5,8 +5,8 @@
// Copyright (C) 2015-2018 paramat // Copyright (C) 2015-2018 paramat
#include <stack> #include <stack>
#include <matrix4.h>
#include "treegen.h" #include "treegen.h"
#include "irr_v3d.h"
#include "util/pointer.h" #include "util/pointer.h"
#include "util/numeric.h" #include "util/numeric.h"
#include "servermap.h" #include "servermap.h"

View file

@ -6,9 +6,10 @@
#include "util/numeric.h" #include "util/numeric.h"
#include "log.h" #include "log.h"
#include "gamedef.h" #include "gamedef.h"
#include "porting.h" // strcasecmp #include "porting.h" // strcasecmp for Windows
#include <cassert> #include <cassert>
#include <cstring> // strcasecmp for everything else
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type) ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
{ {

View file

@ -4,6 +4,7 @@
#include "profiler.h" #include "profiler.h"
#include "porting.h" #include "porting.h"
#include <cstring>
static Profiler main_profiler; static Profiler main_profiler;
Profiler *g_profiler = &main_profiler; Profiler *g_profiler = &main_profiler;

View file

@ -8,7 +8,7 @@
#include "object_properties.h" #include "object_properties.h"
#include "serveractiveobject.h" #include "serveractiveobject.h"
#include <quaternion.h> #include <quaternion.h>
#include "util/numeric.h" #include "util/rotation_matrix.h"
class UnitSAO : public ServerActiveObject class UnitSAO : public ServerActiveObject
{ {
@ -26,7 +26,7 @@ public:
const v3f getTotalRotation() const { const v3f getTotalRotation() const {
// This replicates what happens clientside serverside // This replicates what happens clientside serverside
core::matrix4 rot; core::matrix4 rot;
setPitchYawRoll(rot, -m_rotation); setPitchYawRollRad(rot, -m_rotation * core::DEGTORAD);
v3f res; v3f res;
// First rotate by m_rotation, then rotate by the automatic rotate yaw // 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(v3f(0, -m_rotation_add_yaw * core::DEGTORAD, 0))

View file

@ -2,12 +2,14 @@
// SPDX-License-Identifier: LGPL-2.1-or-later // SPDX-License-Identifier: LGPL-2.1-or-later
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> // Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
#include "irrMath.h"
#include "test.h" #include "test.h"
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include "util/enriched_string.h" #include "util/enriched_string.h"
#include "util/numeric.h" #include "util/numeric.h"
#include "util/rotation_matrix.h"
#include "util/string.h" #include "util/string.h"
#include "util/base64.h" #include "util/base64.h"
#include "util/colorize.h" #include "util/colorize.h"
@ -459,11 +461,11 @@ static bool within(const core::matrix4 &m1, const core::matrix4 &m2,
return true; return true;
} }
static bool roundTripsDeg(const v3f &v, const f32 precision) static bool roundTripsRad(const v3f &v, const f32 precision)
{ {
core::matrix4 m; core::matrix4 m;
setPitchYawRoll(m, v); setPitchYawRollRad(m, v);
return within(v, getPitchYawRoll(m), precision); return within(v, getPitchYawRollRad(m), precision);
} }
void TestUtilities::testEulerConversion() void TestUtilities::testEulerConversion()
@ -484,12 +486,8 @@ void TestUtilities::testEulerConversion()
// produce the same results. Check also that the conversion // produce the same results. Check also that the conversion
// works both ways for these values. // works both ways for these values.
v1 = v3f(M_PI/3.0, M_PI/5.0, M_PI/4.0); v1 = v3f(M_PI/3.0, M_PI/5.0, M_PI/4.0);
v2 = v3f(60.0f, 36.0f, 45.0f);
setPitchYawRollRad(m1, v1); setPitchYawRollRad(m1, v1);
setPitchYawRoll(m2, v2);
UASSERT(within(m1, m2, tolL));
UASSERT(within(getPitchYawRollRad(m1), v1, tolL)); UASSERT(within(getPitchYawRollRad(m1), v1, tolL));
UASSERT(within(getPitchYawRoll(m2), v2, tolH));
// Check the rotation matrix produced. // Check the rotation matrix produced.
UASSERT(within(M1[0], 0.932004869f, tolL)); UASSERT(within(M1[0], 0.932004869f, tolL));
@ -535,17 +533,17 @@ void TestUtilities::testEulerConversion()
UASSERT(within(M1[10], M2[0], tolL)); UASSERT(within(M1[10], M2[0], tolL));
// Check that Eulers that produce near gimbal-lock still round-trip // Check that Eulers that produce near gimbal-lock still round-trip
UASSERT(roundTripsDeg(v3f(89.9999f, 17.f, 0.f), tolH)); UASSERT(roundTripsRad(core::DEGTORAD * v3f(89.9999f, 17.f, 0.f), tolH));
UASSERT(roundTripsDeg(v3f(89.9999f, 0.f, 19.f), tolH)); UASSERT(roundTripsRad(core::DEGTORAD * v3f(89.9999f, 0.f, 19.f), tolH));
UASSERT(roundTripsDeg(v3f(89.9999f, 17.f, 19.f), tolH)); UASSERT(roundTripsRad(core::DEGTORAD * v3f(89.9999f, 17.f, 19.f), tolH));
// Check that Eulers at an angle > 90 degrees may not round-trip... // Check that Eulers at an angle > 90 degrees may not round-trip...
v1 = v3f(90.00001f, 1.f, 1.f); v1 = core::DEGTORAD * v3f(90.00001f, 1.f, 1.f);
setPitchYawRoll(m1, v1); setPitchYawRollRad(m1, v1);
v2 = getPitchYawRoll(m1); v2 = getPitchYawRollRad(m1);
//UASSERT(within(v1, v2, tolL)); // this is typically false //UASSERT(within(v1, v2, tolL)); // this is typically false
// ... however the rotation matrix is the same for both // ... however the rotation matrix is the same for both
setPitchYawRoll(m2, v2); setPitchYawRollRad(m2, v2);
UASSERT(within(m1, m2, tolL)); UASSERT(within(m1, m2, tolL));
} }

View file

@ -9,6 +9,7 @@ set(util_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/ieee_float.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ieee_float.cpp
${CMAKE_CURRENT_SOURCE_DIR}/metricsbackend.cpp ${CMAKE_CURRENT_SOURCE_DIR}/metricsbackend.cpp
${CMAKE_CURRENT_SOURCE_DIR}/numeric.cpp ${CMAKE_CURRENT_SOURCE_DIR}/numeric.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rotation_matrix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pointedthing.cpp ${CMAKE_CURRENT_SOURCE_DIR}/pointedthing.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pointabilities.cpp ${CMAKE_CURRENT_SOURCE_DIR}/pointabilities.cpp
${CMAKE_CURRENT_SOURCE_DIR}/quicktune.cpp ${CMAKE_CURRENT_SOURCE_DIR}/quicktune.cpp

View file

@ -157,38 +157,3 @@ s16 adjustDist(s16 dist, float zoom_fov)
{ {
return std::round(adjustDist((float)dist, zoom_fov)); return std::round(adjustDist((float)dist, zoom_fov));
} }
void setPitchYawRollRad(core::matrix4 &m, v3f rot)
{
f64 a1 = rot.Z, a2 = rot.X, a3 = rot.Y;
f64 c1 = cos(a1), s1 = sin(a1);
f64 c2 = cos(a2), s2 = sin(a2);
f64 c3 = cos(a3), s3 = sin(a3);
f32 *M = m.pointer();
M[0] = s1 * s2 * s3 + c1 * c3;
M[1] = s1 * c2;
M[2] = s1 * s2 * c3 - c1 * s3;
M[4] = c1 * s2 * s3 - s1 * c3;
M[5] = c1 * c2;
M[6] = c1 * s2 * c3 + s1 * s3;
M[8] = c2 * s3;
M[9] = -s2;
M[10] = c2 * c3;
}
v3f getPitchYawRollRad(const core::matrix4 &m)
{
const f32 *M = m.pointer();
f64 a1 = atan2(M[1], M[5]);
f32 c2 = std::sqrt((f64)M[10]*M[10] + (f64)M[8]*M[8]);
f32 a2 = atan2f(-M[9], c2);
f64 c1 = cos(a1);
f64 s1 = sin(a1);
f32 a3 = atan2f(s1*M[6] - c1*M[2], c1*M[0] - s1*M[4]);
return v3f(a2, a3, a1);
}

View file

@ -11,7 +11,6 @@
#include "irr_v3d.h" #include "irr_v3d.h"
#include "irr_aabb3d.h" #include "irr_aabb3d.h"
#include "SColor.h" #include "SColor.h"
#include <matrix4.h>
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
@ -478,20 +477,6 @@ inline void wrappedApproachShortest(T &current, const T target, const T stepsize
} }
} }
void setPitchYawRollRad(core::matrix4 &m, v3f rot);
inline void setPitchYawRoll(core::matrix4 &m, v3f rot)
{
setPitchYawRollRad(m, rot * core::DEGTORAD);
}
v3f getPitchYawRollRad(const core::matrix4 &m);
inline v3f getPitchYawRoll(const core::matrix4 &m)
{
return getPitchYawRollRad(m) * core::RADTODEG;
}
// Muliply the RGB value of a color linearly, and clamp to black/white // Muliply the RGB value of a color linearly, and clamp to black/white
inline video::SColor multiplyColorValue(const video::SColor &color, float mod) inline video::SColor multiplyColorValue(const video::SColor &color, float mod)
{ {

View file

@ -0,0 +1,36 @@
#include "rotation_matrix.h"
void setPitchYawRollRad(core::matrix4 &m, v3f rot)
{
f64 a1 = rot.Z, a2 = rot.X, a3 = rot.Y;
f64 c1 = cos(a1), s1 = sin(a1);
f64 c2 = cos(a2), s2 = sin(a2);
f64 c3 = cos(a3), s3 = sin(a3);
f32 *M = m.pointer();
M[0] = s1 * s2 * s3 + c1 * c3;
M[1] = s1 * c2;
M[2] = s1 * s2 * c3 - c1 * s3;
M[4] = c1 * s2 * s3 - s1 * c3;
M[5] = c1 * c2;
M[6] = c1 * s2 * c3 + s1 * s3;
M[8] = c2 * s3;
M[9] = -s2;
M[10] = c2 * c3;
}
v3f getPitchYawRollRad(const core::matrix4 &m)
{
const f32 *M = m.pointer();
f64 a1 = atan2(M[1], M[5]);
f32 c2 = std::sqrt((f64)M[10]*M[10] + (f64)M[8]*M[8]);
f32 a2 = atan2f(-M[9], c2);
f64 c1 = cos(a1);
f64 s1 = sin(a1);
f32 a3 = atan2f(s1*M[6] - c1*M[2], c1*M[0] - s1*M[4]);
return v3f(a2, a3, a1);
}

View file

@ -0,0 +1,8 @@
#include <matrix4.h>
#include "irr_v3d.h"
/// @note This is not consistent with Irrlicht's setRotationRadians
void setPitchYawRollRad(core::matrix4 &m, v3f rot);
/// @note This is not consistent with Irrlicht's getRotationRadians
v3f getPitchYawRollRad(const core::matrix4 &m);