Lua: change obj.rotation from Euler angles to Quaternion

This commit is contained in:
Petr Mikheev 2023-06-10 16:02:32 +02:00
parent 1d5b73f20a
commit 3b43cc2aea
14 changed files with 175 additions and 30 deletions

View file

@ -2,7 +2,10 @@
#define MISC_MATHUTIL_H
#include <osg/Math>
#include <osg/Matrixf>
#include <osg/Quat>
#include <osg/Vec2f>
#include <osg/Vec3f>
namespace Misc
{
@ -22,6 +25,44 @@ namespace Misc
return osg::Vec2f(vec.x() * c + vec.y() * -s, vec.x() * s + vec.y() * c);
}
inline osg::Vec3f toEulerAnglesXZ(osg::Vec3f forward)
{
float x = -asin(forward.z());
float z = atan2(forward.x(), forward.y());
return osg::Vec3f(x, 0, z);
}
inline osg::Vec3f toEulerAnglesXZ(osg::Quat quat)
{
return toEulerAnglesXZ(quat * osg::Vec3f(0, 1, 0));
}
inline osg::Vec3f toEulerAnglesXZ(osg::Matrixf m)
{
osg::Vec3f forward(m(1, 0), m(1, 1), m(1, 2));
forward.normalize();
return toEulerAnglesXZ(forward);
}
inline osg::Vec3f toEulerAnglesZYX(osg::Vec3f forward, osg::Vec3f up)
{
float y = -asin(up.x());
float x = atan2(up.y(), up.z());
osg::Vec3f forwardZ = (osg::Quat(x, osg::Vec3f(1, 0, 0)) * osg::Quat(y, osg::Vec3f(0, 1, 0))) * forward;
float z = atan2(forwardZ.x(), forwardZ.y());
return osg::Vec3f(x, y, z);
}
inline osg::Vec3f toEulerAnglesZYX(osg::Quat quat)
{
return toEulerAnglesZYX(quat * osg::Vec3f(0, 1, 0), quat * osg::Vec3f(0, 0, 1));
}
inline osg::Vec3f toEulerAnglesZYX(osg::Matrixf m)
{
osg::Vec3f forward(m(1, 0), m(1, 1), m(1, 2));
osg::Vec3f up(m(2, 0), m(2, 1), m(2, 2));
forward.normalize();
up.normalize();
return toEulerAnglesZYX(forward, up);
}
inline bool isPowerOfTwo(int x)
{
return ((x > 0) && ((x & (x - 1)) == 0));