Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 2251 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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

JGO Coder


Medals: 20


Game Engineer


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

Does MathUtils.sin expect radians or degrees?

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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!
Legends of Yore - The Casual Retro Roguelike
Offline ClickerMonkey

JGO Coder


Medals: 20


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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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 Kernel


Medals: 145
Projects: 4
Exp: 14 years


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: 757
Projects: 4
Exp: 16 years


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
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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: 757
Projects: 4
Exp: 16 years


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
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« 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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #13 - Posted 2013-01-14 23:36:47 »

ofc it lags, that for loop is iterating 36100 times per method call.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #14 - Posted 2013-01-14 23:38:26 »

How can I achieve this without lag?

Offline Varkas
« 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(); // Blog (german): http://gedankenweber.wordpress.com
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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
« 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(); // Blog (german): http://gedankenweber.wordpress.com
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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: 23



« 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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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: 23



« 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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« 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

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


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

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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