From 4f2eaf1adb11255a98178e751a9ae8257c0ed13b Mon Sep 17 00:00:00 2001 From: H0zen Date: Sun, 18 Dec 2016 16:32:36 +0200 Subject: [PATCH] Remove rotation matrices from GameObjectModel - the quaternions are more numerically stable and faster than matrices --- src/game/ChatCommands/Level2.cpp | 2 +- src/game/vmap/GameObjectModel.cpp | 14 +++++--------- src/game/vmap/GameObjectModel.h | 13 ++++--------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/game/ChatCommands/Level2.cpp b/src/game/ChatCommands/Level2.cpp index abf5f745..3f5dc498 100644 --- a/src/game/ChatCommands/Level2.cpp +++ b/src/game/ChatCommands/Level2.cpp @@ -58,7 +58,7 @@ #include #include #include "Formulas.h" -#include "G3D/Quat.h" // for turning GO's +#include "G3D/Quat.h" // for turning GO's #include "TargetedMovementGenerator.h" // for HandleNpcUnFollowCommand #include "MoveMap.h" // for mmap manager #include "PathFinder.h" // for mmap commands diff --git a/src/game/vmap/GameObjectModel.cpp b/src/game/vmap/GameObjectModel.cpp index 00757a1c..78d1f76a 100644 --- a/src/game/vmap/GameObjectModel.cpp +++ b/src/game/vmap/GameObjectModel.cpp @@ -32,7 +32,6 @@ #include "GameObjectModel.h" #include "DBCStores.h" #include "Creature.h" -#include "G3D/Quat.h" struct GameobjectModelData { @@ -136,9 +135,7 @@ bool GameObjectModel::initialize(const GameObject* const pGo, const GameObjectDi void GameObjectModel::UpdateRotation(G3D::Quat const& q) { - q.toRotationMatrix(iRot); - - iInvRot = iRot.inverse(); + iQuat = q; G3D::AABox mdl_box(iModelBound); @@ -148,7 +145,7 @@ void GameObjectModel::UpdateRotation(G3D::Quat const& q) G3D::AABox rotated_bounds; for (int i = 0; i < 8; ++i) - { rotated_bounds.merge(iRot * mdl_box.corner(i)); } + { rotated_bounds.merge((iQuat * G3D::Quat(mdl_box.corner(i)) * iQuat.conj()).imag()); } iBound = rotated_bounds + iPos; } @@ -179,8 +176,8 @@ bool GameObjectModel::IntersectRay(const G3D::Ray& ray, float& MaxDist, bool Sto { return false; } // child bounds are defined in object space: - Vector3 p = iInvRot * (ray.origin() - iPos) * iInvScale; - Ray modRay(p, iInvRot * ray.direction()); + Vector3 p = (iQuat.conj() * G3D::Quat((ray.origin() - iPos) * iInvScale) * iQuat).imag(); + Ray modRay(p, (iQuat.conj() * G3D::Quat(ray.direction()) * iQuat).imag()); float distance = MaxDist * iInvScale; bool hit = iModel->IntersectRay(modRay, distance, StopAtFirstHit); if (hit) @@ -195,7 +192,7 @@ bool GameObjectModel::GetIntersectPoint(const G3D::Vector3& srcPoint, G3D::Vecto { G3D::Vector3 p; if (absolute) - p = iInvRot * (srcPoint - iPos) * iInvScale; + p = (iQuat * G3D::Quat((srcPoint - iPos) * iInvScale) * iQuat.conj()).imag(); else p = srcPoint; @@ -218,4 +215,3 @@ void GameObjectModel::GetWorldCoords(const G3D::Vector3& localCoords, G3D::Vecto { } - diff --git a/src/game/vmap/GameObjectModel.h b/src/game/vmap/GameObjectModel.h index 492cbc85..738e90b8 100644 --- a/src/game/vmap/GameObjectModel.h +++ b/src/game/vmap/GameObjectModel.h @@ -31,6 +31,7 @@ #include #include #include +#include #include "DBCStructure.h" #include "GameObject.h" @@ -40,11 +41,6 @@ namespace VMAP class WorldModel; } -namespace G3D -{ - class Quat; -} - /** * @brief * @@ -52,17 +48,16 @@ namespace G3D class GameObjectModel { private: - bool isCollidable; + bool isCollidable; std::string iName; G3D::AABox iBound; G3D::AABox iModelBound; G3D::Vector3 iPos; - G3D::Matrix3 iRot; - float iScale; + G3D::Quat iQuat; //Note: this must be a unit quaternion!!! + float iScale; float iInvScale; - G3D::Matrix3 iInvRot; VMAP::WorldModel* iModel; GameObject const* iOwner;