Add acceleration to movement code

This commit is contained in:
Doyle Thai 2016-06-17 18:11:23 +10:00
parent c54e6323a7
commit bcb847c18c
7 changed files with 80 additions and 16 deletions

View File

@ -140,6 +140,7 @@
<ClInclude Include="src\include\Dengine\Common.h" />
<ClInclude Include="src\include\Breakout\Game.h" />
<ClInclude Include="src\include\Dengine\Entity.h" />
<ClInclude Include="src\include\Dengine\Math.h" />
<ClInclude Include="src\include\Dengine\OpenGL.h" />
<ClInclude Include="src\include\Dengine\Renderer.h" />
<ClInclude Include="src\include\Dengine\Shader.h" />

View File

@ -74,5 +74,8 @@
<ClInclude Include="src\include\Dengine\Entity.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\Dengine\Math.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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());

View File

@ -64,27 +64,60 @@ void Game::init()
void Game::update(const f32 dt)
{
const f32 heroSpeed = static_cast<f32>((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<f32>((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()

View File

@ -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;
};

View File

@ -0,0 +1,19 @@
#ifndef DENGINE_MATH_H
#define DENGINE_MATH_H
#include <Dengine/Common.h>
namespace Dengine
{
class Math
{
public:
static inline f32 squared(const f32 a)
{
f32 result = a * a;
return result;
}
};
}
#endif

View File

@ -1,17 +1,18 @@
#ifndef WORLDTRAVELLER_GAME_H
#define WORLDTRAVELLER_GAME_H
#include <Dengine/OpenGL.h>
#include <Dengine/AssetManager.h>
#include <Dengine/Common.h>
#include <Dengine/Entity.h>
#include <Dengine/Math.h>
#include <Dengine/OpenGL.h>
#include <Dengine/Renderer.h>
#include <Dengine/Shader.h>
#include <Dengine/AssetManager.h>
#include <Dengine/Entity.h>
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
{