Need help with simple wave motion algorithm.
Hey,
I am having a bit of trouble getting a simple wave motion algorithm together....
Ok, I have a 2d wave sprite that starts at Xcoord -53 and then moves to Xcoord 23, then goes back to -53, etc. etc..
Now, what I would like is for the wave to slowly pick up speed at the beginning and slowly come to a halt before changing directions as to simulate a smooth wave motion...AND...take in consideration processor speeds so that the wave motion is basically the same speed on every computer.
I''ll try to paste the code I am using now to handle the wave motion to the right, below:
if (FrontWave->m_VelX == 1) //wave is moving to the right
{
if (FrontWave->m_TimeStamp != 0)
{
if (FrontWave->m_PosX == 93){FrontWave->m_Velocity=80;}
if (FrontWave->m_PosX >= 40 && (float(timeGetTime()-FrontWave->m_TimeStamp) > FrontWave->m_Velocity))
{
FrontWave->m_PosX--;
FrontWave->m_Velocity--;
FrontWave->m_TimeStamp=0;
}
if (FrontWave->m_PosX > 0 && FrontWave->m_PosX < 40 && (float(timeGetTime()-FrontWave->m_TimeStamp) > FrontWave->m_Velocity))
{
FrontWave->m_PosX--;
FrontWave->m_TimeStamp=0;
}
if (FrontWave->m_PosX <= 0 && (float(timeGetTime()-FrontWave->m_TimeStamp) > FrontWave->m_Velocity))
{
FrontWave->m_PosX--;
FrontWave->m_Velocity++;
FrontWave->m_TimeStamp=0;
}
if (FrontWave->m_PosX <= -53){FrontWave->m_PosX=-53;FrontWave->m_VelX=0;}
}
if (FrontWave->m_TimeStamp == 0){ FrontWave->m_TimeStamp = timeGetTime();}
}
.......If anyone has any tips they could offer I would appreciate it, Thanks
I would suggest using sin() since a sinusoide(?) has a wavy pattern...
----------
Drago
----------
Drago
osu!
If you want to use Drago''s suggestion, a formula would be:
Xcoord = -15 + 38 * sin (2.0 * M_PI * (time / time_per_wave));
If you''re intent on doing things by velocity rathar than absolute position you can use:
VelX = 38.0 * 2.0 * M_PI / time_per_wave * cos(2.0 * M_PI * (time / time_per_wave));
However using that could suffer from rounding error as time goes on, and only works if your frame rate is relatively high.
Xcoord = -15 + 38 * sin (2.0 * M_PI * (time / time_per_wave));
If you''re intent on doing things by velocity rathar than absolute position you can use:
VelX = 38.0 * 2.0 * M_PI / time_per_wave * cos(2.0 * M_PI * (time / time_per_wave));
However using that could suffer from rounding error as time goes on, and only works if your frame rate is relatively high.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement