public FluidSimulation(World world, SpriteBatch spriteBatch, SpriteFont font) { _world = world; _spriteBatch = spriteBatch; _font = font; _activeParticles = new List<int>(MAX_PARTICLES); _liquid = new Particle[MAX_PARTICLES]; for (int i = 0; i < MAX_PARTICLES; i++) { _liquid[i] = new Particle(Vector2.Zero, Vector2.Zero, false); _liquid[i].index = i; } _delta = new Vector2[MAX_PARTICLES]; _scaledPositions = new Vector2[MAX_PARTICLES]; _scaledVelocities = new Vector2[MAX_PARTICLES]; _grid = new Dictionary<int, Dictionary<int, List<int>>>(); _pixel = new Texture2D(_spriteBatch.GraphicsDevice, 1, 1); _pixel.SetData<Color>(new[] { Color.White }); _random = new Random(); _halfScreen = new Vector2( _spriteBatch.GraphicsDevice.Viewport.Width, _spriteBatch.GraphicsDevice.Viewport.Height) / 2f; _simulationAABB.LowerBound.X = -(_halfScreen.X + 100f); _simulationAABB.LowerBound.Y = -(_halfScreen.Y + 100f); _simulationAABB.UpperBound.X = _halfScreen.X + 100f; _simulationAABB.UpperBound.Y = _halfScreen.Y + 100f; }
private void findNeighbors(Particle particle) { particle.neighborCount = 0; Dictionary<int, List<int>> gridX; List<int> gridY; for (int nx = -1; nx < 2; nx++) { for (int ny = -1; ny < 2; ny++) { int x = particle.ci + nx; int y = particle.cj + ny; if (_grid.TryGetValue(x, out gridX) && gridX.TryGetValue(y, out gridY)) { for (int a = 0; a < gridY.Count; a++) { if (gridY[a] != particle.index) { particle.neighbors[particle.neighborCount] = gridY[a]; particle.neighborCount++; if (particle.neighborCount >= MAX_NEIGHBORS) return; } } } } } }