#Reference problem I think

37 messages · Page 1 of 1 (latest)

sturdy nymph
#

I'm making a fluid simulator and for this I have made a grid to store the particles in each cell so I can limit collision checks to only the neighbors of each particle. Now I have managed to implement the grid and It does indeed store the particles in the right cell but when I iterate over the particles in the grid and update their values the update doesn't show up in the main container of the particles.

Here is how I made the grid:

void clearGrid(Grid& grid)
{
    for (int i = 0; i < grid.width; ++i) {
        for (int j = 0; j < grid.height; ++j) {
            grid.cells[i][j].clear();
        }
    }
}

void createGrid(Grid& grid)
{
    grid.cells.resize(grid.width + 1); // Resize the outer vector to have a size equal to grid.width
    for (int i = 0; i < grid.width; ++i) {
        grid.cells[i].resize(grid.height + 1); // Resize each inner vector to have a size equal to grid.height
    }
}

void updateGrid(Grid& grid, std::vector<Particle>& particles)
{
    grid.cells.clear(); // reset the grid
    createGrid(grid); // remake the grid in reference

    for (Particle& particle : particles)
    {
        // calculate cell indices and multiply to find in which cell we are 
        int cell_x = static_cast<int>((particle.position.x + 4) / grid.cellSize);
        int cell_y = static_cast<int>((particle.position.y + 3) / grid.cellSize);
        // sometimes particle pushed past the wall gotta correct the value
        if (cell_x > grid.width - 1) cell_x = grid.width - 1;
        if (cell_y > grid.height - 1) cell_y = grid.height - 1;

        grid.cells[cell_x][cell_y].push_back(particle);
    }

    // make the neighbors here

}

Here is how the particle is made and in the main.cpp it is in a std::vector<Particle> particles:

struct Particle
{
    glm::vec2 position;
    glm::vec2 velocity;
    float density;
    float density0 = 1000; // 
    const float mass = 1.0f;
    const float radius = 0.02f;
    const int segments = 6;

};
languid moonBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sturdy nymph
#

Here is the rest:

#

I passed everything as a reference hoping it would refer the "Particles" created in the main code : Here is how I went about updating the density:

void computeDensity(Grid& grid)
{
    int iterCount = 0;
    for (auto& column: grid.cells) // check every comlumn
    {
        for (auto& cell: column) // check cells in the columns
        {
            for (Particle& particle1: cell) // iterate over the particles in the cell
            {
                for (Particle& particle2: cell) // check particle1 with other particles in the cell
                {
                    if (particle1.position != particle2.position) // if it's not the same particle
                    {
                        // check distance between particles
                        glm::vec2 distanceVect = particle1.position - particle2.position;
                        float r2 = glm::dot(distanceVect, distanceVect);
                        float h2 = pow(cutoffDistance, 2);

                        //std::cout << h2 << "    " << r2 << "    " << h2 - r2 << std::endl;
                        float densityValue = particle2.mass * (315 / (64 * PI * pow(cutoffDistance, 9))) * pow(h2 - r2, 3);

                        if (r2 < h2)
                        {
                            particle1.density += densityValue;
                        }
                        //std::cout << densityValue << std::endl;
                        
                    }
                }
            }
        }
    }
}

when I print the particle.density after running this it remains 0. Anyone know what might be the problem?

calm bronze
#

Have you ever used a debugger?

sturdy nymph
#

I never use it

calm bronze
#

Well now would be a good time to start

sturdy nymph
#

I usually debug doing prints and stuff

#

ok

calm bronze
#

If you used one you could step through the code line by line and see what is happening

#

With no need to print

sturdy nymph
#

I have one setup one sec

#

do I have to do a line break

#

?

calm bronze
#

Yeah if you add a breakpoint to the line you want, this is to the left of the line by the line number, on Visual Studio / VSCode this will show as a red point when you hover over it

sturdy nymph
#

Ok it works

calm bronze
#

Nice now press the down arrow to go into the function

sturdy nymph
#

oh :0

#

ok I'm in

#

step into is like to go forward ?

#

the line break runs the code until a certain point and the I have to kinda walk forward with the down arrow ?

calm bronze
#

yeah it will step into things, from left to right the symbols mean Play(to next breakpoint), StepOver(if you don't want to step into a function, useful to stdlib stuff), StepInto, StepOut(go up one level of the call stack), restart, stop

calm bronze
sturdy nymph
#

oh ok cool

#

well time to dig through my code

#

cause I already tested and int he function the values are fine

#

but when I go to send em into the particle it just remains at 0

calm bronze
#

I am guessing you are probably multiplying something by 0 so densityValue is always 0

#

But not you can find out for sure

sturdy nymph
calm bronze
#

Now you will (almost) never need a print statement again

sturdy nymph
#

thank god less time waste 😄

#

thanks again

calm bronze
#

It's cool, learning a debugger is one of the most essential tools you need to use everyday in programming

languid moonBOT
#

@sturdy nymph Has your question been resolved? If so, type !solved :)

sturdy nymph
#

!solved