[bb] Simple particle engine by Andres [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Simple particle engine
Author : Andres
Posted : 1+ years ago

Description : image% - either singleframed or multiframed image handle
x#, y# - location of the particle
xs#, ys# - velocity of the particle
duration% - duration of the particle in millisecs
frames% - if does then how many frames particle has (default: 1 frame)
Global Inerts# - Will be multiplied with velocity in every update


Code :
Code (blitzbasic) Select
Type particle
Field image%, x#, y#, xs#, ys#, w%, h%, time%, duration%, frames%
End Type

Global Inerts# = .9

Function CreateParticle(image%, x#, y#, xs#, ys#, duration%, frames% = 1)
this.particle = New particle
thisimage% = CopyImage(image%)
thisx# = x#
thisy# = y#
thisxs# = xs#
thisys# = ys#
thisw% = ImageWidth(thisimage%)
thish% = ImageHeight(thisimage%)
this ime% = MilliSecs()
thisduration% = duration%
thisframes% = frames%
End Function

Function DrawParticles()
For par.particle = Each particle
If parframes% = 1
DrawImage parimage%, parx% - parw% / 2, pary% - parh% / 2
Else
frame% = (parframes% - 1) * (Float (MilliSecs() - par ime%) / parduration%)
If frame% > parframes% Then frame% = parframes%
DrawImage parimage%, parx% - parw% / 2, pary% - parh% / 2, frame%
EndIf
Next
End Function

Function UpdateParticles()
For par.particle = Each particle
If MilliSecs() - par ime% < parduration% Then
parx# = parx# + parxs#
pary# = pary# + parys#

parxs# = parxs# * Inerts#
parys# = parys# * Inerts#
Else
FreeImage parimage%
Delete par
EndIf
Next
End Function


Comments : none...