Initial
This commit is contained in:
parent
591cbd03bb
commit
d886a1c2f2
5 changed files with 158 additions and 0 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
8
CMakeLists.txt
Normal file
8
CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
cmake_minimum_required(VERSION 3.30)
|
||||||
|
project(VoxelSim)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
|
add_executable(VoxelSim main.cpp
|
||||||
|
simulation.cpp
|
||||||
|
simulation.h)
|
54
main.cpp
Normal file
54
main.cpp
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstring>
|
||||||
|
#include "simulation.h"
|
||||||
|
|
||||||
|
void printGrid(simulation::SimulationGrid* grid)
|
||||||
|
{
|
||||||
|
std::cout << std::string(grid->x_max+2, '_') << std::endl;
|
||||||
|
for (unsigned char y = grid->y_max; y > 0; y--)
|
||||||
|
{
|
||||||
|
std::cout << "|";
|
||||||
|
for (unsigned char x = 0; x < grid->x_max; x++)
|
||||||
|
{
|
||||||
|
unsigned int offset = simulation::getCellOffset(x, y-1, 0, grid);
|
||||||
|
unsigned int material = grid->cells[offset];
|
||||||
|
switch (material)
|
||||||
|
{
|
||||||
|
case simulation::VACUUM:
|
||||||
|
std::cout << " ";
|
||||||
|
break;
|
||||||
|
case simulation::WATER:
|
||||||
|
std::cout << "~";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "|" << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << std::string(grid->x_max+2, '_') << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
unsigned char xMax = 50;
|
||||||
|
unsigned char yMax = 20;
|
||||||
|
unsigned char zMax = 1;
|
||||||
|
size_t bufferSizeInBytes = sizeof(unsigned int) * xMax * yMax * zMax;
|
||||||
|
auto inCells = static_cast<unsigned int*>(std::malloc(bufferSizeInBytes));
|
||||||
|
std::memset(inCells, 0, bufferSizeInBytes);
|
||||||
|
auto outCells = static_cast<unsigned int*>(std::malloc(bufferSizeInBytes));
|
||||||
|
simulation::SimulationGrid inGrid = {
|
||||||
|
inCells, xMax, yMax, zMax
|
||||||
|
};
|
||||||
|
simulation::SimulationGrid outGrid = {
|
||||||
|
outCells, xMax, yMax, zMax
|
||||||
|
};
|
||||||
|
|
||||||
|
step(0, &inGrid, &outGrid);
|
||||||
|
|
||||||
|
printGrid(&outGrid);
|
||||||
|
std::free(inCells);
|
||||||
|
std::free(outCells);
|
||||||
|
return 0;
|
||||||
|
}
|
51
simulation.cpp
Normal file
51
simulation.cpp
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
#include "simulation.h"
|
||||||
|
|
||||||
|
namespace simulation
|
||||||
|
{
|
||||||
|
unsigned int getCellOffset(
|
||||||
|
unsigned char x,
|
||||||
|
unsigned char y,
|
||||||
|
unsigned char z,
|
||||||
|
SimulationGrid* grid)
|
||||||
|
{
|
||||||
|
if (x < grid->x_max && y < grid->y_max && z < grid->z_max)
|
||||||
|
{
|
||||||
|
unsigned int offset = x + y * grid->x_max + z * (grid->y_max * grid->x_max);
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void step(
|
||||||
|
double millisecondsSinceLastStep,
|
||||||
|
SimulationGrid* input,
|
||||||
|
SimulationGrid* output)
|
||||||
|
{
|
||||||
|
for (unsigned char x = 0; x < input->x_max; x++)
|
||||||
|
{
|
||||||
|
for (unsigned char y = 0; y < input->y_max; y++)
|
||||||
|
{
|
||||||
|
for (unsigned char z = 0; z < input->z_max; z++)
|
||||||
|
{
|
||||||
|
unsigned int offset = getCellOffset(x, y, z, input);
|
||||||
|
unsigned int material = input->cells[offset];
|
||||||
|
switch (material)
|
||||||
|
{
|
||||||
|
case WATER:
|
||||||
|
if (y > 0)
|
||||||
|
{
|
||||||
|
unsigned int downOffset = getCellOffset(x, y - 1, z, input);
|
||||||
|
// Flow down if cell is empty
|
||||||
|
if (output->cells[downOffset] == 0)
|
||||||
|
{
|
||||||
|
output->cells[downOffset] = material;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
output->cells[offset] = material;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
simulation.h
Normal file
37
simulation.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// Created by johan on 2025-01-25.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef SIMULATION_H
|
||||||
|
#define SIMULATION_H
|
||||||
|
namespace simulation
|
||||||
|
{
|
||||||
|
enum Fluid
|
||||||
|
{
|
||||||
|
VACUUM = 0,
|
||||||
|
WATER,
|
||||||
|
MERCURY
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SimulationGrid
|
||||||
|
{
|
||||||
|
unsigned int* cells;
|
||||||
|
unsigned char x_max;
|
||||||
|
unsigned char y_max;
|
||||||
|
unsigned char z_max;
|
||||||
|
};
|
||||||
|
|
||||||
|
void step(
|
||||||
|
double millisecondsSinceLastStep,
|
||||||
|
SimulationGrid* input,
|
||||||
|
SimulationGrid* output);
|
||||||
|
|
||||||
|
unsigned int getCellOffset(
|
||||||
|
unsigned char x,
|
||||||
|
unsigned char y,
|
||||||
|
unsigned char z,
|
||||||
|
SimulationGrid* grid);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //SIMULATION_H
|
Loading…
Add table
Add a link
Reference in a new issue