Skip to content

Commit b875952

Browse files
committed
Predicting the path of a projectile with a twice-integrated acceleration.
1 parent a701354 commit b875952

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

game/game.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ void CGame::Load()
4848
m_iNormalTexture = GetRenderer()->LoadTextureIntoGL("normal.png");
4949

5050
GraphReset();
51+
52+
m_projectile_initial_time = 0;
53+
m_projectile_initial_position = Vector(2, 1, 2);
54+
m_projectile_initial_velocity = Vector(-1, 3, -1) * 5;
55+
m_projectile_gravity = Vector(0, -5, 0);
56+
57+
// Fire the first one
58+
m_projectile_position = m_projectile_initial_position;
59+
m_projectile_velocity = m_projectile_initial_velocity;
5160
}
5261

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

331340
pCharacter->SetTranslation(pCharacter->GetGlobalOrigin() + pCharacter->m_vecVelocity * dt);
332341
}
342+
343+
if (Game()->GetTime() >= m_projectile_initial_time + 6)
344+
{
345+
m_projectile_position = m_projectile_initial_position;
346+
m_projectile_velocity = m_projectile_initial_velocity = Vector((float)(rand()%1000)/250-2, 2.5, (float)(rand()%1000)/250-2) * 5;
347+
m_projectile_initial_time = Game()->GetTime();
348+
}
349+
350+
// Simulate the projectile
351+
m_projectile_position = m_projectile_position + m_projectile_velocity * dt;
352+
m_projectile_velocity = m_projectile_velocity + m_projectile_gravity * dt;
353+
}
354+
355+
Vector PredictProjectileAtTime(float t, Vector v0, Vector x0, Vector g)
356+
{
357+
return g * (0.5f * t * t) + v0 * t + x0;
333358
}
334359

335360
void CGame::Draw()
@@ -494,6 +519,24 @@ void CGame::Draw()
494519

495520
GraphDraw();
496521

522+
r.SetUniform("vecColor", Color(0, 0, 0, 255));
523+
r.RenderBox(m_projectile_position - Vector(1, 1, 1)*0.4f, m_projectile_position + Vector(1, 1, 1)*0.4f);
524+
525+
r.SetUniform("vecColor", Vector4D(1, 0, 0, 1));
526+
for (int i = 0; i < 100; i++)
527+
{
528+
float time_0 = (float)i * 0.2f;
529+
float time_1 = (float)(i+1) * 0.2f;
530+
531+
Vector x_0 = PredictProjectileAtTime(time_0, m_projectile_initial_velocity, m_projectile_initial_position, m_projectile_gravity);
532+
Vector x_1 = PredictProjectileAtTime(time_1, m_projectile_initial_velocity, m_projectile_initial_position, m_projectile_gravity);
533+
534+
r.BeginRenderLines();
535+
r.Vertex(x_0);
536+
r.Vertex(x_1);
537+
r.EndRender();
538+
}
539+
497540
pRenderer->FinishRendering(&r);
498541

499542
// Call this last. Your rendered stuff won't appear on the screen until you call this.

game/game.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ class CGame : public CApplication
117117
size_t m_iMeshVB;
118118
size_t m_iMeshSize;
119119

120+
float m_projectile_initial_time;
121+
Vector m_projectile_position;
122+
Vector m_projectile_velocity;
123+
Vector m_projectile_gravity;
124+
125+
Vector m_projectile_initial_position;
126+
Vector m_projectile_initial_velocity;
127+
120128
public:
121129
typedef enum
122130
{

0 commit comments

Comments
 (0)