Sine oscillation speed

Just starting out? Need help? Post your questions and find answers here.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Sine oscillation speed

Post 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.
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: Sine oscillation speed

Post by em_uk »

Perhaps actual working code my help.



Thanks
----

R Tape loading error, 0:1
pjay
Enthusiast
Enthusiast
Posts: 251
Joined: Thu Mar 30, 2006 11:14 am

Re: Sine oscillation speed

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Sine oscillation speed

Post 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
Post Reply