但我仍然无法获得有效的模拟。有没有我不知道的调试选项?
我从https://github.com/NVIDIAGameWorks/FleX获得了 Flex
这是我的代码:
#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;
}
这是一些输出:
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
现在我希望粒子开始向右下落。我不确定我加载到粒子缓冲区中的数据是否正确,我该如何检查?所有文档都说是[float x,float y,float z,inverse_mass]。我应该检查哪些参数?
我能找到的缓冲区是:
/**
* Opaque type representing a data buffer, type and contents depends on usage, see NvFlexAllocBuffer()
*/
typedef struct NvFlexBuffer NvFlexBuffer;