Page 1 of 1

Sine oscillation speed

Posted: Thu May 01, 2014 7:19 pm
by Nubcake
I'm using the following code to create a wavy background effect of an image but the oscillation speed is too fast.

Code: Select all


Pi.d = 3.1415926535897931
Phase.d = 0;
Amplitude = 10;
Waves = 2;
Angle.d = 0;

;Displays each row of pixels in the image with an offset to create the effect
For Count = 0 To Rows
    DisplaySprite(Sprites(Count),x + (Amplitude * Sin(Phase + Waves*Angle)),y + Count)
    Angle + Pi/32
Next
How do I modify this so I can control the speed of the oscillation? If the angle increment is too small then the effect becomes less and less visible (just an oscillating image) , if it's too big then the effect becomes really bad ; just oscillating too fast.

Re: Sine oscillation speed

Posted: Mon May 05, 2014 4:06 am
by em_uk
Perhaps actual working code my help.



Thanks

Re: Sine oscillation speed

Posted: Mon May 05, 2014 9:37 am
by pjay
Do you need to lower the amount that your adding to your angle each scanline?

Code: Select all

InitSprite()
OpenWindow(0, 0, 0, 640, 480, "Sine wave", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
CreateSprite(0, 32, 1)
StartDrawing(SpriteOutput(0))
  LineXY(0,0,32,1,255)
StopDrawing()

Pi.d = 3.1415926535897931
Phase.d = 0;
Amplitude = 100;
Waves = 2;
Angle.d = 0;

Repeat
  Repeat
    Event = WindowEvent()
    Select Event 
      Case #PB_Event_CloseWindow
        End 
    EndSelect
  Until Event = 0
  
  Rows = 40
  X = 320
  Y = 50
  Phase + Pi/80.0
  Angle = Phase
  For Count = 0 To Rows
    DisplaySprite(0,x + (Amplitude * Sin(Phase + Waves*Angle)),y + Count)
    Angle + Pi/80.0
  Next
  
  FlipBuffers() 
  ClearScreen(RGB(0, 0, 0))
ForEver

Re: Sine oscillation speed

Posted: Mon May 05, 2014 10:50 am
by Demivec
@pjay: Thanks for the code sample. :wink:


@Numbcake: You can control the speed of the oscillations by modifying how fast the Phase changes. Smaller changes means the oscillations are slower.

In pjay's code sample it is found in this line:

Code: Select all

Phase + Pi/80.0 ;negative changes cause the waves to move down the screen instead of up