News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Sprite Tweening / Animation

Started by kevin, April 10, 2015, 11:10:35 AM

Previous topic - Next topic

kevin

   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.  

PlayBASIC Code: [Select]
   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







  Tint Colour version

PlayBASIC Code: [Select]
   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




      
      

   

ATLUS

Didn't know about program constant: programdir$()

kevin

#2
 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.  


ATLUS


kevin

#4
  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
      * Handling Multiple Animations using Timer()
   


 Download

   attached