Rudimentary gravity
This commit is contained in:
parent
a2f1e11af8
commit
d6b3be83d5
3 changed files with 56 additions and 32 deletions
28
main.cpp
28
main.cpp
|
@ -2,16 +2,16 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
|
|
||||||
void printGrid(simulation::SimulationGrid grid)
|
void printGrid(simulation::SimulationGrid* grid)
|
||||||
{
|
{
|
||||||
std::cout << std::string(grid.x_max+2, '_') << std::endl;
|
std::cout << std::string(grid->x_max+2, '_') << std::endl;
|
||||||
for (unsigned char y = grid.y_max; y > 0; y--)
|
for (unsigned char y = grid->y_max; y > 0; y--)
|
||||||
{
|
{
|
||||||
std::cout << "|";
|
std::cout << "|";
|
||||||
for (unsigned char x = 0; x < grid.x_max; x++)
|
for (unsigned char x = 0; x < grid->x_max; x++)
|
||||||
{
|
{
|
||||||
unsigned int offset = simulation::getCellOffset(x, y-1, 0, grid);
|
unsigned int offset = getCellOffset(x, y-1, 0, grid);
|
||||||
unsigned int material = grid.out[offset];
|
unsigned int material = grid->out[offset];
|
||||||
switch (material)
|
switch (material)
|
||||||
{
|
{
|
||||||
case simulation::VACUUM:
|
case simulation::VACUUM:
|
||||||
|
@ -26,12 +26,12 @@ void printGrid(simulation::SimulationGrid grid)
|
||||||
}
|
}
|
||||||
std::cout << "|" << std::endl;
|
std::cout << "|" << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << std::string(grid.x_max+2, '_') << std::endl;
|
std::cout << std::string(grid->x_max+2, '_') << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initGrid(simulation::SimulationGrid grid) {
|
void initGrid(simulation::SimulationGrid* grid) {
|
||||||
unsigned int offset = simulation::getCellOffset(0,1,0, grid);
|
unsigned int offset = simulation::getCellOffset(0,2,0, grid);
|
||||||
grid.in[offset] = simulation::WATER;
|
grid->in[offset] = simulation::WATER;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -50,11 +50,11 @@ int main() {
|
||||||
zMax
|
zMax
|
||||||
};
|
};
|
||||||
|
|
||||||
initGrid(grid);
|
initGrid(&grid);
|
||||||
step(0, grid);
|
|
||||||
|
|
||||||
for (int n=0; n< 2; n++) {
|
for (int n=0; n< 2; n++) {
|
||||||
printGrid(grid);
|
simulation::step(0, &grid);
|
||||||
|
printGrid(&grid);
|
||||||
|
prepareForNextStep(&grid);
|
||||||
}
|
}
|
||||||
std::free(inCells);
|
std::free(inCells);
|
||||||
std::free(outCells);
|
std::free(outCells);
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
namespace simulation
|
namespace simulation
|
||||||
{
|
{
|
||||||
unsigned int getCellOffset(
|
int getCellOffset(
|
||||||
unsigned char x,
|
unsigned char x,
|
||||||
unsigned char y,
|
unsigned char y,
|
||||||
unsigned char z,
|
unsigned char z,
|
||||||
SimulationGrid grid)
|
SimulationGrid* grid)
|
||||||
{
|
{
|
||||||
if (x < grid.x_max && y < grid.y_max && z < grid.z_max)
|
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);
|
int offset = x + y * grid->x_max + z * (grid->y_max * grid->x_max);
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -18,33 +19,55 @@ namespace simulation
|
||||||
|
|
||||||
void step(
|
void step(
|
||||||
double millisecondsSinceLastStep,
|
double millisecondsSinceLastStep,
|
||||||
SimulationGrid grid)
|
SimulationGrid* grid)
|
||||||
{
|
{
|
||||||
for (unsigned char x = 0; x < grid.x_max; x++)
|
for (unsigned char x = 0; x < grid->x_max; x++)
|
||||||
{
|
{
|
||||||
for (unsigned char y = 0; y < grid.y_max; y++)
|
for (unsigned char y = 0; y < grid->y_max; y++)
|
||||||
{
|
{
|
||||||
for (unsigned char z = 0; z < grid.z_max; z++)
|
for (unsigned char z = 0; z < grid->z_max; z++)
|
||||||
{
|
{
|
||||||
unsigned int offset = getCellOffset(x, y, z, grid);
|
int offset = getCellOffset(x, y, z, grid);
|
||||||
unsigned int material = grid.in[offset];
|
unsigned int material = grid->in[offset];
|
||||||
switch (material)
|
switch (material)
|
||||||
{
|
{
|
||||||
case WATER:
|
case WATER:
|
||||||
if (y > 0)
|
|
||||||
{
|
{
|
||||||
unsigned int downOffset = getCellOffset(x, y - 1, z, grid);
|
int downOffset = getCellOffset(x, y - 1, z, grid);
|
||||||
// Flow down if cell is empty
|
// Flow down if cell is empty
|
||||||
if (grid.out[downOffset] == 0)
|
if (downOffset >= 0 && grid->out[downOffset] == 0)
|
||||||
{
|
{
|
||||||
grid.out[downOffset] = material;
|
grid->out[downOffset] = material;
|
||||||
|
grid->out[offset] = VACUUM;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
// Stay in the same location
|
||||||
|
grid->out[offset] = material;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case MERCURY:
|
||||||
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
grid.out[offset] = material;
|
{
|
||||||
|
grid->out[offset] = material;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prepareForNextStep(SimulationGrid* grid)
|
||||||
|
{
|
||||||
|
// Flip the buffers
|
||||||
|
unsigned int* inCells = grid->in;
|
||||||
|
grid->in = grid->out;
|
||||||
|
grid->out = inCells;
|
||||||
|
// Clear out buffer
|
||||||
|
size_t bufferSizeInBytes = sizeof(unsigned int) * grid->x_max * grid->y_max * grid->z_max;
|
||||||
|
std::memset(grid->out, VACUUM, bufferSizeInBytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,15 +24,16 @@ namespace simulation
|
||||||
|
|
||||||
void step(
|
void step(
|
||||||
double millisecondsSinceLastStep,
|
double millisecondsSinceLastStep,
|
||||||
SimulationGrid grid
|
SimulationGrid* grid
|
||||||
);
|
);
|
||||||
|
|
||||||
unsigned int getCellOffset(
|
int getCellOffset(
|
||||||
unsigned char x,
|
unsigned char x,
|
||||||
unsigned char y,
|
unsigned char y,
|
||||||
unsigned char z,
|
unsigned char z,
|
||||||
SimulationGrid grid);
|
SimulationGrid* grid);
|
||||||
|
|
||||||
|
void prepareForNextStep(SimulationGrid* grid);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //SIMULATION_H
|
#endif //SIMULATION_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue