Search code examples
c#xnamonogameparticle-system

Particle effect displaying same particles -- not randomizing, Monogame


When I create particle effects, they all have the same pattern. They are rotated, but they all have the same pattern and same colored particles. See picture:

enter image description here

This is how a new ParticleEffect gets created:

ParticleEffect p = new ParticleEffect(textures, Vector2.Zero, destination, speed);

Where textures is aTexture2D list, VectorZero is the starting location, and so on.

Whenever a new ParticleEffect gets created it gets added to the ParticleList list, which later loops through all the items and calls update and draw for each effect inside.

Here is where the particles are randomised:

private Particle GenerateNewParticle()
{
    Random random = new Random();
    Texture2D texture = textures[random.Next(textures.Count)];
    Vector2 position = EmitterLocation;
    Vector2 velocity = new Vector2(
            1f * (float)(random.NextDouble() * 2 - 1),
            1f * (float)(random.NextDouble() * 2 - 1));
    float angle = 0;
    float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
    Color color = new Color(
            (float)random.NextDouble(),
            (float)random.NextDouble(),
            (float)random.NextDouble());
    float size = (float)random.NextDouble();
    int ttl = 20 + random.Next(40);

    return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
}

A bunch of randoms there, but each effect still comes out the same. Comment if you want to see more code.

Edit: Here is how a particle gets drawn:

public void Draw(SpriteBatch spriteBatch)
{
    Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
    Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);

    spriteBatch.Draw(Texture, Position, sourceRectangle, Color,
        Angle, origin, Size, SpriteEffects.None, 0f);
}

Solution

  • By default, Random instances are initiated with the current time as seed, that means the same sequence of numbers will be re-generated if you create instances at the same time - do not create new instances, reuse an existing instance to get more "randomized" behavior (in your case e.g. use a static Random instance).