Skip to content

Commit

Permalink
Euler integration, fixed and variable
Browse files Browse the repository at this point in the history
  • Loading branch information
BSVino committed Jul 3, 2016
1 parent 9ee0bd3 commit a3a56da
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 15 deletions.
172 changes: 157 additions & 15 deletions game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A

#if defined(__APPLE__)
#include <OpenGL/glu.h>
#include <unistd.h>
#else
#include <GL/glu.h>
#endif
Expand Down Expand Up @@ -114,24 +115,19 @@ void CGame::Load()

float total_length = g_spline.GetTotalLength();








for (int k = 0; k < VArraySize(g_spline_segments); k++)
g_spline_segments[k] = g_spline.ConstVelocitySplineAtTime(total_length*k/VArraySize(g_spline_segments)/g_spline_speed, g_spline_speed);

m_particle_variable_position = Vector(1, 2, 0);
m_particle_fixed_position = Vector(1, 2, 0);

m_satellite_euler_variable_position = Vector(2, 2, 1);
m_satellite_euler_variable_velocity = Vector(2, 0, 0);

m_satellite_euler_position = Vector(2, 2, 1);
m_satellite_euler_velocity = Vector(2, 0, 0);






m_particle_paths.resize(2);
}

void CGame::MakePuff(const Point& p)
Expand Down Expand Up @@ -285,6 +281,9 @@ bool CGame::MouseInput(int iButton, tinker_mouse_state_t iState)
return true;
}

if (iButton == TINKER_KEY_MOUSE_RIGHT && iState == TINKER_MOUSE_PRESSED)
sleep(1);

return false;
}

Expand Down Expand Up @@ -342,6 +341,18 @@ bool CGame::TraceLine(const Vector& v0, const Vector& v1, Vector& vecIntersectio
return false;
}

static Vector VelocityField(Vector position, float time)
{
Vector to_origin = Vector(0, 2, 0)-position;
float return_to_origin_strength = to_origin.Length() / 10;
float return_to_origin = return_to_origin_strength*return_to_origin_strength;

float x_warp = sin(position.x + time);
float z_warp = cos(position.z + time);

return to_origin.Normalized() * return_to_origin + x_warp * Vector(1, 0, 0) + z_warp * Vector(0, 0, 1);
}

// In this Update() function we need to update all of our characters. Move them around or whatever we want to do.
// http://www.youtube.com/watch?v=c4b9lCfSDQM
void CGame::Update(float dt)
Expand Down Expand Up @@ -470,6 +481,56 @@ void CGame::Update(float dt)

while (g_seaweed_simulation_time < Game()->GetTime())
SimulateSeaweed();







// Velocity field
{
m_particle_variable_position = m_particle_variable_position + VelocityField(m_particle_variable_position, Game()->GetTime()) * dt;
m_particle_paths[0].push_back(m_particle_variable_position);

float h = 1.0f/60.0f;
for (; m_particle_time < Game()->GetTime(); m_particle_time += h)
{
m_particle_fixed_position = m_particle_fixed_position + VelocityField(m_particle_fixed_position, m_particle_time) * h;
m_particle_paths[1].push_back(m_particle_fixed_position);
}
}








// Satellites
{
Vector center_of_gravity(0, 2, 0);
float g = 10;
float m = 1;
float h = 1.0f/60.0f;

auto get_gravity_acceleration = [center_of_gravity, g](Vector position)
{
Vector to_center = (center_of_gravity - position);
float r = to_center.Length();
return to_center * (g / (r*r*r));
};

m_satellite_euler_variable_position = m_satellite_euler_variable_position + m_satellite_euler_variable_velocity * dt;
m_satellite_euler_variable_velocity = m_satellite_euler_variable_velocity + get_gravity_acceleration(m_satellite_euler_variable_position) * dt;

for (; m_satellite_time < Game()->GetTime(); m_satellite_time += h)
{
m_satellite_euler_position = m_satellite_euler_position + m_satellite_euler_velocity * h;
m_satellite_euler_velocity = m_satellite_euler_velocity + get_gravity_acceleration(m_satellite_euler_position) * h;
}
}
}

void CGame::Draw()
Expand All @@ -483,7 +544,7 @@ void CGame::Draw()
CRenderer* pRenderer = GetRenderer();

// Tell the renderer how to set up the camera.
pRenderer->SetCameraPosition(m_hPlayer->GetGlobalOrigin() - vecForward * 6 + vecUp * 3 - vecRight * 0.5f);
pRenderer->SetCameraPosition(m_hPlayer->GetGlobalOrigin() - vecForward * 3 + vecUp * 3 - vecRight * 0.5f);
pRenderer->SetCameraDirection(vecForward);
pRenderer->SetCameraUp(Vector(0, 1, 0));
pRenderer->SetCameraFOV(90);
Expand Down Expand Up @@ -637,6 +698,7 @@ void CGame::Draw()

RenderSeaweed();

if (false)
{
CRenderingContext c(Game()->GetRenderer(), true);

Expand Down Expand Up @@ -691,6 +753,86 @@ void CGame::Draw()
c.RenderBox(Vector(-0.1f, -0.1f, -0.1f), Vector(0.1f, 0.1f, 0.1f));
}

// Velocity field & Satellites
{
CRenderingContext c(Game()->GetRenderer(), true);

Vector camera = Game()->m_hPlayer->GetGlobalView();

c.UseProgram("model");

c.SetUniform("vecColor", Vector4D(0, 0, 0, 1));
c.BeginRenderLines();

for (int x = -10; x < 10; x++)
{
for (int y = -10; y < 10; y++)
{
Vector position = Vector(x, 2, y);
c.Vertex(position);
c.Vertex(position + VelocityField(position, Game()->GetTime()));
}
}

c.EndRender();

for (int x = -10; x < 10; x++)
{
for (int y = -10; y < 10; y++)
{
Vector position = Vector(x, 2, y);
Matrix4x4 m;
m.SetTranslation(position);
c.LoadTransform(m);
c.RenderBox(Vector(-0.02f, -0.02f, -0.02f), Vector(0.02f, 0.02f, 0.02f));
}
}

Vector4D path_colors[] = {
Vector4D(1, 0, 0, 1),
Vector4D(0, 1, 0, 1),
};

auto draw_particle = [&c, path_colors, this](Vector position, Vector velocity, int& path)
{
c.ResetTransformations();

c.SetUniform("vecColor", path_colors[path] * 0.5f);
c.BeginRenderLines();
for (int k = 0; k < m_particle_paths[path].size()-1; k++)
{
c.Vertex(m_particle_paths[path][k]);
c.Vertex(m_particle_paths[path][k+1]);
}
c.EndRender();

c.SetUniform("vecColor", path_colors[path]);

c.BeginRenderLines();
c.Vertex(position);
c.Vertex(position + velocity);
c.EndRender();

Matrix4x4 m;
m.SetTranslation(position);
c.LoadTransform(m);
c.RenderBox(Vector(-0.1f, -0.1f, -0.1f), Vector(0.1f, 0.1f, 0.1f));

path++;
};

int path = 0;

draw_particle(m_particle_variable_position, VelocityField(m_particle_variable_position, Game()->GetTime()), path);
draw_particle(m_particle_fixed_position, VelocityField(m_particle_fixed_position, Game()->GetTime()), path);

#if 0
draw_particle(m_satellite_euler_variable_position, m_satellite_euler_variable_velocity, Vector4D(1, 0, 0, 1));

draw_particle(m_satellite_euler_position, m_satellite_euler_velocity, Vector4D(0, 1, 0, 1));
#endif
}

pRenderer->FinishRendering(&r);

// Call this last. Your rendered stuff won't appear on the screen until you call this.
Expand Down Expand Up @@ -925,8 +1067,8 @@ void CGame::GameLoop()
float dt = flCurrentTime - flPreviousTime;

// Keep dt from growing too large.
if (dt > 0.15f)
dt = 0.15f;
//if (dt > 0.15f)
// dt = 0.15f;

Update(dt);

Expand Down
14 changes: 14 additions & 0 deletions game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ class CGame : public CApplication
size_t m_iMeshVB;
size_t m_iMeshSize;

float m_particle_time = 0;

vector<vector<Vector>> m_particle_paths;

Vector m_particle_variable_position;
Vector m_particle_fixed_position;

Vector m_satellite_euler_variable_position;
Vector m_satellite_euler_variable_velocity;

float m_satellite_time = 0;
Vector m_satellite_euler_position;
Vector m_satellite_euler_velocity;

#define MAX_PROJECTILES 8
float m_projectile_initial_time;
float m_projectile_break_time;
Expand Down
12 changes: 12 additions & 0 deletions math/vector4d.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Vector4D
public:
const Vector4D operator+(const Vector4D& v) const;
const Vector4D operator-(const Vector4D& v) const;
const Vector4D operator*(float s) const;

bool operator==(const Vector4D& v) const
{
Expand Down Expand Up @@ -94,3 +95,14 @@ inline const Vector4D Vector4D::operator-(const Vector4D& v) const
{
return Vector4D(x-v.x, y-v.y, z-v.z, w-v.w);
}

inline const Vector4D Vector4D::operator*(float s) const
{
return Vector4D(s*x, s*y, s*z, s*w);
}

inline const Vector4D operator*(float s, const Vector4D& v)
{
return Vector4D(s*v.x, s*v.y, s*v.z, s*v.w);
}

0 comments on commit a3a56da

Please sign in to comment.