Skip to content

Commit

Permalink
Work on the water physics (4/X)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Oct 30, 2023
1 parent e5ddc1a commit f368612
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 57 deletions.
21 changes: 19 additions & 2 deletions Apps/CellSim/CellSimApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ namespace CS
}
}

void CellSimApp::HandleCellKill()
{
_cellManager->DeleteCell
(
glm::u32vec3(Engine::CellSimParams::selectedCellCoords[0],
Engine::CellSimParams::selectedCellCoords[1],
Engine::CellSimParams::selectedCellCoords[2])
);
}

// ----- Public -----

CellSimApp::CellSimApp()
Expand Down Expand Up @@ -148,10 +158,17 @@ namespace CS
Engine::PROFILE_SCOPE("Manage cells");

//Check for cell spawn
if(Engine::CellSimParams::spawnNewCell)
if(Engine::CellSimParams::spawnCell)
{
HandleCellSpawn();
Engine::CellSimParams::spawnNewCell = false;
Engine::CellSimParams::spawnCell = false;
}

//Check for cell kill
if(Engine::CellSimParams::killCell)
{
HandleCellKill();
Engine::CellSimParams::killCell = false;
}

//Check for cell delete
Expand Down
1 change: 1 addition & 0 deletions Apps/CellSim/CellSimApp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace CS
void AddObjects();
void AddCellWorld();
void HandleCellSpawn();
void HandleCellKill();

public:
CellSimApp();
Expand Down
15 changes: 11 additions & 4 deletions Apps/CellSim/CellSimInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ namespace CS
if(ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None))
{
// --- Cell spawn menu
if(ImGui::BeginTabItem("Spawn"))
if(ImGui::BeginTabItem("Creation"))
{
// --- Cell selection menu
std::string cellTypeString = std::string("Selected cell type: ") + Engine::CellTypeStrings[Engine::CellSimParams::selectedCellType];
std::string cellTypeString = std::string("Cell type: ") + Engine::CellTypeStrings[Engine::CellSimParams::selectedCellType];
if(ImGui::BeginMenu(cellTypeString.c_str()))
{
if(ImGui::MenuItem("Water"))
Expand Down Expand Up @@ -187,13 +187,20 @@ namespace CS
}

// --- Spawn button
ImGui::Text("\t\t\t\t\t");
ImGui::Text("\t\t\t");
ImGui::SameLine();
if(ImGui::Button("Spawn"))
{
Engine::CellSimParams::spawnNewCell = true;
Engine::CellSimParams::spawnCell = true;
}
// --- Kill button
ImGui::SameLine();
ImGui::Text("\t\t\t");
ImGui::SameLine();
if(ImGui::Button("Kill"))
{
Engine::CellSimParams::killCell = true;
}

ImGui::EndTabItem();
}
Expand Down
4 changes: 3 additions & 1 deletion Engine/Application/GlobalParams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ namespace Engine
{
inline static constexpr uint32 CELL_FRAME_SIZE = 61;
inline static constexpr uint32 MAX_CELL_AMOUNT = CELL_FRAME_SIZE * CELL_FRAME_SIZE * CELL_FRAME_SIZE;
inline static constexpr uint32 MAX_RECURSION_DEPTH = CELL_FRAME_SIZE;
inline static CellType selectedCellType = Water;
inline static uint32 selectedCellCoords[3] = {0, 0, 0};
inline static uint32 selectedCellAmount = 0;
inline static uint32 cellsAlive = 0;
inline static bool spawnNewCell = false;
inline static bool spawnCell = false;
inline static bool killCell = false;
inline static bool createSpawner = false;
inline static bool deleteCells = false;
inline static bool deleteSpawners = false;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderables/CellStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Engine

uint32 CellStorage::GetIndexFrom3DPos(const glm::u32vec3& cellPos)
{
return (cellPos.x * CellSimParams::CELL_FRAME_SIZE * CellSimParams::CELL_FRAME_SIZE )
return (cellPos.x * CellSimParams::CELL_FRAME_SIZE * CellSimParams::CELL_FRAME_SIZE)
+ (cellPos.y * CellSimParams::CELL_FRAME_SIZE ) + cellPos.z;
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderables/CellStorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Engine
uint8 spreadFactor;
CellType type;
uint32 id = UINT32_MAX;
bool movedDownLastTurn = false;
bool movedLastTurn = false;
};

struct CellParams
Expand Down
120 changes: 75 additions & 45 deletions Engine/Rendering/Renderer/CellManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Engine
_cellStorage.Set(_cellStorage.Get(cellPos), targetCellPos);

//Modify movement state
_cellStorage.GetModifiable(targetCellPos).movedDownLastTurn = true;
_cellStorage.GetModifiable(targetCellPos).movedLastTurn = true;

//Delete old cell
_cellStorage.Set({CellTypeSpreadFactor[CellType::Air], CellType::Air}, cellPos);
Expand All @@ -27,28 +27,56 @@ namespace Engine
_cellIndexStorage.at(index) = CellStorage::GetIndexFrom3DPos(targetCellPos);
}

void CellManager::DeleteCell(const uint32 index, const glm::u32vec3& cellPos)
bool CellManager::FindFreeCellRecursive(const glm::u32vec3& cellPos, glm::u32vec3* freeCell, uint32* recursionDepth)
{
//ToDo: Fix
//ToDo: Implement specific kill button in the UI
std::array<glm::u32vec3, 8> posToCheck = {};
uint32 posOffset = 1;
*recursionDepth++;

/*_cellStorage.Set({CellTypeSpreadFactor[CellType::Air], CellType::Air}, cellPos);
if(*recursionDepth >= CellSimParams::MAX_RECURSION_DEPTH)
{
return false;
}

//Update the corresponding gpu buffers
_cellRenderer->UpdateGPUStorage
(
_cellStorage.Get(cellPos).id,
cellPos,
CellTypeColor[_cellStorage.Get(cellPos).type]
);
while((cellPos.x + posOffset < CellSimParams::CELL_FRAME_SIZE) &&
(cellPos.x - posOffset != UINT32_MAX) &&
(cellPos.z + posOffset < CellSimParams::CELL_FRAME_SIZE) &&
(cellPos.z - posOffset != UINT32_MAX))
{
posToCheck =
{
glm::u32vec3{cellPos.x+posOffset, cellPos.y, cellPos.z-posOffset},
glm::u32vec3{cellPos.x+posOffset, cellPos.y, cellPos.z},
glm::u32vec3{cellPos.x+posOffset, cellPos.y, cellPos.z+posOffset},
glm::u32vec3{cellPos.x, cellPos.y, cellPos.z-posOffset},
glm::u32vec3{cellPos.x, cellPos.y, cellPos.z+posOffset},
glm::u32vec3{cellPos.x-posOffset, cellPos.y, cellPos.z-posOffset},
glm::u32vec3{cellPos.x-posOffset, cellPos.y, cellPos.z},
glm::u32vec3{cellPos.x-posOffset, cellPos.y, cellPos.z+posOffset}
};

Random::Shuffle(posToCheck.begin(), posToCheck.end());

for(auto pos : posToCheck)
{
if(_cellStorage.Get(pos).type == CellType::Air)
{
*freeCell = pos;
return true;
}
else if(FindFreeCellRecursive(pos, freeCell, recursionDepth))
{
return true;
}
}

//Update index
_cellIndexStorage.at(index) = CellStorage::GetIndexFrom3DPos(cellPos);
posOffset++;
}

CellSimParams::cellsAlive--;*/
return false;
}

void CellManager::Fill8Neighbours(const glm::u32vec3& cellPos, std::vector<glm::u32vec3>* occupiedPos)
void CellManager::Fill8Neighbours(const glm::u32vec3& cellPos)
{
// ############################################ //
// (x+1, y, z-1) | (x+1, y, z) | (x+1, y, z+1) //
Expand Down Expand Up @@ -80,7 +108,7 @@ namespace Engine
}
else
{
occupiedPos->push_back(pos);
_occupiedCellsThisTurn.push_back(pos);
}
}
}
Expand Down Expand Up @@ -119,9 +147,7 @@ namespace Engine
{
//Get coordinates from cell below (y-1)
glm::u32vec3 cellPosBelow = glm::u32vec3(cellPos.x, cellPos.y-1, cellPos.z);

glm::u32vec3 cellNeighboursToFill = cellPos;
bool deleteCell = false;
static uint32 recursionDepth = 0;

//Check for type of cell below
if(_cellStorage.Get(cellPosBelow).type == CellType::Air)
Expand All @@ -131,60 +157,57 @@ namespace Engine
}
else if(_cellStorage.Get(cellPosBelow).type == CellType::Water)
{
//If it's water, enable cell displacement for the cell below
cellNeighboursToFill = cellPosBelow;
glm::u32vec3 freeCell;
//ToDo: Find iterative solution
if(FindFreeCellRecursive(cellPosBelow, &freeCell, &recursionDepth))
{
MoveCell(index, cellPos, freeCell);
}
else
{
Logger::Warn("Failed", "Finding", "Free cell at: " + CellStorage::Get3DPosAsString(cellPos));
}

deleteCell = true;
return;
}

//Vector that gets filled with all positions that were already full
std::vector<glm::u32vec3> occupiedBeforehand;
occupiedBeforehand.reserve(8);
//ToDo: Refactor vector as class variable

//Check if spread factor allows for further distribution
if(_cellStorage.Get(cellPos).spreadFactor > 0)
if(_cellStorage.Get(cellPos).spreadFactor > 0 && _cellStorage.Get(cellPos).movedLastTurn)
{
Fill8Neighbours(cellNeighboursToFill, &occupiedBeforehand);
Fill8Neighbours(cellPos);
_cellStorage.GetModifiable(cellPos).spreadFactor = 0;
}

//Check the amount of cells that couldn't be filled up
if(occupiedBeforehand.size() == 8)
/*if(_occupiedCellsThisTurn.size() == 8)
{
//Every position was full / nothing was filled up
//ToDo: Find out what to do now ...
return;
}
//Handle the non-fillable cells
else if(!occupiedBeforehand.empty())
else if(!_occupiedCellsThisTurn.empty())
{
for(auto pos : occupiedBeforehand)
for(auto pos : _occupiedCellsThisTurn)
{
if(_cellStorage.Get(pos).type == CellType::Water)
{
MoveCellIntoDirection(index, pos, cellNeighboursToFill);
MoveCellIntoDirection(index, pos, cellPos);
}
}
//ToDo: Clear vector
}
_occupiedCellsThisTurn.clear();
}*/

//Clean up
_cellStorage.GetModifiable(cellPos).movedDownLastTurn = false;

if(deleteCell)
{
//Delete cell and increment excessive cell counter
DeleteCell(index, cellPos);
_excessiveCells++;
}
_cellStorage.GetModifiable(cellPos).movedLastTurn = false;
}

// ----- Public -----

CellManager::CellManager()
{
_cellSpawnerStorage.reserve(5);
_occupiedCellsThisTurn.reserve(8);
}

void CellManager::AddCellRenderer(const std::string& shader, const glm::vec3& worldSpawnPos)
Expand Down Expand Up @@ -239,6 +262,12 @@ namespace Engine
_cellSpawnerStorage.push_back(cellParams);
}

void CellManager::DeleteCell(const glm::u32vec3& cellPos)
{
_cellStorage.Set({CellTypeSpreadFactor[CellType::Air], CellType::Air}, cellPos);
CellSimParams::cellsAlive--;
}

void CellManager::DeleteCells()
{
_cellStorage.Init();
Expand Down Expand Up @@ -330,9 +359,10 @@ namespace Engine
+ FileManager::PadString(std::to_string(cell.id), 5) + " | "
+ FileManager::PadString(std::to_string(cell.type), 5) + " | "
+ FileManager::PadString(std::to_string(cell.spreadFactor), 6) + " | "
+ FileManager::PadString(std::to_string(cell.movedDownLastTurn), 6) + " | ");
+ FileManager::PadString(std::to_string(cell.movedLastTurn), 6) + " | ");
}

Logger::Print("Cells alive: " + std::to_string(CellSimParams::cellsAlive));
Logger::Print("Excessive cell counter: " + std::to_string(_excessiveCells));
}
}
8 changes: 5 additions & 3 deletions Engine/Rendering/Renderer/CellManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ namespace Engine
CellStorage _cellStorage;
std::array<uint32, CellSimParams::MAX_CELL_AMOUNT> _cellIndexStorage = {0};
std::vector<CellParams> _cellSpawnerStorage;
std::vector<glm::u32vec3> _occupiedCellsThisTurn;

void MoveCell(uint32 index, const glm::u32vec3& cellPos, const glm::u32vec3& targetCellPos);
void DeleteCell(uint32 index, const glm::u32vec3& cellPos);
void Fill8Neighbours(const glm::u32vec3& cellPos, std::vector<glm::u32vec3>* occupiedPos);
void MoveCellIntoDirection(const uint32 index, const glm::u32vec3& cellPos, const glm::u32vec3& originCellPos);
bool FindFreeCellRecursive(const glm::u32vec3& cellPos, glm::u32vec3* freeCell, uint32* recursionDepth);
void Fill8Neighbours(const glm::u32vec3& cellPos);
void MoveCellIntoDirection(uint32 index, const glm::u32vec3& cellPos, const glm::u32vec3& originCellPos);
void HandleWaterCell(uint32 index, const glm::u32vec3& cellPos);

public:
Expand All @@ -27,6 +28,7 @@ namespace Engine
void AddCell(const CellParams& cellParams);
void AddCellWithoutRender(const CellParams& cellParams);
void AddCellSpawner(const CellParams& cellParams);
void DeleteCell(const glm::u32vec3& cellPos);
void DeleteCells();
void DeleteSpawners();
void CalculateCellPhysics();
Expand Down

0 comments on commit f368612

Please sign in to comment.