Jump to content

Falling particles help

dialgarocksful
Go to solution Solved by dialgarocksful,

Anyways, I found the answer. Turns out, I also need to set the Y-position in the Update method

 

protected override void Update(GameTime gameTime)
{
            
	for (arrayCtr = 0; arrayCtr < snowRect.Length; arrayCtr++)
	{
		randY[arrayCtr]++;
        if (snowRect[arrayCtr].Y > 770) 
        {
			randX[arrayCtr] = rnd.Next(0, 1366);
			randY[arrayCtr] = -5;
        }
        snowRect[arrayCtr] = new Rectangle(randX[arrayCtr], randY[arrayCtr], 10, 10);
    }

    base.Update(gameTime);
}

 

Anyways, without the use of engines I'm trying to create a snow effect in XNA. I'm definitely sure I'm doing something wrong since I'm targeting for a 100-count array and it only produces 10 (including updates). Here's my file and looking for some help in displaying all 100 in timed manner:

 

public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D snowTex;
        Rectangle[] snowRect;
        int[] randX;
        int posY, ctr, arrayCtr;
        Random rnd;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1366;
            IsMouseVisible = true;
        }
        protected override void Initialize()
        {
            snowRect = new Rectangle[100];
            randX = new int[100];
            ctr = 0;
            arrayCtr = 0;
            rnd = new Random();
            posY = -5;
            for (int q = 0; q < snowRect.Length; q++)
            {
                randX[q] = rnd.Next(0, 1366);
            }

            base.Initialize();
        }
        
        protected override void LoadContent()
        {
           
            spriteBatch = new SpriteBatch(GraphicsDevice);
            snowTex = Content.Load<Texture2D>(@"Snow");
        }
        
        protected override void UnloadContent()
        {
            
        }

        
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            posY++;
            ctr++;

            for (arrayCtr = 0; arrayCtr < 100;)
            {
                if (ctr == 120) // TRIGGERS A NEW SET OF SNOW PARTICLES EVERY 2 SECONDS
                {
                    ctr = 0;

                    for (int q = 0; q < 10; q++)
                    {
                        randX[q] = rnd.Next(0, 1366); // RANDOMIZES NEW RANDOM X POSITIONS
                    }
                }

                arrayCtr += 10;

            }


            for (int q = 0; q < 100; q++)
            {
                snowRect[q] = new Rectangle(randX[q], posY, 10, 10);

                if (snowRect[q].Y > 770) //IF SNOW[Q] GOES PAST GRAPHICS WIDTH
                {
                    posY = -5;
                }
            }

            base.Update(gameTime);
        }

        
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            for (int x = 0; x < 100;)
            {
                for (int y = 0; y < 10; y++)
                {
                    spriteBatch.Draw(snowTex, snowRect[y], Color.White);
                }
                x += 10;
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }

 

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, dialgarocksful said:

 



for (int x = 0; x < 100;)
{
  for (int y = 0; y < 10; y++)
  {
  	spriteBatch.Draw(snowTex, snowRect[y], Color.White);
  }
  x += 10;
}
  

 

You're calling draw on the first 10 items in the array 10 times each. 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

You're calling draw on the first 10 items in the array 10 times each. 

how should i do it? any recommendations you have?

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, dialgarocksful said:

how should i do it? any recommendations you have?

Use a single loop that goes from 0 to 100. You're breaking things up into batches of 10 but doing the same thing to every element anyway.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, fizzlesticks said:

Use a single loop that goes from 0 to 100. You're breaking things up into batches of 10 but doing the same thing to every element anyway.

it gave me a single line of 100 textures. Now, how do I fix this particles into batches of 10 or <insert number here> per instance?

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

why do you want to break into batches of ten?

why arent you doing this in the gpu?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SCHISCHKA said:

why do you want to break into batches of ten?

why arent you doing this in the gpu?

trying to create a batch-of-10 snow drop every 2 seconds in a random x position.

 

what gpu?

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, dialgarocksful said:

what gpu?

hlsl programming

 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SCHISCHKA said:

hlsl programming

 

i cant do that yet. I'm only limited to 2D games, so most likely the particles the player will be seeing is just a 4x4 pixel snow image that spawns from top to bottom in batches of 10

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

Anyways, I found the answer. Turns out, I also need to set the Y-position in the Update method

 

protected override void Update(GameTime gameTime)
{
            
	for (arrayCtr = 0; arrayCtr < snowRect.Length; arrayCtr++)
	{
		randY[arrayCtr]++;
        if (snowRect[arrayCtr].Y > 770) 
        {
			randX[arrayCtr] = rnd.Next(0, 1366);
			randY[arrayCtr] = -5;
        }
        snowRect[arrayCtr] = new Rectangle(randX[arrayCtr], randY[arrayCtr], 10, 10);
    }

    base.Update(gameTime);
}

 

 

Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×