Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2] 3
  ignore  |  Print  
  Wavy lines in java?  (Read 5726 times)
0 Members and 1 Guest are viewing this topic.
Offline sproingie

JGO Kernel


Medals: 201



« Reply #30 - Posted 2012-12-15 07:10:40 »

Back to math: you might want to wander on over to http://khanacademy.org and check out the geometry and trig lessons.  You're going need some basic trig to do any 2d game dealing with angles, waves, that sort of thing, and some basic vector and matrix arithmetic for any 3d game.
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #31 - Posted 2012-12-15 11:30:47 »

Dude it's like 2 lines of code, literally
If you can't solve small problems like this without giant amounts of help from others then you shouldn't be making games.

:/ I guess this is too demotivating...
Yes. He already pointed out, he's just in middle school, and many others are too.
Even if you don't know trigonometry you are still able to make games.
And we didn't have trigonometry in math until this grade, and we only talked about sin, cos and tan yet. No vectors, and obviously no matrices. And I still understand vectors fully and am able to use matrices, even if I don't understand them perfectly.
And I am able to code games, no reason to demotivate somebody like that, really. He is probably just as old as you, Jimmt, what would think if you didn't know trigonometry and somebody said that to you? :/ You had to learn it yourself too, I guess.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Phibedy

Senior Member


Medals: 8



« Reply #32 - Posted 2012-12-15 15:00:53 »

Heyho,
A vector describes the way from one point to another. You got a 3D world -> x,y,z axis.
The x-axis says how far something is west or east.
The y-axis says how far something is north or south.
The z-axis says how high something is. //That doesn't sound english  Roll Eyes

pi  = 3.14....
sin(x) is a wave that:
 -sin(0*pi/4) = 0
 -sin(1*pi/4) = 1
 -sin(2*pi/4) = 0
 -sin(3*pi/4) = -1
 -sin(4*pi/4) = 0
 
sin(alpha) = something between -1 and 1
sin(alpha-[whole Number]*Period) = sin(alpha) = something between -1 and 1  // Note 1

sin(alpha) = 0.5
<=> alpha = arcsin(0.5)
<=> alpha = 0.524
Have a look at "Note 1" -> Actualle the result is 0.524+2*k*pi // k instanceof whole numbers

cos(x)  wave:
cos(pi/4 - alpha) = sin(alpha)
cos(alpha) = sin( pi/4 - alpha)
tan-wave:
tan(alpha) = sin(alpha)/cos(alpha)

I don't know how much you already know, if you have any questions left, feel free to ask Smiley
best regards
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #33 - Posted 2012-12-15 15:51:30 »

I'm not understanding that completely... so what I want to do is continuously increase the y values by lets say 5. Then I want the x balls to have the curve. So would I use the sin function in the x values? And also this isn't a 3d game, just a simple 2d one

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #34 - Posted 2012-12-15 17:52:34 »

Dude it's like 2 lines of code, literally
If you can't solve small problems like this without giant amounts of help from others then you shouldn't be making games.

:/ I guess this is too demotivating...
Yes. He already pointed out, he's just in middle school, and many others are too.
Even if you don't know trigonometry you are still able to make games.
And we didn't have trigonometry in math until this grade, and we only talked about sin, cos and tan yet. No vectors, and obviously no matrices. And I still understand vectors fully and am able to use matrices, even if I don't understand them perfectly.
And I am able to code games, no reason to demotivate somebody like that, really. He is probably just as old as you, Jimmt, what would think if you didn't know trigonometry and somebody said that to you? :/ You had to learn it yourself too, I guess.
Maybe its different in your area, but in my school we learned sin, cos, and tan in 8th grade...limited trig, the rest I learned what I needed to.
Offline Sammidysam
« Reply #35 - Posted 2012-12-15 18:17:14 »

Back to math: you might want to wander on over to http://khanacademy.org and check out the geometry and trig lessons.  You're going need some basic trig to do any 2d game dealing with angles, waves, that sort of thing, and some basic vector and matrix arithmetic for any 3d game.


I forgot about that place.  It'll help once I get into trigonometry and other geometry.  Thanks.
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #36 - Posted 2012-12-16 03:37:01 »

Ugh. You don't even need sin(x) to fake a falling snowflake animation. What is this place coming to? *sigh*...

Seriously, all you need is a simple loop to oscillate between 2 points and represent a falling snowflake. Obviously the snowflake will be moving downward, so you need to keep the y constant, and make sure the x value oscillates (or moves back and forth between 2 values.) Here is some psuedo-code...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
//This is the starting horizontal value for the flake
public int x = 10;
//The starting vertical value - We want the snowflake to start off screen
public int y = -10;
//Whether a snowflake will move left or right
public boolean moveLeft;

public void snowflakeLoop(){

    y = y + 2;

    if(moveLeft) {
          x = x - 1;
          if( x < 5 )
                moveLeft = false;
    }
    else{
         x = x + 1;
         if( x > 15 )
                 moveLeft = true;
    }
}


Have the picture of the snowflake bind to this and running through that loop multiple times will achieve the snowflake effect without trig. What happened to the whole notion of simplicity in this forum? [/rant]

Offline Rorkien
« Reply #37 - Posted 2012-12-16 05:08:53 »

Simplicity isn't all about noobproof code.

And some old-wolves (75% of this forum, my optimistic guess) would say that using sin() is way simpler.
Offline sproingie

JGO Kernel


Medals: 201



« Reply #38 - Posted 2012-12-16 06:33:29 »

No, you don't need trig for simple oscillation, but knowing how sin and cos work is still going to be a rather big help in the long run ... and earlier.  Certainly it's easier to express wave motion with a single function than with all that state.
Offline StumpyStrust
« Reply #39 - Posted 2012-12-16 10:19:56 »

...I am not pro at anything but if you want to make snow, you do not need sines, oscillating or anything other then some objects the get a random x velocity and have a y velocity pulling them slow towards the ground. Oscillating would be for things like a feather falling. Most of the time snow is being blow all around or is going in a general direction but still very random as each flake is different. 

Now for that example it looks like the objects are going back and forth between directions which is not very nice looking but does suggest osculating and sines.

If you are having a hard time understanding suggestions then this is to advanced and you need to slow down a little. Tip on where to start. If you know some basic java, (can draw squares and such in a window) then try making stuff move around. Have an object that gets updated and moves. Maybe make it bounce against the screen. After that, play with the movement a little. Start small and work up.

What that example has is basically a particle system that plays more like an animation. This is not something you start out with. The reason why most people won't just give all the code is so you can learn how to do it. If we all just did our kids math problems for them, then they would never learn. Good luck and if you need a starting point just ask.  Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rorkien
« Reply #40 - Posted 2012-12-16 17:38:51 »

...I am not pro at anything but if you want to make snow, you do not need sines, oscillating or anything other then some objects the get a random x velocity and have a y velocity pulling them slow towards the ground. Oscillating would be for things like a feather falling. Most of the time snow is being blow all around or is going in a general direction but still very random as each flake is different. 

Now for that example it looks like the objects are going back and forth between directions which is not very nice looking but does suggest osculating and sines.

If you are having a hard time understanding suggestions then this is to advanced and you need to slow down a little. Tip on where to start. If you know some basic java, (can draw squares and such in a window) then try making stuff move around. Have an object that gets updated and moves. Maybe make it bounce against the screen. After that, play with the movement a little. Start small and work up.

What that example has is basically a particle system that plays more like an animation. This is not something you start out with. The reason why most people won't just give all the code is so you can learn how to do it. If we all just did our kids math problems for them, then they would never learn. Good luck and if you need a starting point just ask.  Smiley

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   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.
Offline StumpyStrust
« Reply #41 - Posted 2012-12-17 01:52:07 »

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.  Grin

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #42 - Posted 2012-12-17 02:09:12 »

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

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #43 - Posted 2012-12-17 02:10:51 »

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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #44 - Posted 2012-12-17 02:13:34 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
//This is the starting horizontal value for the flake
public int x = 10;
//The starting vertical value - We want the snowflake to start off screen
public int y = -10;
//Whether a snowflake will move left or right
public boolean moveLeft;

public void snowflakeLoop(){

    y = y + 2;

    if(moveLeft) {
          x = x - 1;
          if( x < 5 )
                moveLeft = false;
    }
    else{
         x = x + 1;
         if( x > 15 )
                 moveLeft = true;
    }
}

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

Offline Cero
« Reply #45 - Posted 2012-12-17 02:44:37 »

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

Offline theagentd
« Reply #46 - Posted 2012-12-17 03:16:21 »

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.

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #47 - Posted 2012-12-17 03:21:07 »

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

Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #48 - Posted 2012-12-17 03:30:43 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
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

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #49 - Posted 2012-12-17 03:36:19 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
//This is the starting horizontal value for the flake
public int x = 10;
//The starting vertical value - We want the snowflake to start off screen
public int y = -10;
//Whether a snowflake will move left or right
public boolean moveLeft;

public void snowflakeLoop(){

    y = y + 2;

    if(moveLeft) {
          x = x - 1;
          if( x < 5 )
                moveLeft = false;
    }
    else{
         x = x + 1;
         if( x > 15 )
                 moveLeft = true;
    }
}

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

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

Offline Rorkien
« Reply #50 - Posted 2012-12-17 03:36:44 »

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.

1  
2  
3  
      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");


1  
2  
3  
4  
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


1  
2  
3  
4  
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 Sad
Offline kramin42
« Reply #51 - Posted 2012-12-17 04:26:31 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
    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

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Offline StumpyStrust
« Reply #52 - Posted 2012-12-17 11:46:28 »

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. 

<a href="http://www.youtube.com/v/xopUMemmNXg?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/xopUMemmNXg?version=3&amp;hl=en_US&amp;start=</a>

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #53 - Posted 2012-12-18 02:51:37 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
 //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;
      }

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #54 - Posted 2012-12-18 02:52:51 »

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. 

how do i make something like this?? Shocked

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #55 - Posted 2012-12-18 02:58:42 »

link=topic=28086.msg255081#msg255081 date=1355795571]
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. 

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

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #56 - Posted 2012-12-18 03:05:55 »

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...

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

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #57 - Posted 2012-12-18 03:07:52 »

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...

1  
2  
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

Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #58 - Posted 2012-12-18 03:13:00 »

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.

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #59 - Posted 2012-12-18 03:13:42 »

Thank you very much i really appreciate your help

Pages: 1 [2] 3
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (22 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (54 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!