Example #1
0
    public void TestSetRigidsGetTransforms()
    {
        float   DELTA_T = 0.016f;
        float   INTERACTION_DISTANCE = 0.5f;
        float   SOLID_REST_DISTANCE  = 0.2f;
        Vector3 GRAVITY = new Vector3(1, 2, 3);

        Flex.Library    lib  = Flex.Init(Flex.FLEX_VERSION, ErrorCallback);
        Flex.SolverDesc desc = default(Flex.SolverDesc);
        Flex.SetSolverDescDefaults(ref desc);
        desc.maxParticles        = 1000;
        desc.maxDiffuseParticles = 1000;
        Flex.Solver solver = Flex.CreateSolver(lib, ref desc);

        Flex.Params prms = new Flex.Params();
        Flex.GetParams(solver, ref prms);
        prms.radius            = INTERACTION_DISTANCE;
        prms.solidRestDistance = SOLID_REST_DISTANCE;
        prms.gravity           = GRAVITY;
        Flex.SetParams(solver, ref prms);

        Flex.Buffer particles = CreateBuffer(lib, 8, 4, new float[]
        {
            0.1f, 0.1f, 0.1f, 1.0f, -0.1f, 0.1f, 0.1f, 1.0f, -0.1f, -0.1f, 0.1f, 1.0f, 0.1f, -0.1f, 0.1f, 1.0f,
            0.1f, 0.1f, -0.1f, 1.0f, -0.1f, 0.1f, -0.1f, 1.0f, -0.1f, -0.1f, -0.1f, 1.0f, 0.1f, -0.1f, -0.1f, 1.0f,
        });
        Flex.SetParticles(solver, particles);

        Flex.Buffer active = CreateBuffer(lib, 8, 1, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
        Flex.SetActive(solver, active);

        Flex.Buffer offsets   = CreateBuffer(lib, 2, 1, new int[] { 0, 8 });
        Flex.Buffer indices   = CreateBuffer(lib, 8, 1, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
        Flex.Buffer positions = CreateBuffer(lib, 8, 3, new float[]
        {
            0.1f, 0.1f, 0.1f, -0.1f, 0.1f, 0.1f, -0.1f, -0.1f, 0.1f, 0.1f, -0.1f, 0.1f,
            0.1f, 0.1f, -0.1f, -0.1f, 0.1f, -0.1f, -0.1f, -0.1f, -0.1f, 0.1f, -0.1f, -0.1f,
        });
        const float N = 0.57735026918962576450914878050196f; // 1 / sqrt(3)

        Flex.Buffer normals = CreateBuffer(lib, 8, 4, new float[]
        {
            N, N, N, 0, -N, N, N, 0, -N, -N, N, 0, N, -N, N, 0,
            N, N, -N, 0, -N, N, -N, 0, -N, -N, -N, 0, N, -N, -N, 0,
        });
        Flex.Buffer stiffness    = CreateBuffer(lib, 1, 1, new float[] { 1.0f });
        Flex.Buffer thresholds   = CreateBuffer(lib, 1, 1, new float[] { 1.0f });
        Flex.Buffer creeps       = CreateBuffer(lib, 1, 1, new float[] { 1.0f });
        Flex.Buffer rotations    = CreateBuffer(lib, 1, 4, new float[] { 0.0f, 0.0f, 0.0f, 1.0f });
        Flex.Buffer translations = CreateBuffer(lib, 1, 3, new float[] { 0.0f, 0.0f, 0.0f });

        Flex.SetRigids(solver, offsets, indices, positions, normals, stiffness, thresholds, creeps, rotations, translations, 1, 8);

        Flex.UpdateSolver(solver, DELTA_T, 1);

        Flex.Buffer nullBuffer = default(Flex.Buffer);
        Flex.GetRigids(solver, nullBuffer, nullBuffer, nullBuffer, nullBuffer, nullBuffer, nullBuffer, nullBuffer, rotations, translations);

        float[] values; ReadBuffer(lib, translations, 1, 3, out values);
        Vector3 expectedPosition = GRAVITY * DELTA_T * DELTA_T;
        Vector3 currentPosition  = new Vector3(values[0], values[1], values[2]);

        Assert.AreEqual(expectedPosition.magnitude, currentPosition.magnitude, 0.001f);

        Flex.FreeBuffer(translations);
        Flex.FreeBuffer(rotations);
        Flex.FreeBuffer(creeps);
        Flex.FreeBuffer(thresholds);
        Flex.FreeBuffer(stiffness);
        Flex.FreeBuffer(normals);
        Flex.FreeBuffer(positions);
        Flex.FreeBuffer(indices);
        Flex.FreeBuffer(offsets);

        Flex.FreeBuffer(active);
        Flex.FreeBuffer(particles);

        Flex.DestroySolver(solver);
        Flex.Shutdown(lib);
    }