Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] sin(x) going too fast to be effective  (Read 1216 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345
« Posted 2013-01-14 03:13:52 »

After taking what I learned from Wavy lines in java thread and from trial and error i attempted to make a star go up and down but I am doing it with sin so it looks a little smoother; but anyway I am using this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
   // loop is going too fast so motion is not visible(is that really why?)... not sure what to do
  private static float starY = 0;

   public static void star(SpriteBatch batch) {
      batch.draw(Assets.star, 200, starY + 200);
      for (float x = 0; x < 361; x ++) {
         starY = MathUtils.sin(x) * 50;
         System.out.println(starY);
      }
   }

Now what I think the problem is, is that the foor loop is going to fast (does that even make sense) because I see no movement in the star that I am drawing. I printed out the values of the starY and they seem pretty scattered so I would believe this loop is going maybe too fast to work correctly. Also I multiplied sin(x) by 50 but I am not sure that is the correct way to do this... If you think the problem is just my lack of trig knowledge then I will go learn some more but if not is there any other option?
Offline ClickerMonkey

Senior Member


Medals: 11


Game Engineer


« Reply #1 - Posted 2013-01-14 04:03:12 »

Does MathUtils.sin expect radians or degrees?

Offline wreed12345
« Reply #2 - Posted 2013-01-14 04:16:48 »

I just looked at this page quick libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/math/MathUtils.html and it looks like radians
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline ClickerMonkey

Senior Member


Medals: 11


Game Engineer


« Reply #3 - Posted 2013-01-14 04:24:03 »

That could be your problem, your sending in values 0->360 when it should be 0.0->6.28.

Try:

1  
MathUtils.sin(x * PI / 180.0) * 50;

Offline wreed12345
« Reply #4 - Posted 2013-01-14 04:44:51 »

Can I still use that code in the for loop? Or are the values to high or does what you just said solve that?
Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #5 - Posted 2013-01-14 08:56:13 »

There is MathUtils.sinDeg(float degrees).

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #6 - Posted 2013-01-14 11:10:15 »

You guys are cruel. The OP clearly doesn't understand control flow, and you are explaining the parameters of a function. Whatever is fed into that function, it will never result in the animation he's trying to achieve.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline wreed12345
« Reply #7 - Posted 2013-01-14 12:42:58 »

@Riven
Do you think I should go learn some more trig from khanacademy, would that help? Or you just think my lack of java knowledge of doing this to me
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #8 - Posted 2013-01-14 13:26:31 »

Learn to walk before attempting to run.

Learn java first, because even with the best understanding of sin(...) in the world, you wouldn't get it to work, without knowing how Java works.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline RobinB
« Reply #9 - Posted 2013-01-14 13:41:37 »

1  
2  
3  
4  
5  
6  
7  
   public static void star(SpriteBatch batch) {
      batch.draw(Assets.star, 200, starY + 200);
      for (float x = 0; x < 361; x ++) {
         starY = MathUtils.sin(x) * 50;
         System.out.println(starY);
      }
   }


gives the same animation as this
1  
2  
3  
4  
   public static void star(SpriteBatch batch) {
      batch.draw(Assets.star, 200, starY + 200);
                starY = MathUtils.sin(360) * 50;
   }


the for loop just prints some stuff, but does not help with your animation
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2013-01-14 14:02:55 »

to say it simply: you're going through 360 degrees in one frame
meaning after the picture was rendered, you are calculating your rotation values, all of them 0-360 and then you render again
so its going "too fast" isnt wrong, in fact its instantaneous

what you want to do is change the starY one everyframe, and increment your x also only every frame
if you do x++ every frame, you would need 360 frames for one revolution, hence 6 seconds in 60 fps - this is with degrees, when using radians you just have it use different values. in that case just try something like x+=0.01

Offline wreed12345
« Reply #11 - Posted 2013-01-14 23:23:48 »

1  
2  
3  
4  
   public static void star(SpriteBatch batch) {
      batch.draw(Assets.star, 200, starY + 200);
                starY = MathUtils.sin(360) * 50;
   }

This code does not create an animation, as the star is still not moving.
Offline wreed12345
« Reply #12 - Posted 2013-01-14 23:28:50 »

what you want to do is change the starY one everyframe, and increment your x also only every frame
if you do x++ every frame, you would need 360 frames for one revolution, hence 6 seconds in 60 fps - this is with degrees, when using radians you just have it use different values. in that case just try something like x+=0.01
I am not sure if this is what you mean. but i tried this:
1  
2  
3  
4  
5  
   public static void star(SpriteBatch batch) {
      for (float x = 0f; x < 361; x += .01f) {
         batch.draw(Assets.star, 200, MathUtils.sinDeg(x) * 50 );
      }
   }

one major problem is this greatly lags out the game. and the animation isnt fluid; it is just a solid image of where the star would move
EDIT: here are some images of whats going on:
here is the normal star image without any movement

here it is using this code
Offline Jimmt
« Reply #13 - Posted 2013-01-14 23:36:47 »

ofc it lags, that for loop is iterating 36100 times per method call.
Offline wreed12345
« Reply #14 - Posted 2013-01-14 23:38:26 »

How can I achieve this without lag?
Offline Varkas

JGO Knight


Medals: 14
Projects: 5


iDream


« Reply #15 - Posted 2013-01-14 23:46:29 »

1  
for (float x = 0f; x < 361; x += .01f)


Maybe it will work better with a bigger increment? It seems you do this with steps of 1/100 of a degree, that is a very small angle increment, and you loop over the full 360 degrees (even a bit more ...)

if (error) throw new Brick();
Offline wreed12345
« Reply #16 - Posted 2013-01-14 23:48:16 »

I was just trying that because someone had suggested it. If i increase the number there is still no animation but the blob of stars becomes less
Offline Varkas

JGO Knight


Medals: 14
Projects: 5


iDream


« Reply #17 - Posted 2013-01-14 23:51:28 »

If I understood the problem correctly you need to move only a few degrees, then display the frame, and then do some more degrees, display a frame again ... and so on.

You must store the current angle in between the frames so you can start the loop next frame where it ended for the former frame.

(I did not really read all of this thread, sorry. Just guessing.)

if (error) throw new Brick();
Offline wreed12345
« Reply #18 - Posted 2013-01-15 00:13:52 »

so only use the for loop for like 10 degrees display the image then do it again?
Offline actual

JGO Coder


Medals: 19



« Reply #19 - Posted 2013-01-15 00:27:57 »

You don't need a for loop. What you want to do is each frame to move it just a tad .

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// Holds the angle of the star through the sin wav.
float starAngle;

// Draws the star (called once per time step.
public static void star(SpriteBatch batch) {
  // Draw the star at its current position
  batch.draw(Assets.star, 200, MathUtils.sinDeg(starAngle) * 50 );

  // Increment the angle a bit
  starAngle += 0.05;
}


This isn't perfect but it should look closer to what you want.
Offline wreed12345
« Reply #20 - Posted 2013-01-15 00:34:42 »

well thank you guys for the help; this code seems to be working great  Cheesy
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   private static float x = 0;
   private static float derp = 0;
   
   public static void star(SpriteBatch batch) {
      for (x = derp; x < derp + 10; x += 1f) {
         batch.draw(Assets.star, 200, (MathUtils.sinDeg(x) * 50) +200 );
      }
      derp += 10;
      if (derp > 360){
         derp = 0;
      }
   }

please excuse the naming of the derp variable lol
Offline actual

JGO Coder


Medals: 19



« Reply #21 - Posted 2013-01-15 00:54:55 »

You may be getting the effect you want in this specific case, but this is not how you should be doing it. Common practice is to draw an object only once per time step.  You, on the other hand, are calling batch.draw 10 times every single time you call star.

This will work better. you aren't wasting resources drawing the same thing over and over again and you don't need to hold onto the x variable.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
   
   private static float derp = 0;
   
   public static void star(SpriteBatch batch) {
         batch.draw(Assets.star, 200, (MathUtils.sinDeg(derp) * 50) +200 );

      derp += 10;   // if it is waving too quickly, increment derp my less.
     if (derp > 360){
         derp = 0;
      }
   }

Offline wreed12345
« Reply #22 - Posted 2013-01-15 01:35:42 »

Thanks actual! That code does make more sense then my for loop *face palm* thanks again Cool
Offline RobinB
« Reply #23 - Posted 2013-01-15 11:50:55 »

1  
2  
3  
4  
   public static void star(SpriteBatch batch) {
      batch.draw(Assets.star, 200, starY + 200);
                starY = MathUtils.sin(360) * 50;
   }

This code does not create an animation, as the star is still not moving.

Yeah thats what i sayd Smiley
Its for you to figure out .

Ah the guy above me spoiled it already.
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (77 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (179 views)
2013-05-12 17:10:36

kutucuk (179 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.124 seconds with 21 queries.