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
| 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?
|
|
|
|
|
ClickerMonkey
|
 |
«
Reply #1 - Posted
2013-01-14 04:03:12 » |
|
Does MathUtils.sin expect radians or degrees?
|
|
|
|
wreed12345
|
 |
«
Reply #2 - Posted
2013-01-14 04:16:48 » |
|
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ClickerMonkey
|
 |
«
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; |
|
|
|
|
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?
|
|
|
|
|
Nate
|
 |
«
Reply #5 - Posted
2013-01-14 08:56:13 » |
|
There is MathUtils.sinDeg(float degrees).
|
|
|
|
Riven
|
 |
«
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.
|
|
|
|
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
|
|
|
|
|
Riven
|
 |
«
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.
|
|
|
|
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!
|
|
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
|
|
|
|
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.
|
|
|
|
|
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 
|
|
|
|
|
Jimmt
|
 |
«
Reply #13 - Posted
2013-01-14 23:36:47 » |
|
ofc it lags, that for loop is iterating 36100 times per method call.
|
|
|
|
|
wreed12345
|
 |
«
Reply #14 - Posted
2013-01-14 23:38:26 » |
|
How can I achieve this without lag?
|
|
|
|
|
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();
|
|
|
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
|
|
|
|
|
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();
|
|
|
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?
|
|
|
|
|
actual
|
 |
«
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
| float starAngle;
public static void star(SpriteBatch batch) { batch.draw(Assets.star, 200, MathUtils.sinDeg(starAngle) * 50 );
starAngle += 0.05; } |
This isn't perfect but it should look closer to what you want.
|
|
|
|
|
wreed12345
|
 |
«
Reply #20 - Posted
2013-01-15 00:34:42 » |
|
well thank you guys for the help; this code seems to be working great  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
|
|
|
|
|
actual
|
 |
«
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 (derp > 360){ derp = 0; } } |
|
|
|
|
|
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 
|
|
|
|
|
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  Its for you to figure out . Ah the guy above me spoiled it already.
|
|
|
|
|
|