private void ComputeDensityPressure()
    {
        for (int i = 0; i < particles.Length; i++)
        {
            hash.Insert(particles[i].position, particles[i]);
            particles[i].density = 0.0f;

            //currentParticleBin = hash.GetBin(particles[i].position);
            // listNeighbours = hash.GetNeighbourBinsFromPosition(particles[i].position);
            neighbourParticles = hash.GetParticlesFromBins(hash.GetNeighbourBinsFromPosition(particles[i].position));
            //Debug.Log(neighbourParticles.Count);

            //look at neighbours
            for (int j = 0; j < neighbourParticles.Count; j++)
            {
                Vector3 dist = neighbourParticles[j].position - particles[i].position;
                float   r2   = dist.sqrMagnitude;

                if (r2 < parameters[0].smoothingRadiusSq)
                {
                    particles[i].density += parameters[0].particleMass *
                                            (315.0f / (64.0f * Mathf.PI * Mathf.Pow(parameters[0].smoothingRadius, 9.0f))) *
                                            Mathf.Pow(parameters[0].smoothingRadiusSq - r2, 3.0f);
                }
            }

            particles[i].pressure = GAS_CONST * (particles[i].density - parameters[0].restDensity);
        }
    }
Ejemplo n.º 2
0
    void Start()
    {
        hash = new SpatialHash<Transform>(cellSize);
        clones = new Transform[numberOfPoints];
        wireSize = Mathf.CeilToInt(sphereRadius / cellSize) * cellSize + cellSize / 2;
        for (var i = 0; i < numberOfPoints; i++)
        {
            clone = Instantiate(point, Random.insideUnitSphere * sphereRadius, Quaternion.identity) as Transform;
            if (clone != null)
            {
                hash.Insert(clone.position, clone);
                clone.parent = transform;
                clones[i] = clone;
            }
        }
        InvokeRepeating("MovePoint", 0, 0.5f);
        InvokeRepeating("ClearHash", 10, 10);

        for (var x = -wireSize; x <= wireSize; x++)
        {
            for (var y = -wireSize; y <= wireSize; y++)
            {
                for (var z = -wireSize; z <= wireSize; z++)
                {
                    yellowCubes.Add(Instantiate(yellowCube, new Vector3(x, y, z), Quaternion.identity) as Transform);
                    blueCubes.Add(Instantiate(blueCube, new Vector3(x, y, z), Quaternion.identity) as Transform);
                }
            }
        }
    }
        public void Clear()
        {
            var spatialHash = new SpatialHash <int>(16);

            var rectA          = new Rectangle(-2, -2, 2, 2);
            var rectATransform = new Transform2D(new Vector2(-8, -8));

            var rectB          = new Rectangle(-2, -2, 2, 2);
            var rectBTransform = new Transform2D(new Vector2(8, 8));

            spatialHash.Insert(0, rectA, rectATransform);
            spatialHash.Insert(1, rectB, rectBTransform);

            spatialHash.Clear();

            spatialHash.Retrieve(0, rectA, rectATransform).Should().HaveCount(0);
        }
        public void InsertAndRetrieveSameValues()
        {
            var spatialHash = new SpatialHash <int>(16);

            var rectA          = new Rectangle(-2, -2, 2, 2);
            var rectATransform = new Transform2D(new Vector2(-8, -8));

            var rectB          = new Rectangle(-2, -2, 2, 2);
            var rectBTransform = new Transform2D(new Vector2(-8, -8));

            var rectC          = new Rectangle(-1, -1, 1, 1);
            var rectCTransform = new Transform2D(new Vector2(-8, -8));

            spatialHash.Insert(0, rectA, rectATransform);
            spatialHash.Insert(1, rectB, rectBTransform);
            spatialHash.Insert(2, rectC, rectCTransform);

            spatialHash.Retrieve(2, rectC, rectCTransform).Should().HaveCount(2);
        }
Ejemplo n.º 5
0
 void Start()
 {
     spatialHash = new SpatialHash(2);
     vertices    = GetComponentsInChildren <vertex> ();
     for (int i = 0; i < vertices.Length; i++)
     {
         spatialHash.Insert(vertices[i].transform.position, vertices[i]);
     }
     triangles   = new List <vertex>();
     constraints = new List <Constraint>();
     GenerateConstraints();
 }
        public void InsertAndRetrieve()
        {
            var spatialHash = new SpatialHash <int>(16);

            var rectA          = new Rectangle(-2, -2, 2, 2);
            var rectATransform = new Transform2D(new Vector2(-8, -8));

            var rectB          = new Rectangle(-2, -2, 2, 2);
            var rectBTransform = new Transform2D(new Vector2(8, 8));

            var rectC          = new Rectangle(-2, -2, 2, 2);
            var rectCTransform = new Transform2D(new Vector2(24, -4));

            var rectD          = new Rectangle(-2, -2, 2, 2);
            var rectDTransform = new Transform2D(new Vector2(24, 24));

            var circleA          = new Circle(2);
            var circleATransform = new Transform2D(new Vector2(24, -8));

            var circleB          = new Circle(8);
            var circleBTransform = new Transform2D(new Vector2(16, 16));

            var line          = new Line(new Position2D(20, -4), new Position2D(22, -12));
            var lineTransform = new Transform2D(new Vector2(0, 0));

            var point          = new Point();
            var pointTransform = new Transform2D(new Position2D(8, 8));

            spatialHash.Insert(0, rectA, rectATransform);
            spatialHash.Insert(1, rectB, rectBTransform);
            spatialHash.Insert(2, rectC, rectCTransform);
            spatialHash.Insert(3, rectD, rectDTransform);
            spatialHash.Insert(4, circleA, circleATransform);
            spatialHash.Insert(1, circleB, circleBTransform);
            spatialHash.Insert(6, line, lineTransform);
            spatialHash.Insert(7, point, pointTransform);

            spatialHash.Retrieve(0, rectA, rectATransform).Should().BeEmpty();
            spatialHash.Retrieve(1, rectB, rectBTransform).Should().NotContain((1, circleB, circleBTransform));
            spatialHash.Retrieve(1, rectB, rectBTransform).Should().Contain((7, point, pointTransform));
            spatialHash.Retrieve(2, rectC, rectCTransform).Should().Contain((6, line, lineTransform)).And.Contain((4, circleA, circleATransform));
            spatialHash.Retrieve(3, rectD, rectDTransform).Should().Contain((1, circleB, circleBTransform));

            spatialHash.Retrieve(4, circleA, circleATransform).Should().Contain((6, line, lineTransform)).And.Contain((2, rectC, rectCTransform));
            spatialHash.Retrieve(1, circleB, circleBTransform).Should().NotContain((1, rectB, rectBTransform)).And.Contain((3, rectD, rectDTransform));

            spatialHash.Retrieve(6, line, lineTransform).Should().Contain((4, circleA, circleATransform)).And.Contain((2, rectC, rectCTransform));
        }
    private void InitSPH()
    {
        particles = new SPHParticle[amount];
        hash      = new SpatialHash <SPHParticle>(cellSize);

        for (int i = 0; i < amount; i++)
        {
            float jitter = (Random.value * 2f - 1f) * parameters[parameterID].particleRadius * 0.1f;
            float x      = worldStartOffsetX + (i % rowSize) + Random.Range(-0.1f, 0.1f);
            float y      = 2 + (float)((i / rowSize) / rowSize) * 1.1f;
            float z      = worldStartOffsetY + ((i / rowSize) % rowSize) + Random.Range(-0.1f, 0.1f);

            GameObject go = Instantiate(Prefab);
            go.transform.localScale = Vector3.one * parameters[parameterID].particleRadius;
            go.transform.position   = new Vector3(x + jitter, y, z + jitter);
            go.name = "Particle" + i.ToString();

            particles[i].Init(new Vector3(x, y, z), parameterID, go);
            particles[i].velocity = Vector3.zero;
            hash.Insert(particles[i].position, particles[i]);
        }
    }