示例#1
0
    private void UpdateWind()
    {
        // Retrieve the wind from the Flex Solver
        Flex.Params windParams = new Flex.Params();
        Flex.GetParams(container.solver, ref windParams);

        // Swap the windtype if the current windtype has exceeded its duration
        if (currentWind.HasEnded)
        {
            float lastWindSpeed = currentWind.currentSpeed;
            float lastSwivel    = currentWind.currentSwivel;
            currentWind = (currentWind.Equals(wind)) ? burstWind : wind;
            currentWind.Init(lastWindSpeed, lastSwivel);
        }

        // Update and set the wind values and rotate the flag parents to look in the right direction
        currentWind.UpdateWind();
        currentWindDirection = Quaternion.AngleAxis(currentWind.currentSwivel, Vector3.up) * windDirection;
        currentWindDirection.Normalize();
        windParams.wind   = currentWind.currentSpeed * currentWindDirection;
        windParams.wind.y = currentWind.currentSpeed / 8f; // <-- MAGIC NUMBER !!! TODO: create a function for upward wind lift.
        RotateParents();

        // Update the wind in the Flex Solver
        Flex.SetParams(container.solver, ref windParams);
    }
示例#2
0
 void UpdateParams()
 {
     Flex.Params prms = new Flex.Params();
     Flex.GetParams(m_solverHandle, ref prms);
     prms.numIterations     = m_iterationCount;
     prms.gravity           = m_gravity;
     prms.radius            = m_radius;
     prms.solidRestDistance = m_solidRest;
     prms.fluidRestDistance = m_fluidRest;
     prms.staticFriction    = m_staticFriction;
     prms.dynamicFriction   = m_dynamicFriction;
     prms.particleFriction  = m_particleFriction;
     prms.restitution       = m_restitution;
     prms.adhesion          = m_adhesion;
     prms.sleepThreshold    = m_sleepThreshold;
     prms.maxSpeed          = m_maxSpeed;
     prms.maxAcceleration   = m_maxAcceleration;
     prms.shockPropagation  = m_shockPropagation;
     prms.dissipation       = m_dissipation;
     prms.damping           = m_damping;
     prms.wind = m_wind;
     prms.drag = m_drag;
     prms.lift = m_lift;
     //prms.fluid = m_fluid;
     prms.cohesion             = m_cohesion;
     prms.surfaceTension       = m_surfaceTension;
     prms.viscosity            = m_viscosity;
     prms.vorticityConfinement = m_vorticityConfinement;
     prms.anisotropyScale      = m_anisotropyScale;
     prms.anisotropyMin        = m_anisotropyMin;
     prms.anisotropyMax        = m_anisotropyMax;
     prms.smoothing            = m_smoothing;
     prms.solidPressure        = m_solidPressure;
     prms.freeSurfaceDrag      = m_freeSurfaceDrag;
     prms.buoyancy             = m_buoyancy;
     prms.diffuseThreshold     = m_diffuseThreshold;
     prms.diffuseBuoyancy      = m_diffuseBuoyancy;
     prms.diffuseDrag          = m_diffuseDrag;
     prms.diffuseBallistic     = m_diffuseBallistic;
     //prms.diffuseSortAxis = m_diffuseSortAxis;
     prms.diffuseLifetime = m_diffuseLifetime;
     //prms.plasticThreshold = m_plasticThreshold;
     //prms.plasticCreep = m_plasticCreep;
     prms.collisionDistance       = m_collisionDistance;
     prms.particleCollisionMargin = m_particleCollisionMargin;
     prms.shapeCollisionMargin    = m_shapeCollisionMargin;
     prms.numPlanes        = m_planes.Length;
     prms.plane0           = m_planes.Length > 0 ? m_planes[0] : Vector4.zero;
     prms.plane1           = m_planes.Length > 1 ? m_planes[1] : Vector4.zero;
     prms.plane2           = m_planes.Length > 2 ? m_planes[2] : Vector4.zero;
     prms.plane3           = m_planes.Length > 3 ? m_planes[3] : Vector4.zero;
     prms.plane4           = m_planes.Length > 4 ? m_planes[4] : Vector4.zero;
     prms.plane5           = m_planes.Length > 5 ? m_planes[5] : Vector4.zero;
     prms.plane6           = m_planes.Length > 6 ? m_planes[6] : Vector4.zero;
     prms.plane7           = m_planes.Length > 7 ? m_planes[7] : Vector4.zero;
     prms.relaxationMode   = m_relaxationMode;
     prms.relaxationFactor = m_relaxationFactor;
     Flex.SetParams(m_solverHandle, ref prms);
 }
示例#3
0
    public void TestSetParticlesPhases()
    {
        float DELTA_T = 0.016f;
        float INTERACTION_DISTANCE = 0.5f;
        float SOLID_REST_DISTANCE  = 0.2f;

        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;
        Flex.SetParams(solver, ref prms);

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

        Flex.Buffer phases = CreateBuffer(lib, 2, 1, new int[]
        {
            Flex.MakePhase(1, Flex.Phase.SelfCollide),
            Flex.MakePhase(1, Flex.Phase.SelfCollide),
        });
        Flex.SetPhases(solver, phases);

        Flex.SetActiveCount(solver, 2);

        Flex.Buffer active = CreateBuffer(lib, 2, 1, new int[]
        {
            0, 1,
        });
        Flex.SetActive(solver, active);

        Flex.UpdateSolver(solver, DELTA_T, 1);

        Flex.GetParticles(solver, particles);
        float[] values; ReadBuffer(lib, particles, 2, 4, out values);

        Assert.AreEqual(SOLID_REST_DISTANCE, Vector3.Distance(new Vector3(values[0], values[1], values[2]), new Vector3(values[4], values[5], values[6])));

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

        Flex.DestroySolver(solver);
        Flex.Shutdown(lib);
    }
示例#4
0
    public void TestSetSpringConstraints()
    {
        float DELTA_T = 0.016f;
        float INTERACTION_DISTANCE = 0.5f;
        float SOLID_REST_DISTANCE  = 0.2f;
        float SPRING_LENGTH        = 1.0f;

        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;
        Flex.SetParams(solver, ref prms);

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

        Flex.Buffer indices   = CreateBuffer(lib, 2, 1, new int[] { 0, 1 });
        Flex.Buffer lengths   = CreateBuffer(lib, 1, 1, new float[] { SPRING_LENGTH });
        Flex.Buffer stiffness = CreateBuffer(lib, 1, 1, new float[] { 1.0f });
        Flex.SetSprings(solver, indices, lengths, stiffness, 1);

        Flex.SetActiveCount(solver, 2);

        Flex.Buffer active = CreateBuffer(lib, 2, 1, new int[] { 0, 1 });
        Flex.SetActive(solver, active);

        Flex.UpdateSolver(solver, DELTA_T, 1);

        Flex.GetParticles(solver, particles);
        float[] values; ReadBuffer(lib, particles, 2, 4, out values);

        Assert.AreEqual(SPRING_LENGTH, Vector3.Distance(new Vector3(values[0], values[1], values[2]), new Vector3(values[4], values[5], values[6])), 0.001f);

        Flex.FreeBuffer(particles);
        Flex.FreeBuffer(indices);
        Flex.FreeBuffer(lengths);
        Flex.FreeBuffer(stiffness);
        Flex.FreeBuffer(active);

        Flex.DestroySolver(solver);
        Flex.Shutdown(lib);
    }
示例#5
0
    public void TestSetGetParticlesVelocities()
    {
        Vector3 GRAVITY = new Vector3(1, 2, 3);
        float   DELTA_T = 0.016f;

        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.gravity = GRAVITY;
        Flex.SetParams(solver, ref prms);

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

        Flex.Buffer velocities = CreateBuffer(lib, 1, 3, new float[]
        {
            0.0f, 0.0f, 0.0f,
        });
        Flex.SetVelocities(solver, velocities);

        Flex.SetActiveCount(solver, 1);

        Flex.Buffer active = CreateBuffer(lib, 1, 1, new int[]
        {
            0
        });
        Flex.SetActive(solver, active);

        Flex.UpdateSolver(solver, DELTA_T, 1);

        Flex.GetVelocities(solver, velocities);
        float[] values; ReadBuffer(lib, velocities, 1, 3, out values);

        Assert.AreEqual(GRAVITY * DELTA_T, new Vector3(values[0], values[1], values[2]));

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

        Flex.DestroySolver(solver);
        Flex.Shutdown(lib);
    }
示例#6
0
    public void TestParamsPlaneCollision()
    {
        float   DELTA_T = 0.016f;
        float   INTERACTION_DISTANCE = 0.5f;
        float   SOLID_REST_DISTANCE  = 0.2f;
        Vector3 GRAVITY = new Vector3(0, -9.81f, 0);

        Flex.Library    lib  = Flex.Init(Flex.FLEX_VERSION, ErrorCallback);
        Flex.SolverDesc desc = default(Flex.SolverDesc);
        Flex.SetSolverDescDefaults(ref desc);
        desc.maxParticles        = 1000;
        desc.maxDiffuseParticles = 0;
        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;
        prms.plane0            = new Vector4(0.0f, 1.0f, 0.0f, 0.0f);
        prms.numPlanes         = 1;
        Flex.SetParams(solver, ref prms);

        Flex.Buffer particles = CreateBuffer(lib, 1, 4, new float[] { 0.0f, 2.0f, 0.0f, 1.0f });
        Flex.SetParticles(solver, particles);

        Flex.SetActiveCount(solver, 1);

        Flex.Buffer   active = CreateBuffer(lib, 1, 1, new int[] { 0 });
        Flex.CopyDesc cpyDsc; cpyDsc.srcOffset = cpyDsc.dstOffset = 0; cpyDsc.elementCount = 1;
        Flex.SetActive(solver, active, ref cpyDsc);

        for (int i = 0; i < 100; ++i)
        {
            Flex.UpdateSolver(solver, DELTA_T, 1);
        }

        Flex.GetParticles(solver, particles);
        float[] values; ReadBuffer(lib, particles, 1, 4, out values);

        Assert.AreEqual(0, values[1], 0.001f);

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

        Flex.DestroySolver(solver);
        Flex.Shutdown(lib);
    }
示例#7
0
    public void TestSetGetParams()
    {
        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);
        Assert.AreNotEqual(GRAVITY, prms.gravity);

        prms.gravity = GRAVITY;

        Flex.SetParams(solver, ref prms);
        Assert.AreEqual(GRAVITY, prms.gravity);

        Flex.DestroySolver(solver);
        Flex.Shutdown(lib);
    }
示例#8
0
    private void InitializeWind()
    {
        // Set the current wind type
        switch (startingWind)
        {
        case StartingWind.Burst:
            currentWind = burstWind;
            break;

        default:
            currentWind = wind;
            break;
        }

        // Initialize the current wind type with the current speed and angle of the wind
        // Angle is relative to the desired angle.
        Flex.Params windParams = new Flex.Params();
        Flex.GetParams(container.solver, ref windParams);

        float currentSpeed  = windParams.wind.magnitude;
        float currentSwivel = Vector3.Angle(windParams.wind.normalized, windDirection);

        currentWind.Init(currentSpeed, currentSwivel);
    }
示例#9
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);
    }