UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on April 10, 2015, 11:10:35 AM

Title: Sprite Tweening / Animation
Post by: kevin on April 10, 2015, 11:10:35 AM
   Sprite Tweening / Animation


      This example create a list of random positions then interpolates a sprite between each key frame in the set.  This first version interpolates just the sprites coordinates/angle and scale, while the second version includes tint colour interpolation also.  

[pbcode]

   filename$=programdir$()+"Help\Commands\Media\ship.bmp"
   image=loadnewimage(Filename$,2)
   

   Type tSpriteTween
         Xpos#
         Ypos#
         Angle#
         Scale#
   EndType

   dim tween(100) as tSpriteTween


   TweenChainSize=50
   Dim Chain(TweenChainSize)
   for lp =0 to TweenChainSize-1
      Chain(lp)=NewTween(rnd(800),rnd(600),rndrange#(50,200)/100.0,rnd(360))
   next   
   
   
   sprite=newsprite(0,0,image)
   spritedrawmode sprite,2
   centerspritehandle sprite
   
   StartTime=timer()
   
   Do
   
      cls
   
      TicksPast=Timer()-StartTime

      // compute the link in the chain where we should render
      Link=(TicksPast/1000)
      // compute  where we are within this link and the next
      Scale#=(TicksPast-(link*1000))/1000.0

      // interpolate the sprites position
      TweenSprite(sprite,LInk,LInk+1,Scale#)

      drawallsprites
   
      sync
   loop
   







function NewTween(xpos#,ypos#,Scale#,Angle#=0)
      index=getfreecell(Tween())
      Tween(index) = new tSpriteTween
      Tween(index).Xpos#   =Xpos#      
      Tween(index).Ypos#   =Ypos#      
      Tween(index).Angle#   =Angle#      
      Tween(index).scale#   =Scale#
   
EndFunction Index

   
   
function TweenSprite(sprite,Src,Dest,Scale#)

      local x1#=Tween(src).Xpos#   
      local y1#=Tween(src).Ypos#   
      local a1#=Tween(src).Angle#   
      local s1#=Tween(src).Scale#   

      local x2#=Tween(dest).Xpos#   
      local y2#=Tween(dest).Ypos#   
      local a2#=Tween(dest).Angle#   
      local s2#=Tween(dest).Scale#   

      positionsprite sprite,x1#+((x2#-x1#)*Scale#),y1#+((y2#-y1#)*Scale#)
      rotatesprite sprite,a1#+((a2#-a1#)*Scale#)
      scaleSprite sprite,s1#+((s2#-s1#)*Scale#)
      
EndFunction Index



[/pbcode]


  Tint Colour version

[pbcode]


   filename$=programdir$()+"Help\Commands\Media\ship.bmp"
   image=loadnewimage(Filename$,2)
   

   Type tSpriteTween
         Xpos#
         Ypos#
         Angle#
         Scale#
         TintColour
   EndType

   dim tween(100) as tSpriteTween


   TweenChainSize=50
   Dim Chain(TweenChainSize)
   for lp =0 to TweenChainSize-1
      Chain(lp)=NewTween(rnd(800),rnd(600),rndrange#(50,200)/100.0,rnd(360))
   next   
   
   
   sprite=newsprite(0,0,image)
   spritedrawmode sprite,2
   centerspritehandle sprite
   
   StartTime=timer()
   
   Do
   
      cls
   
      TicksPast=Timer()-StartTime

      // compute the link in the chain where we should render
      Link=(TicksPast/1000)
      // compute  where we are within this link and the next
      Scale#=(TicksPast-(link*1000))/1000.0

      // interpolate the sprites position
      TweenSprite(sprite,LInk,LInk+1,Scale#)

      drawallsprites
   
      sync
   loop
   







function NewTween(xpos#,ypos#,Scale#,Angle#=0)
      index=getfreecell(Tween())
      Tween(index) = new tSpriteTween
      Tween(index).Xpos#   =Xpos#      
      Tween(index).Ypos#   =Ypos#      
      Tween(index).Angle#   =Angle#      
      Tween(index).scale#   =Scale#
      Tween(index).TintColour   =rndrgb()
      
EndFunction Index

   
   
function TweenSprite(sprite,Src,Dest,Scale#)

      local x1#=Tween(src).Xpos#   
      local y1#=Tween(src).Ypos#   
      local a1#=Tween(src).Angle#   
      local s1#=Tween(src).Scale#   
      local rgb1=Tween(src).TintColour   

      local x2#=Tween(dest).Xpos#   
      local y2#=Tween(dest).Ypos#   
      local a2#=Tween(dest).Angle#   
      local s2#=Tween(dest).Scale#   
      local rgb2=Tween(dest).TintColour   

      positionsprite sprite,x1#+((x2#-x1#)*Scale#),y1#+((y2#-y1#)*Scale#)
      rotatesprite sprite,a1#+((a2#-a1#)*Scale#)
      scaleSprite sprite,s1#+((s2#-s1#)*Scale#)
      
      Spritetint sprite,RGBAlphaBlend(rgb1,rgb2,scale#*100)
      
EndFunction Index


[/pbcode]
      
      

   
Title: Re: Sprite Tweening / Animation
Post by: ATLUS on April 10, 2015, 11:57:14 AM
Didn't know about program constant: programdir$()
Title: Re: Sprite Tweening / Animation
Post by: kevin on April 10, 2015, 11:59:33 AM
 only been there for a decade or so, it's not a constant, it's the folder the program (the EXE, in this case the compiler) was started in.  

Title: Re: Sprite Tweening / Animation
Post by: ATLUS on April 10, 2015, 12:05:55 PM
oh right, thanks.
Title: Re: Sprite Tweening / Animation Library Example
Post by: kevin on April 23, 2015, 10:06:02 AM
  Sprite Tweening / Animation Library Example

  This attached example takes the previous example and creates a library of sorts the user can expand upon.


 Related Examples

     * Movement Path Library Example (http://www.underwaredesign.com/forums/index.php?topic=2253.0)
      * Handling Multiple Animations using Timer() (http://www.underwaredesign.com/forums/index.php?topic=2993.0)
   


 Download

   attached