Skip to content

Commit

Permalink
Predicting the path of a projectile with a twice-integrated accelerat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
BSVino committed Aug 16, 2014
1 parent a701354 commit b875952
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
43 changes: 43 additions & 0 deletions game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ void CGame::Load()
m_iNormalTexture = GetRenderer()->LoadTextureIntoGL("normal.png");

GraphReset();

m_projectile_initial_time = 0;
m_projectile_initial_position = Vector(2, 1, 2);
m_projectile_initial_velocity = Vector(-1, 3, -1) * 5;
m_projectile_gravity = Vector(0, -5, 0);

// Fire the first one
m_projectile_position = m_projectile_initial_position;
m_projectile_velocity = m_projectile_initial_velocity;
}

void CGame::MakePuff(const Point& p)
Expand Down Expand Up @@ -330,6 +339,22 @@ void CGame::Update(float dt)

pCharacter->SetTranslation(pCharacter->GetGlobalOrigin() + pCharacter->m_vecVelocity * dt);
}

if (Game()->GetTime() >= m_projectile_initial_time + 6)
{
m_projectile_position = m_projectile_initial_position;
m_projectile_velocity = m_projectile_initial_velocity = Vector((float)(rand()%1000)/250-2, 2.5, (float)(rand()%1000)/250-2) * 5;
m_projectile_initial_time = Game()->GetTime();
}

// Simulate the projectile
m_projectile_position = m_projectile_position + m_projectile_velocity * dt;
m_projectile_velocity = m_projectile_velocity + m_projectile_gravity * dt;
}

Vector PredictProjectileAtTime(float t, Vector v0, Vector x0, Vector g)
{
return g * (0.5f * t * t) + v0 * t + x0;
}

void CGame::Draw()
Expand Down Expand Up @@ -494,6 +519,24 @@ void CGame::Draw()

GraphDraw();

r.SetUniform("vecColor", Color(0, 0, 0, 255));
r.RenderBox(m_projectile_position - Vector(1, 1, 1)*0.4f, m_projectile_position + Vector(1, 1, 1)*0.4f);

r.SetUniform("vecColor", Vector4D(1, 0, 0, 1));
for (int i = 0; i < 100; i++)
{
float time_0 = (float)i * 0.2f;
float time_1 = (float)(i+1) * 0.2f;

Vector x_0 = PredictProjectileAtTime(time_0, m_projectile_initial_velocity, m_projectile_initial_position, m_projectile_gravity);
Vector x_1 = PredictProjectileAtTime(time_1, m_projectile_initial_velocity, m_projectile_initial_position, m_projectile_gravity);

r.BeginRenderLines();
r.Vertex(x_0);
r.Vertex(x_1);
r.EndRender();
}

pRenderer->FinishRendering(&r);

// Call this last. Your rendered stuff won't appear on the screen until you call this.
Expand Down
8 changes: 8 additions & 0 deletions game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ class CGame : public CApplication
size_t m_iMeshVB;
size_t m_iMeshSize;

float m_projectile_initial_time;
Vector m_projectile_position;
Vector m_projectile_velocity;
Vector m_projectile_gravity;

Vector m_projectile_initial_position;
Vector m_projectile_initial_velocity;

public:
typedef enum
{
Expand Down

0 comments on commit b875952

Please sign in to comment.