Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quaternions #1796

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rts/System/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ make_global_var(sources_engine_System_common
"${CMAKE_CURRENT_SOURCE_DIR}/Math/SpringDampers.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Math/NURBS.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Matrix44f.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Quaternion.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Misc/RectangleOverlapHandler.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Misc/SpringTime.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Object.cpp"
Expand Down
28 changes: 27 additions & 1 deletion rts/System/Matrix44f.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "System/Matrix44f.h"
#include "System/Quaternion.h"
#include "System/SpringMath.h"
#ifndef UNIT_TEST
#include "Rendering/GlobalRendering.h"
Expand Down Expand Up @@ -70,6 +71,22 @@ bool CMatrix44f::IsIdentity() const
return (*this) == IDENTITY;
}

float CMatrix44f::Det3() const
{
// triple product == D
return col[0].dot(col[1].cross(col[2]));
}

float CMatrix44f::Det4() const
{
return
md[0][3] * md[1][2] * md[2][1] * md[3][0] - md[0][2] * md[1][3] * md[2][1] * md[3][0] - md[0][3] * md[1][1] * md[2][2] * md[3][0] + md[0][1] * md[1][3] * md[2][2] * md[3][0] +
md[0][2] * md[1][1] * md[2][3] * md[3][0] - md[0][1] * md[1][2] * md[2][3] * md[3][0] - md[0][3] * md[1][2] * md[2][0] * md[3][1] + md[0][2] * md[1][3] * md[2][0] * md[3][1] +
md[0][3] * md[1][0] * md[2][2] * md[3][1] - md[0][0] * md[1][3] * md[2][2] * md[3][1] - md[0][2] * md[1][0] * md[2][3] * md[3][1] + md[0][0] * md[1][2] * md[2][3] * md[3][1] +
md[0][3] * md[1][1] * md[2][0] * md[3][2] - md[0][1] * md[1][3] * md[2][0] * md[3][2] - md[0][3] * md[1][0] * md[2][1] * md[3][2] + md[0][0] * md[1][3] * md[2][1] * md[3][2] +
md[0][1] * md[1][0] * md[2][3] * md[3][2] - md[0][0] * md[1][1] * md[2][3] * md[3][2] - md[0][2] * md[1][1] * md[2][0] * md[3][3] + md[0][1] * md[1][2] * md[2][0] * md[3][3] +
md[0][2] * md[1][0] * md[2][1] * md[3][3] - md[0][0] * md[1][2] * md[2][1] * md[3][3] - md[0][1] * md[1][0] * md[2][2] * md[3][3] + md[0][0] * md[1][1] * md[2][2] * md[3][3];
}

CMatrix44f& CMatrix44f::RotateX(float angle)
{
Expand Down Expand Up @@ -272,7 +289,7 @@ CMatrix44f& CMatrix44f::RotateEulerZYX(const float3 angles)
// [ 0 0 sz 0]
// [ 0 0 0 1]
//
CMatrix44f& CMatrix44f::Scale(const float3 scales)
CMatrix44f& CMatrix44f::Scale(const float3& scales)
{
m[ 0] *= scales.x;
m[ 1] *= scales.x;
Expand All @@ -291,6 +308,15 @@ CMatrix44f& CMatrix44f::Scale(const float3 scales)
return *this;
}

CMatrix44f& CMatrix44f::FromTQS(const float3& pos, const CQuaternion& quat, const float3& scale)
{
*this = quat.ToRotMatrix();
this->Scale(scale);
this->Translate(pos);

return *this;
}

CMatrix44f& CMatrix44f::Translate(const float x, const float y, const float z)
{
m[12] += (x*m[0] + y*m[4] + z*m[ 8]);
Expand Down
10 changes: 8 additions & 2 deletions rts/System/Matrix44f.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "System/float3.h"
#include "System/float4.h"

class CQuaternion;
class CMatrix44f
{
public:
Expand All @@ -30,6 +31,10 @@ class CMatrix44f

bool IsOrthoNormal() const;
bool IsIdentity() const;
bool IsRotMatrix() const { return IsOrthoNormal() && math::fabs(1.0f - Det4()) <= float3::cmp_eps(); }
bool IsRotOrRotTranMatrix() const { return IsOrthoNormal() && math::fabs(1.0f - Det3()) <= float3::cmp_eps(); }
float Det3() const;
float Det4() const;

CMatrix44f& LoadIdentity() { return (*this = CMatrix44f()); }

Expand All @@ -43,9 +48,10 @@ class CMatrix44f
CMatrix44f& RotateEulerZXY(const float3 angles); // executes Rotate{Z,X,Y}
CMatrix44f& RotateEulerZYX(const float3 angles); // executes Rotate{Z,Y,X}
CMatrix44f& Translate(const float x, const float y, const float z);
CMatrix44f& Translate(const float3 pos) { return Translate(pos.x, pos.y, pos.z); }
CMatrix44f& Scale(const float3 scales);
CMatrix44f& Translate(const float3& pos) { return Translate(pos.x, pos.y, pos.z); }
CMatrix44f& Scale(const float3& scales);
CMatrix44f& Scale(float scaleX, float scaleY, float scaleZ) { return Scale(float3{ scaleX, scaleY, scaleZ }); }
CMatrix44f& FromTQS(const float3& pos, const CQuaternion& quat, const float3& scale);

void SetPos(const float3 pos) { m[12] = pos.x; m[13] = pos.y; m[14] = pos.z; }
void SetX (const float3 dir) { m[ 0] = dir.x; m[ 1] = dir.y; m[ 2] = dir.z; }
Expand Down
Loading