Page 1 of 1

help with basic particle code

Posted: Wed Oct 27, 2010 6:02 am
by J. Baker
I'm not sure why this will not work correctly? Any help would be great, thanks.

Code: Select all

Structure SnowCalculations
  SnowX.l
  SnowY.l
EndStructure

Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(100)
  
  SnowCount +1
  
    If SnowCount > 101
      SnowRandom = 1
    Else
      SnowRandom = 0
    EndIf

    If SnowRandom = 1
     Snow(SnowSprite)\SnowX = Random(800)
     Snow(SnowSprite)\SnowY = Random(600)
    EndIf
  
    If Snow(SnowSprite)\SnowY = 600 +32
      Snow(SnowSprite)\SnowY = -32
      Snow(SnowSprite)\SnowX = Random(800)
     Else
       DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\SnowX, Snow(SnowSprite)\SnowY)
    EndIf
    
  Snow(SnowSprite)\SnowY +1
    
EndProcedure

InitSprite()

;InitKeyboard()

UsePNGImageDecoder()

WinWidth = 800
WinHeight = 600

OpenWindow(0,0,0,WinWidth,WinHeight,"Snow Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

OpenWindowedScreen(WindowID(0),0,0,WinWidth,WinHeight,0,0,0)

 For Snow = 1 To 100
   CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
 Next Snow

MaxSnow = 0  
quit=0
;SnowRandom = 1


Repeat
  
  Repeat
    ev=WindowEvent()
    If ev=#PB_Event_CloseWindow
      quit=1
    EndIf
  Until ev=0
  
  ;ExamineKeyboard()
  
    For Snow = 1 To 100
      
      SnowFlakes(Snow)
  
    Next Snow
  
  FlipBuffers() 
  ClearScreen(0)
  
Until quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection

Re: help with basic particle code

Posted: Wed Oct 27, 2010 10:24 am
by infratec
Hi,

first of all I think something like

Code: Select all

Static SnowCount = 0
is missing in the SnowFlakes() procedure.

Code: Select all

SnowCount + 1 
Should be inside the else part. Else it sometimes come back to < 101

And than:
What do you want :?:

Without an exact explanation what the result should be, no one can adjust the code.

Bernd

Re: help with basic particle code

Posted: Wed Oct 27, 2010 10:40 am
by infratec
Maybe you want this

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100

Structure SnowCalculations
  X.i
  Y.i
  S.i
EndStructure


Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
  Static SnowCount = 0
  
  If SnowCount < #MaxFlakes
    Snow(SnowSprite)\X = Random(#WinWidth)
    Snow(SnowSprite)\Y = Random(#WinHeight) - #WinHeight
    Snow(SnowSprite)\S = Random(2) + 1
    SnowCount + 1
  EndIf
  
  If Snow(SnowSprite)\Y > #WinHeight + 32
    Snow(SnowSprite)\Y = -32
    Snow(SnowSprite)\X = Random(#WinWidth)
    Snow(SnowSprite)\S = Random(2) + 1
  Else
    DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\X, Snow(SnowSprite)\Y)
  EndIf
  
  Snow(SnowSprite)\Y + Snow(SnowSprite)\S
   
EndProcedure


UsePNGImageDecoder()
InitSprite()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes : CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending) : Next Snow

Quit = #False

Repeat
  
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow : Quit = #True : EndIf
  Until Event = 0
  
  FlipBuffers()
  ClearScreen(0)
  
  For Snow = 1 To #MaxFlakes : SnowFlakes(Snow) : Next Snow
  
  Delay(5)
  
Until Quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection
Best regards,

Bernd

Re: help with basic particle code

Posted: Wed Oct 27, 2010 2:06 pm
by J. Baker
Thanks infratec! Yes I was originally just trying to get them to fall properly but you also added the random speed for me. I see what I wasn't doing correctly now. Thanks again! ;)

Re: help with basic particle code

Posted: Wed Oct 27, 2010 2:55 pm
by infratec
Hi,

you are welcome :!:

And only for fun I added a start/stop function (cursor up/down) :mrgreen: :mrgreen: :mrgreen:

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100
#SpriteHeight = 32

Structure SnowCalculations
  X.i
  Y.i
  V.i
  S.i
EndStructure


Procedure SnowFlakes(SnowSprite, Status)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
    
  If Status 
    If Not Snow(SnowSprite)\S
      Snow(SnowSprite)\X = Random(#WinWidth)
      Snow(SnowSprite)\Y = Random(#WinHeight) - #WinHeight
      Snow(SnowSprite)\V = Random(2) + 1
      Snow(SnowSprite)\S = #True
    EndIf
  Else
    If Snow(SnowSprite)\Y < #SpriteHeight * -1
      Snow(SnowSprite)\Y = #WinHeight + #SpriteHeight
    EndIf
  EndIf
  
  If Snow(SnowSprite)\Y >= #WinHeight + #SpriteHeight
    If Status
      Snow(SnowSprite)\Y = #SpriteHeight * -1
      Snow(SnowSprite)\X = Random(#WinWidth)
      Snow(SnowSprite)\V = Random(2) + 1
    Else
      Snow(SnowSprite)\S = #False
    EndIf
  Else
    DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\X, Snow(SnowSprite)\Y)
  EndIf
  
  If Snow(SnowSprite)\S : Snow(SnowSprite)\Y + Snow(SnowSprite)\V : EndIf
   
EndProcedure


UsePNGImageDecoder()
InitKeyboard()
InitSprite()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes : CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending) : Next Snow

Quit = #False

SnowStatus = #True

Repeat
  
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow : Quit = #True : EndIf
  Until Event = 0
  
  ExamineKeyboard()
  
  If KeyboardReleased(#PB_Key_Up) : SnowStatus = #False : EndIf
  If KeyboardReleased(#PB_Key_Down) : SnowStatus = #True : EndIf
  
  FlipBuffers()
  ClearScreen(0)
  
  For Flake = 1 To #MaxFlakes : SnowFlakes(Flake, SnowStatus) : Next Flake
  
  Delay(5)
  
Until Quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection
Have fun,

Bernd

Re: help with basic particle code

Posted: Wed Oct 27, 2010 3:10 pm
by J. Baker
Pretty Cool! :D

I just need to make them Sprite3D() now. So that they can be zoomed and rotated. :wink:

Re: help with basic particle code

Posted: Wed Oct 27, 2010 8:03 pm
by J. Baker
We now have zoomed and rotating snow based on speed. :D

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100

Structure SnowCalculations
  SnowX.i
  SnowY.i
  SnowS.i
EndStructure

Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
  Static SnowCount = 0
 
  If SnowCount < #MaxFlakes
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowY = Random(#WinHeight) - #WinHeight
    Snow(SnowSprite)\SnowS = Random(2) + 1
    SnowCount + 1
  EndIf
 
  If Snow(SnowSprite)\SnowY > #WinHeight + 32
    Snow(SnowSprite)\SnowY = -32
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowS = Random(2) + 1
  Else
    Start3D()
     ZoomSprite3D(SnowSprite, Snow(SnowSprite)\SnowS * 8, Snow(SnowSprite)\SnowS * 8)
     DisplaySprite3D(SnowSprite, Snow(SnowSprite)\SnowX, Snow(SnowSprite)\SnowY)
     RotateSprite3D(SnowSprite, 0.2 * Snow(SnowSprite)\SnowS, #PB_Relative)
    Stop3D()
  EndIf
 
  Snow(SnowSprite)\SnowY + Snow(SnowSprite)\SnowS
   
EndProcedure

UsePNGImageDecoder()
InitSprite()
InitSprite3D()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes
  CatchSprite(Snow, ?Pic0, #PB_Sprite_Texture | #PB_Sprite_AlphaBlending)
  CreateSprite3D(Snow, Snow)
Next Snow

Quit = #False

Repeat
 
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      Quit = #True
    EndIf
  Until Event = 0
 
  FlipBuffers()
  ClearScreen(0)
 
  For Snow = 1 To #MaxFlakes
    SnowFlakes(Snow)
  Next Snow
 
  Delay(5)
 
Until Quit

DataSection
Pic0: IncludeBinary "flake.png"
EndDataSection

Re: help with basic particle code

Posted: Wed Oct 27, 2010 10:47 pm
by J. Baker
With a little more tweaking, I got pretty much what I was looking for.
http://www.flashpulse.com/purebasic/Snow%20Test.zip

Re: help with basic particle code

Posted: Sun Nov 24, 2013 12:10 am
by rootuid
I compiled but got an error stating that start3d() is not a valid function. Is InitEngine3D the new version of this function ?

Re: help with basic particle code

Posted: Sun Nov 24, 2013 12:44 am
by J. Baker
rootuid wrote:I compiled but got an error stating that start3d() is not a valid function. Is InitEngine3D the new version of this function ?
This code hasn't been updated for the newest version of PureBasic. There aren't any Sprite3D commands as they all have been updated to the normal Sprite commands. Check out the help file. ;)

Re: help with basic particle code

Posted: Sun Nov 24, 2013 1:05 am
by rootuid
Thanks for the heads up J. Baker !

Re: help with basic particle code

Posted: Sun Nov 24, 2013 1:43 am
by J. Baker
rootuid wrote:Thanks for the heads up J. Baker !
Got a little time so I quickly edited the code for PureBasic v5.21. Alter to your liking. ;)

Code: Select all

#WinWidth = 800
#WinHeight = 600
#MaxFlakes = 100

Structure SnowCalculations
  SnowX.i
  SnowY.i
  SnowS.i
EndStructure

Procedure SnowFlakes(SnowSprite)

  Static Dim Snow.SnowCalculations(#MaxFlakes)
  Static SnowCount = 0
 
  If SnowCount < #MaxFlakes
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowY = Random(#WinHeight) - #WinHeight
    Snow(SnowSprite)\SnowS = Random(2) + 1
    SnowCount + 1
  EndIf
 
  If Snow(SnowSprite)\SnowY > #WinHeight + 32
    Snow(SnowSprite)\SnowY = -32
    Snow(SnowSprite)\SnowX = Random(#WinWidth)
    Snow(SnowSprite)\SnowS = Random(2) + 1
  Else
     ZoomSprite(SnowSprite, Snow(SnowSprite)\SnowS * 8, Snow(SnowSprite)\SnowS * 8)
     DisplayTransparentSprite(SnowSprite, Snow(SnowSprite)\SnowX, Snow(SnowSprite)\SnowY)
     RotateSprite(SnowSprite, 0.2 * Snow(SnowSprite)\SnowS, #PB_Relative)
  EndIf
 
  Snow(SnowSprite)\SnowY + Snow(SnowSprite)\SnowS
   
EndProcedure

UsePNGImageDecoder()
InitSprite()

OpenWindow(0, 0, 0,#WinWidth, #WinHeight, "Snow Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #WinWidth, #WinHeight, 0, 0, 0)

For Snow = 1 To #MaxFlakes
  CatchSprite(Snow, ?Pic0, #PB_Sprite_AlphaBlending)
Next Snow

Quit = #False

Repeat
 
  Repeat
    Event = WindowEvent()
    If Event = #PB_Event_CloseWindow
      Quit = #True
    EndIf
  Until Event = 0
 
  FlipBuffers()
  ClearScreen(0)
 
  For Snow = 1 To #MaxFlakes
    SnowFlakes(Snow)
  Next Snow
 
  Delay(5)
 
Until Quit

DataSection
  Pic0:
   IncludeBinary "flake.png"
EndDataSection