UnderwareDESIGN

General => Competitions => CleverCoders => Topic started by: kevin on December 18, 2010, 08:39:43 PM

Title: Challenge #22 - Snow Particle Effect
Post by: kevin on December 18, 2010, 08:39:43 PM


(http://www.underwaredesign.com/PlayBasicSig.png) (http://www.playbasic.com)


Challenge #22 - Snow Particle Effect



    With Christmas rapidly approaching,  this challenge is to create a particle effect that gives the viewer a convincing falling snow effect. 




Submission

* Submissions can be written in any version of PlayBASIC (http://www.playbasic.com)

* Submission can only be accepted in Source Code form only.

* Authors can use external Media (images/music) provided you created the media/and or have permission to distribute the includes media or the Media is in the public domain.

* Zip up your Submissions and please try to keep it smaller than 500K !

* Authors automatically give consent for their submission(s) to distributed and used for promotional purposes via UnderwareDesign.com, PlayBasic code tank.

* Authors can submit as many times as they like.

* To make a submission, zip up your projects source code + media and either post it in this thread, or create your own thread in either the Source Codes or Show Case forums and post a link bellow.



Dead Line

     Anytime before the next ice age



Prizes

     * The best submissions will be periodically highlighted through the PlayBasic IDE news service/News Letters or on Twitter (http://www.twitter.com/PlayBasic)




Return to Challenge Index (http://www.underwaredesign.com/forums/index.php?topic=3225.0)

Title: Re: Challenge #22 - Snow Particle Effect
Post by: daver on December 30, 2010, 01:48:04 PM
My first try at responding to one of these challenges, hope you like it. I ended up going with a simple algorithm for the snow effect, just to make sure I finished the project!

A couple of features:
D key - makes it day
N key - makes it night
+ on keypad - adds snowflakes
- on keypad - removes snowflakes

Title: Re: Challenge #22 - Snow Particle Effect
Post by: darkx on December 30, 2010, 11:52:04 PM
This one uses the library and external media sprite way although it is less efficient. Mostly due to a old library and that the sprites and alpha effects take up a chunk of memory.

Press up to increase the amount of snow particles spawned, down to decrease.


Note:There is no limit to the amount of particles you can spawn. Be sure not to overstress your pc.
Title: Re: Challenge #22 - Snow Particle Effect
Post by: monkeybot on January 05, 2011, 01:52:01 PM
just a quickie,i am going to add settling snow when i get around to it...
[pbcode]; PROJECT : so fake snowflake
; AUTHOR  : Monkeybot
; CREATED : 04/01/2011
; EDITED  : 04/01/2011
; ---------------------------------------------------------------------
explicit on
constant gravity=1
Constant SnowNum=1

type t_flake
   x#
   y#
   cnt#
   mstep#
   min#
   max#
endtype
local g
dim flake as t_flake list
setfps(50)
repeat
   for g=0 to SnowNum
      flake =new t_flake
      flake.x#=rnd(800)
      flake.cnt=rndrange(150,210)   ;set start point
      flake.y#=-rnd(50)
      flake.mstep#=rndrange#(-1,1)
;      flake.min#=rndrange(90,180)
;      flake.max#=rndrange(180,270)
   next
   cls
   lockbuffer
      print "No of flakes "+str$(getlistsize(flake()))
   for each flake()
      flake.y#+=gravity
      flipflop3(flake.x#,flake.y#,150,210,30,60)
      if flake.y>600 then flake=null
   next
   UnLockBuffer   
      sync
until false

;                     
function flipFlop3(x# ,y#,min#,max#,xr#,yr#)
   
   ;0=left   1=right
;   print "min "+str$(min#)
;   print "max "+str$(max#)
;   print "x "+str$(x#)
;   print "y "+str$(y#)
;   print "flake.cnt "+str$(flake.cnt#)
;   print "flake.mstep "+str$(flake.mstep#)
      if flake.mstep#>0
         flake.cnt#+=flake.mstep#
         if flake.cnt#>max# then flake.mstep#=-flake.mstep#;end
      endif
      
      if flake.mstep#<0       
         flake.cnt#-=abs(flake.mstep#)
         if flake.cnt#<min# then flake.mstep#=abs(flake.mstep#);end
      endif   
   circle x#+(sin(flake.cnt)*xr#),y#-(cos(flake.cnt)*yr#),2,1
endfunction   
[/pbcode]
Title: Re: Challenge #22 - Snow Particle Effect
Post by: kevin on January 06, 2011, 12:48:42 PM

Nice ! - There's some really elegant solutions there !
Title: Re: Challenge #22 - Snow Particle Effect
Post by: monkeybot on February 10, 2011, 12:23:45 PM
done a version with settling effect,more difficult than i imagined!


http://dl.dropbox.com/u/19400361/snowflake.rar
Title: Re: Challenge #22 - Snow Particle Effect
Post by: kevin on February 11, 2011, 01:39:20 PM
  Looks pretty good, but there's a few tidbits that could be fine tuned though.  


 The move snow routine can be tweaked so the cost per flake is reduced, for example.

[pbcode]

function movesnow2()
   local ypos

   for each flake()
      flake.y#+=gravity
      flipflop3(150,210)   
      ypos=gety(flake.x#);-60

               ; couldn't this this stored in the ground height, so it's absolute comparison.  Which means less instructions per flake
      if flake.y#>((sh-(imageheight-ypos))-ySwing);+3   
                hitLand(flake.x#,flake.y#)
      endif
   next
endfunction

Psub gety(num#)
   local y,t=abs(num#)
   SetListPos land(),t
   y=land.y
EndPsub y


Psub flipFlop3(min#,max#)
;,xr#,yr#)
      if flake.mstep#>0
            flake.cnt#+=flake.mstep#
            if flake.cnt#>max# then flake.mstep#*=-1
      else
            ; Note: the ABS function of Mstep could be
            ; removed and stored in the structure.
            flake.cnt#-=abs(flake.mstep#)
            if flake.cnt#<min# then flake.mstep#=abs(flake.mstep#)
      endif   
endPsub

[/pbcode]

  You can take this further by using a 1d array to store the grounds height rather than a list, which is significantly less overhead than reading a field in a type.  The ground collision could also store the  highest peek in a global variable for example, so the Y cord is first compared against the highest current peek, which could act as an early rejection for flakes that can't possibly have hit the ground.  Which is going to be say %60 of them..  

  While i think of it, got to careful using too many 'small task' functions in tight loops like this (ie flipFlop3() and gety() ).  Every time you call a function there can be a pretty hefty overhead (the more locals these functions have the bigger the cost), so swapping them to Projected subroutines (psub) can be an easy way of winning back some free time.   The same applies to passing parameters in that aren't being used.  It's just wasted cycles in the vm.  


 

Title: Re: Challenge #22 - Snow Particle Effect
Post by: monkeybot on February 14, 2011, 02:59:25 PM
cool thanks.Food for thought.