示例#1
0
        private void InitSystem()
        {
            dsim        = false;
            dump_frames = false;

            drawables = new List <IDrawable>();
            particles = new List <Particle>();

            _solverEnvironment        = new SolverEnvironment();
            _solverEnvironment.Solver = new EulerSolver();

            liqSystem = new LiquidSystem();
            liqSystem.AllocateData();
            liqSystem.Visualization = Visualization.Both;
            Add(liqSystem);

            _uField = new ContinuousField(liqSystem.N + 2, liqSystem.N + 2);
            _vField = new ContinuousField(liqSystem.N + 2, liqSystem.N + 2);
            _dField = new ContinuousField(liqSystem.N + 2, liqSystem.N + 2);

            mouseForce = new MouseSpringForce(0, new HyperPoint <float>(1f, 1f), 0.02f, 2f, 1f, false);
            Add(mouseForce);

            AddParticle(new Particle(new HyperPoint <float>(0.5f, 0.5f), 1, new List <HyperPoint <float> >()
            {
                new HyperPoint <float>(0.02f, 0.2f),
                new HyperPoint <float>(-0.02f, 0.2f),
                new HyperPoint <float>(-0.02f, -0.2f),
                new HyperPoint <float>(0.02f, -0.2f),
            }));

            _solverEnvironment.Forces.Add(new VelocityFieldForce(particles.ConvertAll(x => x.Index), _uField, _vField, _dField, liqSystem.N + 2));
            _solverEnvironment.Forces.Add(new ViscousDragForce(particles.ConvertAll(x => x.Index), 0.8f));
            _solverEnvironment.Forces.Add(mouseForce);



            mousePointer = new MousePointer();
            Add(mousePointer);

            ClearData();
            UpdateHeader();
        }
 public void ResetSimulation()
 {
     m_HasMouseSelection = false;
     m_CurrentMouseForce = null;
     if (m_SpeedSliderText != null)
     {
         m_SpeedSliderText.text = Math.Round(m_Speed, 4) + "x";
     }
     m_ReversedTime = false;
     if (m_SimulationFlowToggle != null)
     {
         m_SimulationFlowToggle.isOn = false;
     }
     m_TouchForces.Clear();
     m_ParticleSystem.Clear();
     m_Scenario.CreateScenario(m_ParticleSystem);
     m_ParticleSystem.Initialize();
     m_Integrator.Initialize(m_ParticleSystem);
     SetupDebugGameObjects();
 }
    private void HandleUserInteraction()
    {
        if (Input.touchSupported)
        {
            if (Input.touchCount > 0)
            {
                Touch[] touches = Input.touches;
                for (int i = 0; i < touches.Length; ++i)
                {
                    Touch   touch      = touches[i];
                    int     id         = touch.fingerId;
                    Vector3 touchPos3D = Camera.main.ScreenToWorldPoint(touch.position);
                    Vector2 touchPos   = new Vector2(touchPos3D.x, touchPos3D.y);
                    if (touch.phase == TouchPhase.Began)
                    {
                        Particle closestParticle;
                        if (GetClosestParticle(touchPos, m_ParticleSelectThreshold, out closestParticle))
                        {
                            MouseSpringForce touchForce = new MouseSpringForce(closestParticle, touchPos, m_MouseSelectRestLength, m_MouseSelectSpringConstant, m_MouseSelectDampingConstant);
                            // This should never happen, but adding anyway for robustness:
                            MouseSpringForce existingForce;
                            if (m_TouchForces.TryGetValue(id, out existingForce))
                            {
                                m_ParticleSystem.RemoveForce(existingForce);
                            }
                            m_TouchForces[id] = touchForce;
                            m_ParticleSystem.AddForce(touchForce);
                        }
                    }
                    else if (touch.phase == TouchPhase.Moved)
                    {
                        MouseSpringForce existingForce;
                        if (m_TouchForces.TryGetValue(id, out existingForce))
                        {
                            existingForce.UpdateMousePosition(touchPos);
                        }
                    }
                    else if (touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended)
                    {
                        MouseSpringForce existingForce;
                        if (m_TouchForces.TryGetValue(id, out existingForce))
                        {
                            m_ParticleSystem.RemoveForce(existingForce);
                            m_TouchForces.Remove(id);
                        }
                    }
                }
            }
        }
        if (Input.mousePresent && Input.touchCount == 0)
        {
            Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 mousePos   = new Vector2(mousePos3D.x, mousePos3D.y);

            if (!m_HasMouseSelection && Input.GetMouseButtonDown(0))
            {
                Particle closestParticle;
                if (GetClosestParticle(mousePos, m_ParticleSelectThreshold, out closestParticle))
                {
                    m_CurrentMouseForce = new MouseSpringForce(closestParticle, mousePos, m_MouseSelectRestLength,
                                                               m_MouseSelectSpringConstant, m_MouseSelectDampingConstant);
                    m_ParticleSystem.AddForce(m_CurrentMouseForce);
                    m_HasMouseSelection = true;
                }
            }
            else if (m_HasMouseSelection && Input.GetMouseButtonDown(1))
            {
                m_ParticleSystem.RemoveForce(m_CurrentMouseForce);
                m_CurrentMouseForce = null;
                m_HasMouseSelection = false;
            }
            else if (m_HasMouseSelection)
            {
                m_CurrentMouseForce.UpdateMousePosition(mousePos);
            }
        }
    }