diff --git a/Dengine.vcxproj b/Dengine.vcxproj index ed270ba..0f42669 100644 --- a/Dengine.vcxproj +++ b/Dengine.vcxproj @@ -140,6 +140,7 @@ + diff --git a/Dengine.vcxproj.filters b/Dengine.vcxproj.filters index 09f3b1d..4012670 100644 --- a/Dengine.vcxproj.filters +++ b/Dengine.vcxproj.filters @@ -74,5 +74,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/Entity.cpp b/src/Entity.cpp index 2fec2a0..521d72f 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -6,6 +6,7 @@ namespace Dengine Entity::Entity() : pos(glm::vec2(0, 0)) +, dPos(glm::vec2(0, 0)) , size(glm::vec2(0, 0)) , tex(nullptr) { @@ -14,6 +15,7 @@ Entity::Entity() Entity::Entity(const glm::vec2 pos, const Texture *tex) { this->pos = pos; + this->dPos = glm::vec2(0, 0); this->tex = tex; this->size = glm::vec2(tex->getWidth(), tex->getHeight()); } @@ -21,6 +23,7 @@ Entity::Entity(const glm::vec2 pos, const Texture *tex) Entity::Entity(const glm::vec2 pos, glm::vec2 size, const Texture *tex) { this->pos = pos; + this->dPos = glm::vec2(0, 0); this->tex = tex; this->size = size; } @@ -29,6 +32,7 @@ Entity::Entity(const glm::vec2 pos, const std::string texName) { Texture *tex = AssetManager::getTexture(texName); this->pos = pos; + this->dPos = glm::vec2(0, 0); this->tex = tex; this->size = glm::vec2(tex->getWidth(), tex->getHeight()); diff --git a/src/WorldTraveller.cpp b/src/WorldTraveller.cpp index 08a3be4..16e099c 100644 --- a/src/WorldTraveller.cpp +++ b/src/WorldTraveller.cpp @@ -64,27 +64,60 @@ void Game::init() void Game::update(const f32 dt) { - const f32 heroSpeed = static_cast((3.0f * METERS_TO_PIXEL) * dt); + /* + Equations of Motion + f(t) = position m + f'(t) = velocity m/s + f"(t) = acceleration m/s^2 + + The user supplies an acceleration, a, and by integrating + f"(t) = a, where a is a constant, acceleration + f'(t) = a*t + v, where v is a constant, old velocity + f (t) = (a/2)*t^2 + v*t + p, where p is a constant, old position + */ + + glm::vec2 ddPos = glm::vec2(0, 0); if (this->keys[GLFW_KEY_SPACE]) { -#if 0 - Dengine::Entity hitMarker = Dengine::Entity(glm::vec2(0, 0), "hitMarker"); - glm::vec2 hitMarkerP = - glm::vec2((hero.pos.x * 1.5f) + hitMarker.tex->getWidth(), hero.pos.y * 1.5f); - hitMarker.pos = hitMarkerP; - renderer->drawEntity(&hitMarker); -#endif } if (this->keys[GLFW_KEY_RIGHT]) { - hero.pos.x += heroSpeed; + ddPos.x = 1.0f; } - else if (this->keys[GLFW_KEY_LEFT]) + if (this->keys[GLFW_KEY_LEFT]) { - hero.pos.x -= heroSpeed; + ddPos.x = -1.0f; } + + if (this->keys[GLFW_KEY_UP]) + { + ddPos.y = 1.0f; + } + if (this->keys[GLFW_KEY_DOWN]) + { + ddPos.y = -1.0f; + } + + if (ddPos.x != 0.0f && ddPos.y != 0.0f) + { + // NOTE(doyle): Cheese it and pre-compute the vector for diagonal using + // pythagoras theorem on a unit triangle + // 1^2 + 1^2 = c^2 + ddPos *= 0.70710678118f; + } + + const f32 heroSpeed = static_cast((22.0f * METERS_TO_PIXEL)); // m/s^2 + ddPos *= heroSpeed; + + // TODO(doyle): Counteracting force on player's acceleration is arbitrary + ddPos += -(5.5f * hero.dPos); + + glm::vec2 newHeroP = (0.5f * ddPos) * Dengine::Math::squared(dt) + (hero.dPos * dt) + hero.pos; + + hero.dPos = (ddPos * dt) + hero.dPos; + hero.pos = newHeroP; } void Game::render() diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index 7183b50..c60b884 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -17,7 +17,10 @@ public: Entity(const glm::vec2 pos, const std::string texName); ~Entity(); - glm::vec2 pos; + + glm::vec2 pos; // Position + glm::vec2 dPos; // Velocity + glm::vec2 size; const Texture *tex; }; diff --git a/src/include/Dengine/Math.h b/src/include/Dengine/Math.h new file mode 100644 index 0000000..1cd2ce6 --- /dev/null +++ b/src/include/Dengine/Math.h @@ -0,0 +1,19 @@ +#ifndef DENGINE_MATH_H +#define DENGINE_MATH_H + +#include + +namespace Dengine +{ +class Math +{ +public: + static inline f32 squared(const f32 a) + { + f32 result = a * a; + return result; + } +}; +} +#endif + diff --git a/src/include/WorldTraveller/WorldTraveller.h b/src/include/WorldTraveller/WorldTraveller.h index f722305..ea356e5 100644 --- a/src/include/WorldTraveller/WorldTraveller.h +++ b/src/include/WorldTraveller/WorldTraveller.h @@ -1,17 +1,18 @@ #ifndef WORLDTRAVELLER_GAME_H #define WORLDTRAVELLER_GAME_H -#include +#include #include +#include +#include +#include #include #include -#include -#include namespace WorldTraveller { GLOBAL_VAR const i32 NUM_KEYS = 1024; -GLOBAL_VAR const i32 METERS_TO_PIXEL = 100; +GLOBAL_VAR const f32 METERS_TO_PIXEL = 100; enum Cardinal {