Wavy lines in java?

Maybe. But i ACTUALLY did a snowflake code last week, using sin() for the oscillation:

	public class Flakes {
		public float x, y;
		public float gravity = 0.2F;
		public int randomMotion = RNG.getRNG(1,8);
		public Flakes(int x, int y) {
			this.x = x;
			this.y = y;
		}
		
		public void tick() {
			this.y += gravity;
			if (Game.instance.ticks % randomMotion == 0) this.x += Math.sin((Game.instance.ticks + (randomMotion * 10)) / 20) / 4;
		}
		
		public void render() {
			Game.instance.raster.fillSquare((int)this.x, (int)this.y, 2, 2, 0xDDDDDD);
		}
	}

randomMotion is there to assure roughly 1/8 of all the falling snowflakes are swinging at the same rate and time, which gives it the needed randomness.
And i must say it looks pretty good.

Nice. I just like staying away from sine/cos/trig as much as possible because it is slow.

Mayhaps a screen? I love eye candy. ;D

what would u use instead of sin/cos/tan ??

Did even read what the other people posted? I remember seeing at least 1 solution with code provided.

I do like the simplicity of this but the result is straight lines :frowning: it lacks the randomness of the example in the orginal post

you dont ACTUALLY use sin in real time, for games, one mostly uses the fast variations which are pre calculated values

You can find these FastMath methods anywhere

… Wanna bet? I can do 300 000 sin() calls in 16ms.

That’s actually pretty slow O___O

Well, if you want Random snow falling, then you’ll have to be a little bit more creative. You still don’t need Trig to accomplish it either, but you will need a little bit of help from java.util.Random.



import java.util.Random;

public class Snowflake{

      //Holds the current oscillation value for the snowflake
      public int oscillateValue = 5;

      //The horizontal position of the snowflake
      private int posx;
      //The vertical position of the snowflake
      private int posy;
      //The horizontal oscillation of the snowflake
      private int oscillationx;
      //This is used to randomly move the snowflake
      private Random rand;
      //Whether a snowflake will move left or right
      public boolean moveLeft;

      public Snowflake(int x, int y){
              posx = x;
              posy = y;
              oscillationx = 0;
              rand = new Random();
      }

      public void fall(){
           posy = posy + 1;
      }

      public void fallRandom(){
              fall();
              if(rand.nextBoolean())
                     posx = posx + 1;
              else
                     posx = posx - 1;
      }

      public void fallOscillate(){
              fall();
              if(moveLeft) {
                     oscillationx = oscillationx - 1;
                      if( oscillationx < -oscillateValue )
                              moveLeft = false;
              }
              else{
                     oscillationx = oscillationx + 1;
                     if( oscillationx > oscillateValue )
                              moveLeft = true;
              }
      }

     public void changeOscillation(int value){
              osicallateValue = value;
     }

      public int getPositionX(){
             return posx + oscillationx;
      }

      public int getPositionY(){
             return posy;
      }
}

This is pretty complete, and also is extendable. You can even add the sin/cos/tan? calculations to this class when you are ready to achieve all the different snowflake formations that are available. Feel free to change the value names or whatever, but this should cover all types of falling snow with relative ease.

EDIT: Fixed

If you don’t want straight lines then either use sin() or square the velocity (increase the increase of the speed)

		long start = System.nanoTime();
		for (Flakes f : flakes) f.tick();
		System.out.println("Ran through " + flakes.size() + " objects. Time: " + (System.nanoTime() - start) / 1000 / 1000D + "ms");
Ran through 400 objects. Time: 0.022ms
Ran through 400 objects. Time: 0.051ms
Ran through 400 objects. Time: 0.03ms
Ran through 400 objects. Time: 0.051ms
Ran through 300000 objects. Time: 9.06ms
Ran through 300000 objects. Time: 5.218ms
Ran through 300000 objects. Time: 5.738ms
Ran through 300000 objects. Time: 3.594ms

Well, it isn’t calling sin() 300 000 times, but…
Pathetic weakling!

@StumpyTrust: I might upload an applet soon, i tried to make a gif of it but it’s too much work :frowning:

Here’s a Python+PyGame script I whipped up to test an idea I had:
http://pastebin.java-gaming.org/af965444e35

It works by storing a position and velocity for each snowflake. the horizontal velocity is changed by a small random amount each frame and the vertical velocity is set to a random value at the start and left the same. In order to stop the snowflakes from moving too fast I put in a friction multiplier which the horizontal velocity gets multiplied by each frame (i.e. if it is 0.5 then the velocity halves each frame). I also added a global random number which gets added to all snowflakes to simulate wind moving them in sync. You can change the three numbers on lines 4, 5, and 6 to get different results.

Here’s the most important bit of code which makes the snowflakes move:


    globalPush = globalPushMult*(random.random() - 0.5)
    
    for i in range(0,len(xpos)):
        pygame.draw.circle(windowSurfaceObj, clrWhite, (int(xpos[i]), int(ypos[i])), 2, 0)
        xpos[i]+=xvel[i]
        ypos[i]+=yvel[i]
        
        xvel[i]+=globalPush+xPushMult*(random.random()-0.5)
        xvel[i]*=frictionMult

Here is a quick little test. Not sure if you can see the snow or not as I have 2 monitors and they show things differently. There is also not global wind which helps create unity.

xopUMemmNXg

I wanted to try ur example but there were many variables that are undefined and im very confused of what to change them to, or if Im supposed to define them. any ideas?
this was the ex

 //Holds the current oscillation value for the snowflake
      public int oscillateValue = 5;

      //The horizontal position of the snowflake
      private int posx;
      //The vertical position of the snowflake
      private int posy;
      //The horizontal oscillation of the snowflake
      private int oscillationx;
      //This is used to randomly move the snowflake
      private Random rand;
      //Whether a snowflake will move left or right
      public boolean moveLeft;

      public Snowflake(int x, int y){
              posx = x;
              posy = y;
              oscillation = 0;
              rand = new Random();
      }

      public void fall(){
           posy = posy + 1;
      }

      public void fallRandom(){
              fall();
              if(rand.nextBoolean())
                     posx = posx + 1;
              else
                     posx = posx - 1;
      }

      public void fallOscillate(){
              fall();
              if(moveLeft) {
                     oscillationx = oscillation - 1;
                      if( oscillationx < -oscillateValue )
                              moveLeft = false;
              }
              else{
                     oscillationx = oscillationx + 1;
                     if( oscillationx > oscillateValue )
                              moveLeft = true;
              }
      }

     public void changeOscillation(int value){
              osicallate = value;
     }

      public int getPositionX(){
             return posx + oscillationx;
      }

      public int getPositionY(){
             return posy;
      }

how do i make something like this?? :o

link=topic=28086.msg255081#msg255081 date=1355795571]

how do i make something like this?? :o
[/quote]
He’s using parallax (I think) which you probably shouldn’t be worrying about.[quote author=wreed12345

Uhhh… the Snowflake class is a self-contained class. Which basically means that you just have to use it as is. If you just take the variables out it will not work. You have to use the functions like you would a Math or String. Like…


Snowflake flake = new Snowflake(-10, 10);
//etc...

I mean the value of oscilation is never defined. Do i have to define it as an int? there are also a couple of other probs like that

Oops… my mistake. Let me fix all the errors in that post. I didn’t actually test it out as I meant it for an example, but since you want to use it, I’ll be more thorough.

Thank you very much i really appreciate your help