# Sine Wave Sprite Movement

This topic is 2595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I want to move the sprite in a sine wave pattern. For that i am trying
is for example if the screen resolution is (800,600), i am taking :

X = 800 and then generate Y = Sin(X) and gradually decreasing the X--;

For the movement i am doing

 Sprite->Move(-X * GetFrameTime(), Y); 

But the problem is that the movement is not smooth and the sprite flickers while moving.
How can i get a smooth sine wave movement with high steep along Y axis as for the present values the upward movement is very small.
I tried to scale the Y value with some factor like 10.0, but still its very jerky movement.

All suggestions are welcome

##### Share on other sites
What are you using for rendering? What do you mean flickers? Do the actual graphic flicker or do you mean the path that the sprite is following, if you could post the whole code that calculates the values that'd be helpful.

Thanks.

##### Share on other sites

What are you using for rendering? What do you mean flickers? Do the actual graphic flicker or do you mean the path that the sprite is following, if you could post the whole code that calculates the values that'd be helpful.

Thanks.

You need some understanding of the sine function. First check out this site: http://www.intmath.c...e-amplitude.php

If you want a steeper amplitude (height) of the wave you have to do something like:
y = amplitude * sin(x).

Also remember that typically the x is in radians, so you you might try something like:
y = amplitude * sin(x * pi / 180)

Now note that you can also scale the x factor up or down, which will increase the wave length.. if you divide x by some factor you'll get a longer wave. If you multiply it you'll get a shorter wave.

See this site for how you can adjust the period of the wave: http://www.intmath.c...sine-period.php

This would make your formula something like this:
y = amplitude * sin(x * period * pi / 180)

Try amplitude = 400 and period = 3 and see how things look. Also, for your sprite movement I'm not 100% sure what you're doing with your code with GetFrameTime(). I assume it's a double/float that just increments. Just make sure that if you want a smooth look you have to make sure X is scaling smoothly as well.

##### Share on other sites
I am using SFML library. It has a inbuild function for sprite movement which is the MOVE(), which is
called as Sprite->Move(X,Y);

Move() function adds the previous position to the (X,Y) value. its like SpritePosition +=X;

Flicker means jerky movement, its not smooth, like a wave transition.

##### Share on other sites
You are accumulating sine values. Consider using something like SetPosition() or SetY() (or whatever function) instead of a Move() function.

##### Share on other sites

You are accumulating sine values. Consider using something like SetPosition() or SetY() (or whatever function) instead of a Move() function.

Actually, the integral of sin() is cos(), so he'll get a cos() wave for his movement (which is really just a sin wave offset by half a cycle-thingie).

Of course you're still right, since it will be easier to reason about the movement if he uses a SetPosition()-like function. Also the movement won't depend on timestep in that case (if his timestep is not fixed).

##### Share on other sites

What are you using for rendering? What do you mean flickers? Do the actual graphic flicker or do you mean the path that the sprite is following, if you could post the whole code that calculates the values that'd be helpful.

Thanks.

You need some understanding of the sine function. First check out this site: http://www.intmath.c...e-amplitude.php

If you want a steeper amplitude (height) of the wave you have to do something like:
y = amplitude * sin(x).

Also remember that typically the x is in radians, so you you might try something like:
y = amplitude * sin(x * pi / 180)

Now note that you can also scale the x factor up or down, which will increase the wave length.. if you divide x by some factor you'll get a longer wave. If you multiply it you'll get a shorter wave.

See this site for how you can adjust the period of the wave: http://www.intmath.c...sine-period.php

This would make your formula something like this:
y = amplitude * sin(x * period * pi / 180)

Try amplitude = 400 and period = 3 and see how things look. Also, for your sprite movement I'm not 100% sure what you're doing with your code with GetFrameTime(). I assume it's a double/float that just increments. Just make sure that if you want a smooth look you have to make sure X is scaling smoothly as well.
[/quote]

I understand the sine function, I was mearly asking if I could see the whole code that he was using to calculate the position of his sprite and what you he was using to render in hope to possibly answer why the movement was jerky.

I am unfamiliar with the SFML library it would still be nice to see the code which you are using to calculate the position along your sine wave (not because I dont understand it, I just want to see if there is anything that could be affecting the movement). I cant speak for the performance of the library you are using are you sure it can provide quick enough rendering for what you need?

Thanks.

##### Share on other sites

Actually, the integral of sin() is cos(), so he'll get a cos() wave for his movement (which is really just a sin wave offset by half a cycle-thingie).
[/quote]
I didn't even consider that. The problem might actually be that the OP is passing an absolute value for X, but this is accumulating, resulting in the sprite jerking horizontally, even if it is in a smooth cos() wave. Another potential issue is sin() takes radians. This shouldn't make a huge difference, it should merely alter the curve that the OP might expect to see.

I cant speak for the performance of the library you are using are you sure it can provide quick enough rendering for what you need?
[/quote]
Should be more than capable.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×