locked
particle emitter not emitting RRS feed

  • Question

  • Hi,

    I'm trying to create a simple particle emitter that just spawns pixel sprites at a location and sends them off in multiple directions. For some reason it only creates one particle not 100, or at least only one can be seen.

    Here is my code so far:

    public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;
            GraphicsDevice device;
            int screenWidth;
            int screenHeight;
            Texture2D particleTexture;
            emitter particleEmitter = new emitter(new Vector2(0, 0), 1);
            ParticleData[] particles = new ParticleData[100];
            int numberOfParticles = 100;
            GameTime gameTime = new GameTime();
            
    
            struct ParticleData
            {
                public Vector2 Position;
                public bool alive;
                //public Color Colour;
                public Vector2 direction;
                //public float speed;
            }
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
    
    
            protected override void Initialize()
            {
                graphics.PreferredBackBufferWidth = 500;
                graphics.PreferredBackBufferHeight = 500;
                graphics.IsFullScreen = false;
                graphics.ApplyChanges();
                Window.Title = "PARTICLZ!";
    
                base.Initialize();
            }
    
    
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                device = graphics.GraphicsDevice;
                screenWidth = device.PresentationParameters.BackBufferWidth;
                screenHeight = device.PresentationParameters.BackBufferHeight;
    
                particleTexture = Content.Load<Texture2D>("particle");
    
            }
    
            void SetUpParticles()
            {
                if (particleEmitter.mode == 1)
                {
                   for (int i = 0; i < numberOfParticles; i++)
                    {
                        particles[i].Position = particleEmitter.position;
                        particles[i].alive = true;
                        Random rand = new Random();
                        double rndx, rndy;
                        rndx = rand.NextDouble();
                        rndy = rand.NextDouble();
                        float n = 0.0f;
                        particles[i].direction.X = (float)rndx + n;
                        particles[i].direction.Y = (float)rndy + n;
                        n += 0.1f;
                    }
    
                }
            }
    
            void UpdateParticles()
            {
                for (int i = 0; i < numberOfParticles; i++)
                {
                    if (particles[i].alive)
                    {
                        particles[i].Position += particles[i].direction;
                    }
                }
            }
    
            protected override void UnloadContent()
            {
                
            }
    
    
            protected override void Update(GameTime gameTime)
            {
                ProcessKeyboard();
                float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (elapsedTime > 0)
                {
                    UpdateParticles();
                }
    
                base.Update(gameTime);
            }
    
            private void ProcessKeyboard()
            {
                KeyboardState keybState = Keyboard.GetState();
                if (keybState.IsKeyDown(Keys.Space))
                {
                    particleEmitter.active = true;
                    particleEmitter.position = new Vector2(250, 250);
                    particleEmitter.mode = 1;
                    numberOfParticles = 100;
                    for (int i = 0; i < numberOfParticles; i++)
                    {
                        particles[i].alive = true;
                    }
                    SetUpParticles();
                }
    
                if (keybState.IsKeyDown(Keys.LeftShift))
                {
                    particleEmitter.active = false;
                }
    
                // Allows the game to exit
                if (keybState.IsKeyDown(Keys.Escape))
                    this.Exit();
    
            }
    
            protected override void Draw(GameTime gameTime)
            {
                graphics.GraphicsDevice.Clear(Color.Black);
    
                spriteBatch.Begin();
                DrawParticles();
                spriteBatch.End();
    
                base.Draw(gameTime);
            }
    
            private void DrawParticles()
            {
                for (int i = 0; i < numberOfParticles; i++)
                {
                    if (particles[i].alive)
                    {
                        int xPos = (int)particles[i].Position.X;
                        int yPos = (int)particles[i].Position.Y;
                        //float angle = 1.0f;
                        spriteBatch.Draw(particleTexture, new Vector2(xPos, yPos), Color.White);
                        //spriteBatch.Draw(particleTexture, new Vector2(xPos, yPos), null, Color.Black, angle, particleEmitter.position, angle, SpriteEffects.None, 1);
                    }
                }
    
            }
    }

    I can't tell if it's not creating all of them or just not drawing them.

    Thanks

    Wednesday, May 2, 2012 5:17 AM

Answers

  • In SetUpParticles try moving the Random rand = new Random(); line outside of the for loop. As is now it will generate the same random numbers for each iteration and your particles will all move in the same direction.
    • Proposed as answer by Remco van den Berg Wednesday, May 2, 2012 11:32 AM
    • Marked as answer by Bob Shen Thursday, May 17, 2012 8:01 AM
    Wednesday, May 2, 2012 6:54 AM

All replies

  • In SetUpParticles try moving the Random rand = new Random(); line outside of the for loop. As is now it will generate the same random numbers for each iteration and your particles will all move in the same direction.
    • Proposed as answer by Remco van den Berg Wednesday, May 2, 2012 11:32 AM
    • Marked as answer by Bob Shen Thursday, May 17, 2012 8:01 AM
    Wednesday, May 2, 2012 6:54 AM
  • That was it thanks! I need to fix some other small problems but that had been bugging me for ages cheers.
    Wednesday, May 2, 2012 7:19 AM