diff --git a/HandmadeMath.h b/HandmadeMath.h index 7e4d973..e0f967e 100644 --- a/HandmadeMath.h +++ b/HandmadeMath.h @@ -2043,6 +2043,11 @@ HMM_INLINE hmm_mat4 &operator*=(hmm_mat4 &Left, float Right) return (Left = Left * Right); } +HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, hmm_quaternion Right) +{ + return (Left = Left * Right); +} + HMM_INLINE hmm_quaternion &operator*=(hmm_quaternion &Left, float Right) { return (Left = Left * Right); diff --git a/example/src/Cube.h b/example/src/Cube.h index 668b8ab..47229b2 100644 --- a/example/src/Cube.h +++ b/example/src/Cube.h @@ -106,9 +106,11 @@ public: renderComponent = c; } - void Tick(float deltaTime) override { - x += 0.1f; - position.X = HMM_SINF(x); + void Tick(float deltaSeconds) override { + x += deltaSeconds; + position.X = 2.0f * HMM_SINF(x); + + rotation *= HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), deltaSeconds * HMM_ToRadians(45.0f)); } }; diff --git a/example/src/Entity.h b/example/src/Entity.h index 76690a3..c917473 100644 --- a/example/src/Entity.h +++ b/example/src/Entity.h @@ -17,7 +17,7 @@ public: children.push_back(e); } - virtual void Tick(float deltaTime) {} + virtual void Tick(float deltaSeconds) {} struct RenderComponent { GLuint vaoID; diff --git a/example/src/main.cpp b/example/src/main.cpp index 3205d91..fd8120d 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -10,9 +11,11 @@ #include "Entity.h" #include "Cube.h" -void TickTree(Entity *e); +void TickTree(Entity *e, float deltaSeconds); void ComputeModelMatrices(Entity *ep, hmm_mat4 parentModelMatrix); +using std::chrono::high_resolution_clock; + int main() { // Initialise GLFW @@ -66,8 +69,15 @@ int main() Cube child = Cube(); child.position = HMM_Vec3(2.1f, 0.0f, 0.0f); + Cube c = Cube(); + child.position = HMM_Vec3(2.1f, 0.0f, 0.0f); + + child.AddChild(&c); root.AddChild(&child); + bool hasTicked = false; + high_resolution_clock::time_point lastTickTime; + do { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -75,7 +85,15 @@ int main() hmm_mat4 view = HMM_LookAt(HMM_Vec3(3.0f, 3.0f, 4.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f)); hmm_mat4 vp = projection * view; - TickTree(&root); + auto now = high_resolution_clock::now(); + if (hasTicked) { + auto elapsedNanoseconds = std::chrono::duration_cast(now - lastTickTime).count(); + float elapsedSeconds = elapsedNanoseconds / 1000000000.0f; + TickTree(&root, elapsedSeconds); + } + lastTickTime = now; + hasTicked = true; + ComputeModelMatrices(&root, HMM_Mat4d(1.0f)); auto it = EntityIterator(&root); @@ -140,11 +158,11 @@ int main() ); } -void TickTree(Entity *e) { - e->Tick(0); +void TickTree(Entity *e, float deltaSeconds) { + e->Tick(deltaSeconds); for (auto child : e->children) { - TickTree(child); + TickTree(child, deltaSeconds); } }