I've read through the docs https://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/flex/manual.html?ncid=afm-chs-44270&ranMID=44270&ranEAID=a1LgFw09t88&ranSiteID=a1LgFw09t88-TGVNs5HtsNwQmL1xZ5XT8A#particles
But I still can't get a working simulation. Are there any Debug options I dont't know about?
I got Flex from https://github.com/NVIDIAGameWorks/FleX
Here's my code:
#pragma once
#include "FlexPhysics.h"
#include "NvFlex.h"
#include "NvFlexExt.h"
#include <iostream>
NvFlexBuffer* particleBuffer;
NvFlexBuffer* velocityBuffer;
NvFlexBuffer* phaseBuffer;
struct Vec4struct {
    float x, y, z, w;
};
void MyErrorCallback(NvFlexErrorSeverity severity, const char* msg, const char* file, int line)
{
    std::cout << "Flex: %s - %s:%d\n" << msg << file << line << std::endl;
}
    
int n = 10000;
int main() {
    NvFlexLibrary* library = NvFlexInit(120, MyErrorCallback);
    // create new solver
    NvFlexSolverDesc solverDesc;
    NvFlexSetSolverDescDefaults(&solverDesc);
    solverDesc.maxParticles = n;
    solverDesc.maxDiffuseParticles = 0;
    NvFlexSolver* solver = NvFlexCreateSolver(library, &solverDesc);
    particleBuffer = NvFlexAllocBuffer(library, n, sizeof(Vec4struct), eNvFlexBufferHost);
    velocityBuffer = NvFlexAllocBuffer(library, n, sizeof(Vec4struct), eNvFlexBufferHost);
    phaseBuffer = NvFlexAllocBuffer(library, n, sizeof(int), eNvFlexBufferHost);
    //set params
    NvFlexParams p;
    p.gravity[0] = -10.0f;
    p.radius = 0.01f;
    p.drag = 0.1f;
    p.damping = 0.0f;
    NvFlexSetParams(solver, &p);
    //spawn particles
    int particle_count = 6;
    float particle_mass = 100;
    //map buffers
    Vec4struct* particles = (Vec4struct*)NvFlexMap(particleBuffer, eNvFlexMapWait);
    Vec4struct* velocities = (Vec4struct*)NvFlexMap(velocityBuffer, eNvFlexMapWait);
    int* phases = (int*)NvFlexMap(phaseBuffer, eNvFlexMapWait);
    for (int i = 0; i < particle_count; ++i)
    {
        particles[i] = { i * 5.0f,0,0, 1.0f / particle_mass };
        velocities[i] = { 0.0f,0.0f,0.0f};
        phases[i] = NvFlexMakePhase(0, 1);
    }
    //unmap buffers
    NvFlexUnmap(particleBuffer);
    NvFlexUnmap(velocityBuffer);
    NvFlexUnmap(phaseBuffer);
    NvFlexSetParticles(solver, particleBuffer, NULL);
    NvFlexSetVelocities(solver, velocityBuffer, NULL);
    NvFlexSetPhases(solver, phaseBuffer, NULL);
    float delta_time = 1 / 60.0f;
    while (true)
    {
        //map buffers
        Vec4struct* particles = (Vec4struct*)NvFlexMap(particleBuffer, eNvFlexMapWait);
        Vec4struct* velocities = (Vec4struct*)NvFlexMap(velocityBuffer, eNvFlexMapWait);
        int* phases = (int*)NvFlexMap(phaseBuffer, eNvFlexMapWait);
        //"draw" particles
        std::cout << "x:" << particles[0].x << " y:" << particles[0].y << " z:" << particles[0].z << std::endl;
        
        //unmap buffers
        NvFlexUnmap(particleBuffer);
        NvFlexUnmap(velocityBuffer);
        NvFlexUnmap(phaseBuffer);
        // set active count
        NvFlexSetActiveCount(solver, particle_count);
        // tick
        NvFlexUpdateSolver(solver, delta_time, 1, false);
        // read back (async)
        NvFlexGetParticles(solver, particleBuffer, NULL);
        NvFlexGetVelocities(solver, velocityBuffer, NULL);
        NvFlexGetPhases(solver, phaseBuffer, NULL);
    }
    
    NvFlexFreeBuffer(particleBuffer);
    NvFlexFreeBuffer(velocityBuffer);
    NvFlexFreeBuffer(phaseBuffer);
    NvFlexDestroySolver(solver);
    NvFlexShutdown(library);
    return 0;
}
Here's some of the Output:
x:0 y:0 z:0
x:0 y:0 z:0
x:0 y:0 z:0
x:0 y:0 z:0
x:0 y:0 z:0
x:0 y:0 z:0
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00274073 y:1.26542e+06 z:1.26542e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
x:0.00550891 y:2.53083e+06 z:2.53083e+06
Now I'd expect the particles to start Falling to the right. Im not sure the data I load into the particlebuffer is correct how can I check that? All the docs say is [float x, float y, float z, inverse_mass]. What Parameters should I check?
And all I can find to the buffer is:
/**
 * Opaque type representing a data buffer, type and contents depends on usage, see NvFlexAllocBuffer()
 */
typedef struct NvFlexBuffer NvFlexBuffer;
 
                        
You Have to Initialize all the Flex Params or else it simple wont work, i think this is because they're saved in a struct.