Difference between revisions of "Particles"

From GiderosMobile
 
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
<!-- GIDEROSOBJ:Particles -->
 
<!-- GIDEROSOBJ:Particles -->
'''<translate>Supported platforms</translate>:''' <br/>
+
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
'''<translate>Available since</translate>:''' Gideros 2016.06<br/>
+
'''Available since:''' Gideros 2016.06<br/>
'''<translate>Inherits from</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
+
'''Inherits from:''' [[Sprite]]<br/>
=== <translate>Description</translate> ===
+
 
<translate>Particles sprite (alpha), which allows to draw several identical dots or bitmaps with varying colour and orientation</translate>
+
=== Description ===
 +
A particle system which allows to draw several identical dots or bitmaps, with varying colour and orientation.
 +
 
 +
'''Note''': starting from '''Gideros 2022.5''', Particles 3D was added
 +
 
 +
=== Example ===
 +
'''Particles 2D'''
 +
<syntaxhighlight lang="lua">
 +
-- gideros particles
 +
local particleGFX = Texture.new("gfx/yourgfx.png")
 +
local stars = Particles.new()
 +
stars:setTexture(particleGFX)
 +
stage:addChild(stars)
 +
 
 +
-- GAME LOOP
 +
function onEnterFrame(e)
 +
if (e.time // 1) % 4 == 0 then
 +
stars:addParticles({
 +
{
 +
x=math.random(480),y=math.random(320),
 +
size=32,angle=math.random(360),
 +
color=0xff00ff,alpha=0.8,
 +
ttl=16*2,
 +
speedX=0.01,speedY=0.01,speedAngular=0.15,
 +
speedGrowth=0.9,
 +
},
 +
{
 +
x=math.random(480),y=math.random(320),
 +
size=16,angle=math.random(360),
 +
color=0x00ffff,alpha=0.8,
 +
ttl=16*16,
 +
speedX=0.02,speedY=0.02,speedAngular=0.2,
 +
speedGrowth=-0.1,
 +
},
 +
})
 +
end
 +
end
 +
 
 +
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
 +
</syntaxhighlight>
 +
 
 +
'''Particles 3D'''
 +
<syntaxhighlight lang="lua">
 +
-- Set up a fullscreen 3D viewport
 +
local sw,sh=application:getContentWidth(),application:getContentHeight()
 +
local view=D3.View.new(sw,sh,45,0.1,1000)
 +
stage:addChild(view)
 +
local scene=view:getScene()
 +
 
 +
-- We will embed our particles in a 3D mesh, to enable depth testing
 +
local sc=Mesh.new(true)
 +
scene:addChild(sc)
 +
-- A particle set showing a bubble, for a fountain
 +
local fountain=Particles.new(true,true)
 +
fountain:setTexture(Texture.new("Bubble.png",true))
 +
sc:addChild(fountain)
 +
fountain:setX(0)
 +
fountain:setY(0)
 +
fountain:setZ(0)
 +
 
 +
-- Our emitter for fountain
 +
function Particles:fountain()
 +
local da=math.random()*6.28
 +
local dh=math.cos(os:clock()/2)
 +
local dr=math.random()*.001+.008*math.sin(os:clock()/2)
 +
self:addParticles({
 +
{
 +
x=0,y=0,z=0,
 +
size=.05,ttl=200,
 +
speedY=.025+.025*math.abs(dh),speedX=dr*math.sin(da),speedZ=dr*math.cos(da),
 +
decay=vector(1,1,1),acceleration=vector(0,-.0005,0),
 +
color=math.random(0x0, 0xffffff),
 +
}
 +
})
 +
end
 +
 
 +
-- Look at it
 +
view:lookAt(0,4,3, 0,0,0)
 +
 
 +
-- Game loop
 +
stage:addEventListener(Event.ENTER_FRAME,function(e)
 +
for i=1,16 do fountain:fountain() end
 +
end)
 +
</syntaxhighlight>
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Methods</translate> ===
+
 
[[Special:MyLanguage/Particles.new|Particles.new]] ''<translate>Create new particles group</translate>''<br/><!-- GIDEROSMTD:Particles.new -->
+
=== Methods ===
[[Special:MyLanguage/Particles:addParticles|Particles:addParticles]] ''<translate>Add particles</translate>''<br/><!-- GIDEROSMTD:Particles:addParticles -->
+
[[Particles.new]] ''creates new particles group''<br/><!--GIDEROSMTD:Particles.new() creates a new particles group-->
[[Special:MyLanguage/Particles:clearTexture|Particles:clearTexture]] ''<translate>Clear texture for all particles</translate>''<br/><!-- GIDEROSMTD:Particles:clearTexture -->
+
[[Particles:addParticles]] ''adds particles''<br/><!--GIDEROSMTD:Particles:addParticles(particles) adds particles-->
[[Special:MyLanguage/Particles:getParticleAngle|Particles:getParticleAngle]] ''<translate>get particle angle</translate>''<br/><!-- GIDEROSMTD:Particles:getParticleAngle -->
+
[[Particles:clearTexture]] ''clears texture for all particles''<br/><!--GIDEROSMTD:Particles:clearTexture(slot) clears texture for all particles-->
[[Special:MyLanguage/Particles:getParticleColor|Particles:getParticleColor]] ''<translate>Get color and alpha value of particle</translate>''<br/><!-- GIDEROSMTD:Particles:getParticleColor -->
+
[[Particles:getDeadParticles]] ''gets dead particles''<br/><!--GIDEROSMTD:Particles:getDeadParticles() gets dead particles-->
[[Special:MyLanguage/Particles:getParticleDecay|Particles:getParticleDecay]] <br/><!-- GIDEROSMTD:Particles:getParticleDecay -->
+
[[Particles:getNearestParticle]] ''gets the index of the nearest particle to the given point''<br/><!--GIDEROSMTD:Particles:getNearestParticle(x,y) gets the index of the nearest particle to the given point-->
[[Special:MyLanguage/Particles:getParticlePosition|Particles:getParticlePosition]] ''<translate>get position of particle</translate>''<br/><!-- GIDEROSMTD:Particles:getParticlePosition -->
+
[[Particles:getNumParticles]] ''gets the number of active particles''<br/><!--GIDEROSMTD:Particles:getNumParticles() gets the number of active particles-->
[[Special:MyLanguage/Particles:getParticleSize|Particles:getParticleSize]] ''<translate>get size of particle in pixels</translate>''<br/><!-- GIDEROSMTD:Particles:getParticleSize -->
+
[[Particles:getParticleAcceleration]] ''gets particle acceleration''<br/><!--GIDEROSMTD:Particles:getParticleAcceleration(i) gets particle dacceleration-->
[[Special:MyLanguage/Particles:getParticleSpeed|Particles:getParticleSpeed]] ''<translate>Get speed of particle</translate>''<br/><!-- GIDEROSMTD:Particles:getParticleSpeed -->
+
[[Particles:getParticleAngle]] ''gets particle angle''<br/><!--GIDEROSMTD:Particles:getParticleAngle(i) gets particle angle-->
[[Special:MyLanguage/Particles:getParticleTag|Particles:getParticleTag]] <br/><!-- GIDEROSMTD:Particles:getParticleTag -->
+
[[Particles:getParticleColor]] ''gets particle color and alpha value''<br/><!--GIDEROSMTD:Particles:getParticleColor(i) gets particle color and alpha value-->
[[Special:MyLanguage/Particles:getParticleTtl|Particles:getParticleTtl]] ''<translate>get initial time to live of particle</translate>''<br/><!-- GIDEROSMTD:Particles:getParticleTtl -->
+
[[Particles:getParticleDecay]] ''gets particle decay factor''<br/><!--GIDEROSMTD:Particles:getParticleDecay(i) gets particle decay factor-->
[[Special:MyLanguage/Particles:getParticles|Particles:getParticles]] <br/><!-- GIDEROSMTD:Particles:getParticles -->
+
[[Particles:getParticleExtra]] ''gets particle extra value''<br/><!--GIDEROSMTD:Particles:getParticleExtra(i) gets particle extra value-->
[[Special:MyLanguage/Particles:isPaused|Particles:isPaused]] <br/><!-- GIDEROSMTD:Particles:isPaused -->
+
[[Particles:getParticlePosition]] ''gets particle position''<br/><!--GIDEROSMTD:Particles:getParticlePosition(i) gets particle position-->
[[Special:MyLanguage/Particles:removeParticles|Particles:removeParticles]] ''<translate>Remove particles by index in table or as arguments</translate>''<br/><!-- GIDEROSMTD:Particles:removeParticles -->
+
[[Particles:getParticleSize]] ''gets particle size in pixels''<br/><!--GIDEROSMTD:Particles:getParticleSize(i) gets particle size in pixels-->
[[Special:MyLanguage/Particles:setParticleAngle|Particles:setParticleAngle]] ''<translate>set angle of particle</translate>''<br/><!-- GIDEROSMTD:Particles:setParticleAngle -->
+
[[Particles:getParticleSpeed]] ''gets particle speed''<br/><!--GIDEROSMTD:Particles:getParticleSpeed(i) gets particle speed-->
[[Special:MyLanguage/Particles:setParticleColor|Particles:setParticleColor]] ''<translate>set color of particles</translate>''<br/><!-- GIDEROSMTD:Particles:setParticleColor -->
+
[[Particles:getParticleTag]] ''gets particle associated tag''<br/><!--GIDEROSMTD:Particles:getParticleTag(i) gets particle associated tag-->
[[Special:MyLanguage/Particles:setParticleDecay|Particles:setParticleDecay]] <br/><!-- GIDEROSMTD:Particles:setParticleDecay -->
+
[[Particles:getParticleTtl]] ''gets particle initial time to live''<br/><!-- GIDEROSMTD:Particles:getParticleTtl(i) gets particle initial time to live-->
[[Special:MyLanguage/Particles:setParticlePosition|Particles:setParticlePosition]] ''<translate>set position of particle</translate>''<br/><!-- GIDEROSMTD:Particles:setParticlePosition -->
+
[[Particles:getParticles]] ''gets particles''<br/><!--GIDEROSMTD:Particles:getParticles(set,tag) gets particles-->
[[Special:MyLanguage/Particles:setParticleSize|Particles:setParticleSize]] ''<translate>set size of particle</translate>''<br/><!-- GIDEROSMTD:Particles:setParticleSize -->
+
[[Particles:isPaused]] ''tells if the Particle set is paused''<br/><!--GIDEROSMTD:Particles:isPaused() tells if the Particle set is paused-->
[[Special:MyLanguage/Particles:setParticleSpeed|Particles:setParticleSpeed]] ''<translate>Set speed of particles</translate>''<br/><!-- GIDEROSMTD:Particles:setParticleSpeed -->
+
[[Particles:removeParticles]] ''removes particles by index in table or as arguments''<br/><!--GIDEROSMTD:Particles:removeParticles(particle indeces) removes particles by index in table or as arguments-->
[[Special:MyLanguage/Particles:setParticleTag|Particles:setParticleTag]] <br/><!-- GIDEROSMTD:Particles:setParticleTag -->
+
[[Particles:scaleParticles]] ''scales or resizes all particles in this sprite''<br/><!--GIDEROSMTD:Particles:scaleParticles(scale,absolute) scales or resizes all particles in this sprite-->
[[Special:MyLanguage/Particles:setParticleTtl|Particles:setParticleTtl]] ''<translate>set time to live</translate>''<br/><!-- GIDEROSMTD:Particles:setParticleTtl -->
+
[[Particles:setParticleAcceleration]] ''sets particle acceleration''<br/><!--GIDEROSMTD:Particles:setParticleAcceleration(i,acceleration,accelerationAlpha,accelerationGrowth,accelerationAngular) sets particle acceleration-->
[[Special:MyLanguage/Particles:setParticlesTag|Particles:setParticlesTag]] <br/><!-- GIDEROSMTD:Particles:setParticlesTag -->
+
[[Particles:setParticleAngle]] ''sets particle angle''<br/><!--GIDEROSMTD:Particles:setParticleAngle(i,angle) sets particle angle-->
[[Special:MyLanguage/Particles:setPaused|Particles:setPaused]] <br/><!-- GIDEROSMTD:Particles:setPaused -->
+
[[Particles:setParticleColor]] ''sets particle color''<br/><!--GIDEROSMTD:Particles:setParticleColor(i,color,alpha) sets particle color-->
[[Special:MyLanguage/Particles:setTexture|Particles:setTexture]] ''<translate>Set texture to all particles</translate>''<br/><!-- GIDEROSMTD:Particles:setTexture -->
+
[[Particles:setParticleDecay]] ''sets particle decay factor''<br/><!--GIDEROSMTD:Particles:setParticleDecay(i,decay,decayAlpha,decayGrowth,decayAngular) sets particle decay factor-->
 +
[[Particles:setParticleExtra]] ''sets particle extra value''<br/><!--GIDEROSMTD:Particles:setParticleExtra(i,extra) sets particle extra value-->
 +
[[Particles:setParticlePosition]] ''sets particle position''<br/><!--GIDEROSMTD:Particles:setParticlePosition(i,x,y) sets particle position-->
 +
[[Particles:setParticleSize]] ''sets particle size''<br/><!--GIDEROSMTD:Particles:setParticleSize(i,size) sets particle size-->
 +
[[Particles:setParticleSpeed]] ''sets particle speed''<br/><!--GIDEROSMTD:Particles:setParticleSpeed(i,x,y,a,s) sets particle speed-->
 +
[[Particles:setParticleTag]] ''sets particle tag''<br/><!--GIDEROSMTD:Particles:setParticleTag(i,tag) sets particle tag-->
 +
[[Particles:setParticleTtl]] ''sets particle time to live''<br/><!--GIDEROSMTD:Particles:setParticleTtl(i,ttl) sets particle time to live-->
 +
[[Particles:setPaused]] ''pauses or resumes the Particle set''<br/><!--GIDEROSMTD:Particles:setPaused(paused) pauses or resumes the Particle set-->
 +
[[Particles:setTexture]] ''sets texture to all particles''<br/><!--GIDEROSMTD:Particles:setTexture(texture,slot) sets texture to all particles-->
 +
 
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
=== <translate>Events</translate> ===
+
 
=== <translate>Constants</translate> ===
+
=== Events ===
 +
=== Constants ===
 
|}
 
|}
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 20:28, 21 September 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2016.06
Inherits from: Sprite

Description

A particle system which allows to draw several identical dots or bitmaps, with varying colour and orientation.

Note: starting from Gideros 2022.5, Particles 3D was added

Example

Particles 2D

-- gideros particles
local particleGFX = Texture.new("gfx/yourgfx.png")
local stars = Particles.new()
stars:setTexture(particleGFX)
stage:addChild(stars)

-- GAME LOOP
function onEnterFrame(e)
	if (e.time // 1) % 4 == 0 then
		stars:addParticles({
			{
				x=math.random(480),y=math.random(320),
				size=32,angle=math.random(360),
				color=0xff00ff,alpha=0.8,
				ttl=16*2,
				speedX=0.01,speedY=0.01,speedAngular=0.15,
				speedGrowth=0.9,
			},
			{
				x=math.random(480),y=math.random(320),
				size=16,angle=math.random(360),
				color=0x00ffff,alpha=0.8,
				ttl=16*16,
				speedX=0.02,speedY=0.02,speedAngular=0.2,
				speedGrowth=-0.1,
			},
		})
	end
end

stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)

Particles 3D

-- Set up a fullscreen 3D viewport
local sw,sh=application:getContentWidth(),application:getContentHeight()
local view=D3.View.new(sw,sh,45,0.1,1000)
stage:addChild(view)
local scene=view:getScene()

-- We will embed our particles in a 3D mesh, to enable depth testing
local sc=Mesh.new(true)
scene:addChild(sc)
-- A particle set showing a bubble, for a fountain
local fountain=Particles.new(true,true)
fountain:setTexture(Texture.new("Bubble.png",true))
sc:addChild(fountain)
fountain:setX(0)
fountain:setY(0)
fountain:setZ(0)

-- Our emitter for fountain
function Particles:fountain()
	local da=math.random()*6.28
	local dh=math.cos(os:clock()/2)
	local dr=math.random()*.001+.008*math.sin(os:clock()/2)
	self:addParticles({
		{
			x=0,y=0,z=0,
			size=.05,ttl=200,
			speedY=.025+.025*math.abs(dh),speedX=dr*math.sin(da),speedZ=dr*math.cos(da),
			decay=vector(1,1,1),acceleration=vector(0,-.0005,0),
			color=math.random(0x0, 0xffffff),
		}
	})
end

-- Look at it
view:lookAt(0,4,3, 0,0,0)

-- Game loop
stage:addEventListener(Event.ENTER_FRAME,function(e)
	for i=1,16 do fountain:fountain() end
end)

Methods

Particles.new creates new particles group
Particles:addParticles adds particles
Particles:clearTexture clears texture for all particles
Particles:getDeadParticles gets dead particles
Particles:getNearestParticle gets the index of the nearest particle to the given point
Particles:getNumParticles gets the number of active particles
Particles:getParticleAcceleration gets particle acceleration
Particles:getParticleAngle gets particle angle
Particles:getParticleColor gets particle color and alpha value
Particles:getParticleDecay gets particle decay factor
Particles:getParticleExtra gets particle extra value
Particles:getParticlePosition gets particle position
Particles:getParticleSize gets particle size in pixels
Particles:getParticleSpeed gets particle speed
Particles:getParticleTag gets particle associated tag
Particles:getParticleTtl gets particle initial time to live
Particles:getParticles gets particles
Particles:isPaused tells if the Particle set is paused
Particles:removeParticles removes particles by index in table or as arguments
Particles:scaleParticles scales or resizes all particles in this sprite
Particles:setParticleAcceleration sets particle acceleration
Particles:setParticleAngle sets particle angle
Particles:setParticleColor sets particle color
Particles:setParticleDecay sets particle decay factor
Particles:setParticleExtra sets particle extra value
Particles:setParticlePosition sets particle position
Particles:setParticleSize sets particle size
Particles:setParticleSpeed sets particle speed
Particles:setParticleTag sets particle tag
Particles:setParticleTtl sets particle time to live
Particles:setPaused pauses or resumes the Particle set
Particles:setTexture sets texture to all particles

Events

Constants