[SOLVED] sin(x) going too fast to be effective

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:


	// 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?

Does MathUtils.sin expect radians or degrees?

I just looked at this page quick libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/math/MathUtils.html and it looks like radians

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

Try:

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

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

There is MathUtils.sinDeg(float degrees).

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.

@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

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.


	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


	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

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

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

I am not sure if this is what you mean. but i tried this:


	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

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

How can I achieve this without lag?

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 …)

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

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

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

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



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