Skip to content

Commit

Permalink
Finalized collisions; added render stats to extended status text
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrieleGiuseppini committed Dec 2, 2018
1 parent 8518c12 commit e5d9a1b
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 43 deletions.
19 changes: 8 additions & 11 deletions FloatingSandbox TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,19 @@ Render:
+ 100: OK,but not really "draggy"
+ 10,000: awesome

= Adjust restitution coefficient at collisions
- Try again with normal, but displacement being delta P instead of delta Y

- Recheck L'n'F' for current collision handling
- Recheck L'n'F' for strength

- Extended status: take stats from RenderContext
- RenderContext::Stats:
- LastRenderedShipSprings
- LastRenderedShipTriangles
- LastRenderedGenericTextureTriangles
- LastRenderedConnectedComponents
= Extended status: take stats from RenderContext
+ RenderContext::Stats:
+ LastRenderedShipSprings
+ LastRenderedShipTriangles
+ LastRenderedGenericTextureTriangles
+ LastRenderedConnectedComponents
----
- Reset stats at each RenderContext::RenderStart()
+ Reset stats at each RenderContext::RenderStart()

= Redo full perf analysis
+ Redo full perf analysis

- 1.8.0
- Libsimdpp spring relaxation (*)
Expand Down
3 changes: 2 additions & 1 deletion GameLib/GameController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,5 +578,6 @@ void GameController::PublishStats(std::chrono::steady_clock::time_point nowReal)
mIsPaused,
mRenderContext->GetZoom(),
totalURRatio,
lastURRatio);
lastURRatio,
mRenderContext->GetStatistics());
}
7 changes: 7 additions & 0 deletions GameLib/GameParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ struct GameParameters
template <typename T>
static constexpr T SimulationStepTimeDuration = 0.02f;

template <typename T>
inline T MechanicalSimulationStepTimeDuration() const
{
return SimulationStepTimeDuration<T>
/ NumMechanicalDynamicsIterations<T>();
}


//
// Physical Constants
Expand Down
6 changes: 6 additions & 0 deletions GameLib/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ RenderContext::RenderContext(
, mVectorFieldLengthMultiplier(1.0f)
, mShowStressedSprings(false)
, mWireframeMode(false)
// Statistics
, mRenderStatistics()
{
static constexpr float GenericTextureProgressSteps = 10.0f;
static constexpr float CloudTextureProgressSteps = 4.0f;
Expand Down Expand Up @@ -408,6 +410,7 @@ void RenderContext::AddShip(
*mShaderManager,
mGenericTextureAtlasOpenGLHandle,
*mGenericTextureAtlasMetadata,
mRenderStatistics,
mOrthoMatrix,
mVisibleWorldHeight,
mVisibleWorldWidth,
Expand Down Expand Up @@ -444,6 +447,9 @@ void RenderContext::RenderStart()

// Communicate start to child contextes
mTextRenderContext->RenderStart();

// Reset stats
mRenderStatistics.Reset();
}

void RenderContext::UploadStarsStart(size_t starCount)
Expand Down
16 changes: 15 additions & 1 deletion GameLib/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ class RenderContext
- screenOffset.y / static_cast<float>(mCanvasHeight) * mVisibleWorldHeight);
}

//
// Statistics
//

RenderStatistics const & GetStatistics() const
{
return mRenderStatistics;
}

public:

void Reset();
Expand Down Expand Up @@ -809,7 +818,6 @@ class RenderContext

void RenderEnd();


private:

void RenderCrossesOfLight();
Expand Down Expand Up @@ -1018,6 +1026,12 @@ class RenderContext
float mVectorFieldLengthMultiplier;
bool mShowStressedSprings;
bool mWireframeMode;

//
// Statistics
//

RenderStatistics mRenderStatistics;
};

}
28 changes: 28 additions & 0 deletions GameLib/RenderCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,32 @@ struct TextQuadVertex
};
#pragma pack(pop)


//
// Statistics
//

struct RenderStatistics
{
std::uint64_t LastRenderedShipSprings;
std::uint64_t LastRenderedShipTriangles;
std::uint64_t LastRenderedShipConnectedComponents;
std::uint64_t LastRenderedGenericTextures;
std::uint64_t LastRenderedEphemeralPoints;

RenderStatistics()
{
Reset();
}

void Reset()
{
LastRenderedShipSprings = 0;
LastRenderedShipTriangles = 0;
LastRenderedShipConnectedComponents = 0;
LastRenderedGenericTextures = 0;
LastRenderedEphemeralPoints = 0;
}
};

}
29 changes: 16 additions & 13 deletions GameLib/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ void Ship::UpdatePointForces(GameParameters const & gameParameters)

if (mPoints.GetPosition(pointIndex).y < waterHeightAtThisPoint)
{
// Drag force = -C*V^2*Vn
mPoints.GetForce(pointIndex) +=
mPoints.GetVelocity(pointIndex).square()
* (-waterDragCoefficient);
Expand Down Expand Up @@ -693,9 +694,7 @@ void Ship::UpdateSpringForces(GameParameters const & /*gameParameters*/)

void Ship::IntegrateAndResetPointForces(GameParameters const & gameParameters)
{
float const dt =
GameParameters::SimulationStepTimeDuration<float>
/ gameParameters.NumMechanicalDynamicsIterations<float>();
float const dt = gameParameters.MechanicalSimulationStepTimeDuration<float>();

// Global damp - lowers velocity uniformly, damping oscillations originating between gravity and buoyancy
//
Expand Down Expand Up @@ -746,30 +745,34 @@ void Ship::HandleCollisionsWithSeaFloor(GameParameters const & gameParameters)
{
//
// We handle collisions really simplistically: we move back points to where they were
// at the last update, when they were NOT under the ocean floor.
// at the last update, when they were NOT under the ocean floor, and fully bounce velocity back.
//
// Ideally we would have to find the mid-point - between the position at t-1 and t - at which
// we really entered the sea floor, and then move the point there. We could find the midpoint
// with successive approximations, but this might not work when the floor is really rugged.
// Regarding calculating the post-collision position: ideally we would have to find the
// mid-point - between the position at t-1 and t - at which we really entered the sea floor,
// and then move the point there. We could find the midpoint with successive approximations,
// but this might not work when the floor is really rugged.
//
// Regarding calculating the post-collision velocity: ideally we would mirror velocity around
// the sea floor normal, but if we did this together with moving the point at the previous position,
// that point would start oscillating up and down, as the new position would allow it to gather
// momentum and come crashing down again.
//
// Hence we're gonna stick with this simple algorithm.
//

float const dt =
GameParameters::SimulationStepTimeDuration<float>
/ gameParameters.NumMechanicalDynamicsIterations<float>();
float const dt = gameParameters.MechanicalSimulationStepTimeDuration<float>();

for (auto pointIndex : mPoints)
{
// Check if point is now below the sea floor
float const floorheight = mParentWorld.GetOceanFloorHeightAt(mPoints.GetPosition(pointIndex).x);
if (mPoints.GetPosition(pointIndex).y < floorheight)
{
// Move point back where it was
// Move point back to where it was
mPoints.GetPosition(pointIndex) -= mPoints.GetVelocity(pointIndex) * dt;

// Simulate a perfectly elastic collision
mPoints.GetVelocity(pointIndex) *= -1.0f;
// Bounce velocity (naively)
mPoints.GetVelocity(pointIndex) = -mPoints.GetVelocity(pointIndex);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions GameLib/ShipRenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ShipRenderContext::ShipRenderContext(
ShaderManager<ShaderManagerTraits> & shaderManager,
GameOpenGLTexture & textureAtlasOpenGLHandle,
TextureAtlasMetadata const & textureAtlasMetadata,
RenderStatistics & renderStatistics,
float const(&orthoMatrix)[4][4],
float visibleWorldHeight,
float visibleWorldWidth,
Expand All @@ -30,6 +31,7 @@ ShipRenderContext::ShipRenderContext(
bool showStressedSprings,
bool wireframeMode)
: mShaderManager(shaderManager)
, mRenderStatistics(renderStatistics)
// Parameters - all set at the end of the constructor
, mCanvasToVisibleWorldHeightRatio(0)
, mAmbientLightIntensity(0.0f)
Expand Down Expand Up @@ -802,6 +804,9 @@ void ShipRenderContext::RenderEnd()
}
}

// Update stats
mRenderStatistics.LastRenderedShipConnectedComponents += mConnectedComponents.size();


//
// Render ephemeral points
Expand Down Expand Up @@ -867,6 +872,9 @@ void ShipRenderContext::RenderSpringElements(

// Draw
glDrawElements(GL_LINES, static_cast<GLsizei>(2 * connectedComponent.springElementCount), GL_UNSIGNED_INT, 0);

// Update stats
mRenderStatistics.LastRenderedShipSprings += connectedComponent.springElementCount;
}

void ShipRenderContext::RenderRopeElements(ConnectedComponentData const & connectedComponent)
Expand Down Expand Up @@ -916,6 +924,9 @@ void ShipRenderContext::RenderTriangleElements(

// Draw
glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(3 * connectedComponent.triangleElementCount), GL_UNSIGNED_INT, 0);

// Update stats
mRenderStatistics.LastRenderedShipTriangles += connectedComponent.triangleElementCount;
}

void ShipRenderContext::RenderStressedSpringElements(ConnectedComponentData const & connectedComponent)
Expand Down Expand Up @@ -977,6 +988,9 @@ void ShipRenderContext::RenderGenericTextures(GenericTextureConnectedComponentDa

// Draw polygons
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(connectedComponent.VertexBuffer.size()));

// Update stats
mRenderStatistics.LastRenderedGenericTextures += connectedComponent.VertexBuffer.size() / 6;
}
}

Expand All @@ -996,6 +1010,9 @@ void ShipRenderContext::RenderEphemeralPoints()

// Draw
glDrawElements(GL_POINTS, static_cast<GLsizei>(mEphemeralPoints.size()), GL_UNSIGNED_INT, 0);

// Update stats
mRenderStatistics.LastRenderedEphemeralPoints += mEphemeralPoints.size();
}
}

Expand Down
Loading

0 comments on commit e5d9a1b

Please sign in to comment.