Skip to content

Commit

Permalink
Fluid system part 1 (Revolutionary-Games#783)
Browse files Browse the repository at this point in the history
* creating a fluid system

* improving velocity fields

and reoving debug code

* I forgot that bit of code

* Adding a todo (quality commit i know)

* Adding fluid forces to the system

* Adding components to iron and cells

And making sure the rigid body actually exists

* adding the component to the cell chunks

* updating to the new master branch

* cleaning the code a bit

* Commenting out the physics pushing

* Why was there a tab here!?

* clangformat plz

* adding component_types.h to CMakeLists.txt

* doing untrustedlife's suggestions

* Currents are now sensible. increased mass of many chunks in biomes.as
  • Loading branch information
crodnu authored and Untrustedlife committed May 1, 2019
1 parent 51cf034 commit f65c3d6
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 86 deletions.
28 changes: 14 additions & 14 deletions scripts/SimulationParameters/MicrobeStage/Biomes.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -162,7 +162,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -197,7 +197,7 @@
"dissolves": true,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 2,
"ventAmount": 3.0,
"damages": 0.0,
Expand Down Expand Up @@ -318,7 +318,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -445,7 +445,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -498,7 +498,7 @@
"dissolves": true,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 2,
"ventAmount": 3.0,
"damages": 0.0,
Expand Down Expand Up @@ -601,7 +601,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -654,7 +654,7 @@
"dissolves": true,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 2,
"ventAmount": 3.0,
"damages": 0.0,
Expand Down Expand Up @@ -757,7 +757,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -884,7 +884,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -1011,7 +1011,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -1169,7 +1169,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -1297,7 +1297,7 @@
"dissolves": false,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 10000,
"ventAmount": 3.0,
"damages": 10.0,
Expand Down Expand Up @@ -1350,7 +1350,7 @@
"dissolves": true,
"radius": 1,
"chunkScale": 1.0,
"mass": 1,
"mass": 7,
"size": 2,
"ventAmount": 3.0,
"damages": 0.0,
Expand Down
3 changes: 3 additions & 0 deletions scripts/microbe_stage/biome.as
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ ObjectID createChunk(CellStageWorld@ world, uint chunkId, Float3 pos)
// Need to set the tint
model.GraphicalObject.setCustomParameter(1, Ogre::Vector4(1, 1, 1, 1));

// Fluid mechanics.
world.Create_FluidEffectComponent(chunkEntity);

// Rigid Body
auto rigidBody = world.Create_Physics(chunkEntity, position);

Expand Down
4 changes: 4 additions & 0 deletions scripts/microbe_stage/microbe_operations.as
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,9 @@ ObjectID _createMicrobeEntity(CellStageWorld@ world, bool aiControlled,

auto compoundAbsorberComponent = world.Create_CompoundAbsorberComponent(entity);

// Uncomment this when we get pretty fluid graphics.
//world.Create_FluidEffectComponent(entity);

if (species.isBacteria) {
compoundAbsorberComponent.setGrabScale(0.5f);
}
Expand Down Expand Up @@ -1253,6 +1256,7 @@ void kill(CellStageWorld@ world, ObjectID microbeEntity)
double amount = double(microbeComponent.totalHexCountCache)/CORPSE_CHUNK_AMOUNT_DIVISER;
// Chunk(should separate into own function)
ObjectID chunkEntity = world.CreateEntity();
world.Create_FluidEffectComponent(chunkEntity);
auto positionAdded = Float3(GetEngine().GetRandom().GetFloat(-2.0f, 2.0f),0,
GetEngine().GetRandom().GetFloat(-2.0f, 2.0f));
auto chunkPosition = world.Create_Position(chunkEntity, position._Position+positionAdded,
Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Add source files to the list
set(GROUP_ENGINE
"engine/component_types.h"
"engine/typedefs.h"
"engine/player_data.cpp"
"engine/player_data.h"
Expand Down Expand Up @@ -44,7 +45,9 @@ set(GROUP_MICROBE_STAGE
"microbe_stage/compounds.cpp"
"microbe_stage/compounds.h"
"microbe_stage/generate_cell_stage_world.rb"
"microbe_stage/generate_microbe_editor_world.rb"
"microbe_stage/generate_microbe_editor_world.rb"
"microbe_stage/fluid_system.cpp"
"microbe_stage/fluid_system.h"
"microbe_stage/membrane_system.cpp"
"microbe_stage/membrane_system.h"
"microbe_stage/microbe_camera_system.cpp"
Expand Down
1 change: 1 addition & 0 deletions src/engine/component_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ enum class THRIVE_COMPONENT : uint16_t {
PROPERTIES,
COMPOUND_VENTER,
ENGULFABLE,
FLUID_EFFECT,
DAMAGETOUCH,
// TODO: check is this needed for anything
// INVALID
Expand Down
77 changes: 25 additions & 52 deletions src/microbe_stage/compound_cloud_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,13 @@ void
////////////////////////////////////////////////////////////////////////////////
// CompoundCloudSystem
////////////////////////////////////////////////////////////////////////////////
CompoundCloudSystem::CompoundCloudSystem() :
m_xVelocity(CLOUD_SIMULATION_WIDTH,
std::vector<float>(CLOUD_SIMULATION_HEIGHT, 0)),
m_yVelocity(CLOUD_SIMULATION_WIDTH,
std::vector<float>(CLOUD_SIMULATION_HEIGHT, 0))
{}

CompoundCloudSystem::~CompoundCloudSystem() {}

void
CompoundCloudSystem::Init(CellStageWorld& world)
{
// Use the curl of a Perlin noise field to create a turbulent velocity
// field.
createVelocityField();
// createVelocityField();

// Skip if no graphics
if(!Ogre::Root::getSingletonPtr())
Expand Down Expand Up @@ -678,7 +670,7 @@ void
"it didn't initialize");
}

processCloud(*value.second, renderTime);
processCloud(*value.second, renderTime, world.GetFluidSystem());
}
}

Expand Down Expand Up @@ -1077,33 +1069,39 @@ void
// ------------------------------------ //
void
CompoundCloudSystem::processCloud(CompoundCloudComponent& cloud,
int renderTime)
int renderTime,
FluidSystem& fluidSystem)
{
// Try to slow things down (doesn't seem to work great)
renderTime /= 10;
Float2 pos(cloud.m_position.X, cloud.m_position.Z);

// The diffusion rate seems to have a bigger effect

// Compound clouds move from area of high concentration to area of low.
if(cloud.m_compoundId1 != NULL_COMPOUND) {
diffuse(0.007f, cloud.m_oldDens1, cloud.m_density1, renderTime);
// Move the compound clouds about the velocity field.
advect(cloud.m_oldDens1, cloud.m_density1, renderTime);
advect(
cloud.m_oldDens1, cloud.m_density1, renderTime, fluidSystem, pos);
}
if(cloud.m_compoundId2 != NULL_COMPOUND) {
diffuse(0.007f, cloud.m_oldDens2, cloud.m_density2, renderTime);
// Move the compound clouds about the velocity field.
advect(cloud.m_oldDens2, cloud.m_density2, renderTime);
advect(
cloud.m_oldDens2, cloud.m_density2, renderTime, fluidSystem, pos);
}
if(cloud.m_compoundId3 != NULL_COMPOUND) {
diffuse(0.007f, cloud.m_oldDens3, cloud.m_density3, renderTime);
// Move the compound clouds about the velocity field.
advect(cloud.m_oldDens3, cloud.m_density3, renderTime);
advect(
cloud.m_oldDens3, cloud.m_density3, renderTime, fluidSystem, pos);
}
if(cloud.m_compoundId4 != NULL_COMPOUND) {
diffuse(0.007f, cloud.m_oldDens4, cloud.m_density4, renderTime);
// Move the compound clouds about the velocity field.
advect(cloud.m_oldDens4, cloud.m_density4, renderTime);
advect(
cloud.m_oldDens4, cloud.m_density4, renderTime, fluidSystem, pos);
}

// No graphics check
Expand Down Expand Up @@ -1189,40 +1187,6 @@ void
}
}


void
CompoundCloudSystem::createVelocityField()
{
const float nxScale = m_noiseScale;
// "float(CLOUD_SIMULATION_WIDTH) / float(CLOUD_SIMULATION_HEIGHT)" is the
// aspect ratio of the cloud. This is 1 if the cloud is a square.
const float nyScale = nxScale * (float(CLOUD_SIMULATION_WIDTH) /
float(CLOUD_SIMULATION_HEIGHT));

for(int x = 0; x < CLOUD_SIMULATION_WIDTH; x++) {
for(int y = 0; y < CLOUD_SIMULATION_HEIGHT; y++) {
const float x0 =
(float(x - 1) / float(CLOUD_SIMULATION_WIDTH)) * nxScale;
const float y0 =
(float(y - 1) / float(CLOUD_SIMULATION_HEIGHT)) * nyScale;
const float x1 =
(float(x + 1) / float(CLOUD_SIMULATION_WIDTH)) * nxScale;
const float y1 =
(float(y + 1) / float(CLOUD_SIMULATION_HEIGHT)) * nyScale;

float n0 = m_fieldPotential.noise(x0, y0, 0);
float n1 = m_fieldPotential.noise(x1, y0, 0);
const float ny = n0 - n1;
n0 = m_fieldPotential.noise(x0, y0, 0);
n1 = m_fieldPotential.noise(x0, y1, 0);
const float nx = n1 - n0;

m_xVelocity[x][y] = nx / 2;
m_yVelocity[x][y] = ny / 2;
}
}
}

void
CompoundCloudSystem::diffuse(float diffRate,
std::vector<std::vector<float>>& oldDens,
Expand All @@ -1243,7 +1207,9 @@ void
void
CompoundCloudSystem::advect(const std::vector<std::vector<float>>& oldDens,
std::vector<std::vector<float>>& density,
int dt)
int dt,
FluidSystem& fluidSystem,
Float2 pos)
{
for(int x = 0; x < CLOUD_SIMULATION_WIDTH; x++) {
for(int y = 0; y < CLOUD_SIMULATION_HEIGHT; y++) {
Expand All @@ -1256,8 +1222,15 @@ void
for(size_t x = 1; x < CLOUD_SIMULATION_WIDTH - 1; x++) {
for(size_t y = 1; y < CLOUD_SIMULATION_HEIGHT - 1; y++) {
if(oldDens[x][y] > 1) {
float dx = x + dt * m_xVelocity[x][y];
float dy = y + dt * m_yVelocity[x][y];
constexpr float viscosity =
0.0525f; // TODO: give each cloud a viscosity value in the
// JSON file and use it instead.
Float2 velocity = fluidSystem.getVelocityAt(
pos + Float2(x, y) * CLOUD_RESOLUTION) *
viscosity;

float dx = x + dt * velocity.X;
float dy = y + dt * velocity.Y;

dx = std::clamp(dx, 0.5f, CLOUD_SIMULATION_WIDTH - 1.5f);
dy = std::clamp(dy, 0.5f, CLOUD_SIMULATION_HEIGHT - 1.5f);
Expand Down
Loading

0 comments on commit f65c3d6

Please sign in to comment.