Difference between revisions of "Particles"
(added example) |
|||
| (10 intermediate revisions by 2 users not shown) | |||
| Line 3: | Line 3: | ||
'''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/> | '''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/> | ||
'''Available since:''' Gideros 2016.06<br/> | '''Available since:''' Gideros 2016.06<br/> | ||
| − | '''Inherits from:''' [[ | + | '''Inherits from:''' [[Sprite]]<br/> |
=== Description === | === Description === | ||
A particle system which allows to draw several identical dots or bitmaps, with varying colour and orientation. | 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 === | === Example === | ||
| − | < | + | '''Particles 2D''' |
| + | <syntaxhighlight lang="lua"> | ||
-- gideros particles | -- gideros particles | ||
| − | local particleGFX = Texture.new("gfx/ | + | local particleGFX = Texture.new("gfx/yourgfx.png") |
local stars = Particles.new() | local stars = Particles.new() | ||
stars:setTexture(particleGFX) | stars:setTexture(particleGFX) | ||
stage:addChild(stars) | stage:addChild(stars) | ||
| − | |||
-- GAME LOOP | -- GAME LOOP | ||
function onEnterFrame(e) | function onEnterFrame(e) | ||
| − | if (e.time // 1) % | + | if (e.time // 1) % 4 == 0 then |
| − | + | stars:addParticles({ | |
{ | { | ||
x=math.random(480),y=math.random(320), | x=math.random(480),y=math.random(320), | ||
size=32,angle=math.random(360), | size=32,angle=math.random(360), | ||
| − | color= | + | color=0xff00ff,alpha=0.8, |
| − | ttl=16* | + | ttl=16*2, |
speedX=0.01,speedY=0.01,speedAngular=0.15, | speedX=0.01,speedY=0.01,speedAngular=0.15, | ||
| − | speedGrowth=0. | + | speedGrowth=0.9, |
}, | }, | ||
{ | { | ||
x=math.random(480),y=math.random(320), | x=math.random(480),y=math.random(320), | ||
| − | size= | + | size=16,angle=math.random(360), |
| − | color= | + | color=0x00ffff,alpha=0.8, |
| − | ttl= | + | ttl=16*16, |
| − | speedX=0. | + | speedX=0.02,speedY=0.02,speedAngular=0.2, |
| − | speedGrowth=0. | + | speedGrowth=-0.1, |
}, | }, | ||
}) | }) | ||
end | end | ||
end | end | ||
| − | </ | + | |
| + | stage:addEventListener(Event.ENTER_FRAME, onEnterFrame) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | '''Particles 3D''' | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | application:setBackgroundColor(0x5500ff) | ||
| + | |||
| + | local abs, cos, sin, random = math.abs, math.cos, math.sin, math.random | ||
| + | |||
| + | -- set up the 3D view and projection | ||
| + | local sw, sh = application:getContentWidth(), application:getContentHeight() | ||
| + | local projection = Matrix.new() | ||
| + | projection:perspectiveProjection(60, sw/sh, 0.01, 1000) -- fov, aspect, near, far | ||
| + | local view = Viewport.new() | ||
| + | view:setProjection(projection) | ||
| + | view:setPosition(sw/2, sh/2) | ||
| + | view:setScale(sw, -sh, 1) | ||
| + | |||
| + | -- a particle set showing a bubble, for a fountain | ||
| + | local fountain = Particles.new(true, true) | ||
| + | --fountain:setTexture(Texture.new("bubble.png")) | ||
| + | |||
| + | -- position | ||
| + | fountain:setX(0) | ||
| + | fountain:setY(0) | ||
| + | fountain:setZ(0) | ||
| + | |||
| + | -- order | ||
| + | view:setContent(fountain) | ||
| + | stage:addChild(view) | ||
| + | |||
| + | -- look at it | ||
| + | view:lookAt(0,-4,-2, fountain:getX(),fountain:getY(),fountain:getZ(), 0,1,0) | ||
| + | |||
| + | -- our emitter for fountain | ||
| + | function Particles:fountain() | ||
| + | local da = random()*6.28 | ||
| + | local dh = cos(os:clock()/2) | ||
| + | local dr = random()*.001+.008*sin(os:clock()/2) | ||
| + | self:addParticles({ | ||
| + | { | ||
| + | x=0, y=0, z=0, | ||
| + | size=.04, ttl=196, | ||
| + | speedY=.02+.02*abs(dh), speedX=dr*sin(da), speedZ=dr*cos(da), | ||
| + | decay=vector(1,1,1), acceleration=vector(0,-.0005,0), | ||
| + | color=random(0xff00ff), | ||
| + | } | ||
| + | }) | ||
| + | end | ||
| + | |||
| + | -- game loop | ||
| + | stage:addEventListener(Event.ENTER_FRAME,function(e) | ||
| + | for i = 1, 8 do fountain:fountain() end | ||
| + | end) | ||
| + | </syntaxhighlight> | ||
{|- | {|- | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
| + | |||
=== Methods === | === Methods === | ||
[[Particles.new]] ''creates new particles group''<br/><!--GIDEROSMTD:Particles.new() creates a new particles group--> | [[Particles.new]] ''creates new particles group''<br/><!--GIDEROSMTD:Particles.new() creates a new particles group--> | ||
[[Particles:addParticles]] ''adds particles''<br/><!--GIDEROSMTD:Particles:addParticles(particles) adds particles--> | [[Particles:addParticles]] ''adds particles''<br/><!--GIDEROSMTD:Particles:addParticles(particles) adds particles--> | ||
| − | [[Particles:clearTexture]] ''clears texture for all particles''<br/><!--GIDEROSMTD:Particles:clearTexture() clears texture for all particles--> | + | [[Particles:clearTexture]] ''clears texture for all particles''<br/><!--GIDEROSMTD:Particles:clearTexture(slot) clears texture for all particles--> |
| − | [[Particles:getNearestParticle]] ''gets the index of the nearest particle to the given point''<br/><!--GIDEROSMTD:Particles:getNearestParticle(x,y) | + | [[Particles:getDeadParticles]] ''gets dead particles''<br/><!--GIDEROSMTD:Particles:getDeadParticles() gets dead particles--> |
| + | [[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--> | ||
| + | [[Particles:getNumParticles]] ''gets the number of active particles''<br/><!--GIDEROSMTD:Particles:getNumParticles() gets the number of active particles--> | ||
| + | [[Particles:getParticleAcceleration]] ''gets particle acceleration''<br/><!--GIDEROSMTD:Particles:getParticleAcceleration(i) gets particle dacceleration--> | ||
[[Particles:getParticleAngle]] ''gets particle angle''<br/><!--GIDEROSMTD:Particles:getParticleAngle(i) gets particle angle--> | [[Particles:getParticleAngle]] ''gets particle angle''<br/><!--GIDEROSMTD:Particles:getParticleAngle(i) gets particle angle--> | ||
[[Particles:getParticleColor]] ''gets particle color and alpha value''<br/><!--GIDEROSMTD:Particles:getParticleColor(i) gets particle color and alpha value--> | [[Particles:getParticleColor]] ''gets particle color and alpha value''<br/><!--GIDEROSMTD:Particles:getParticleColor(i) gets particle color and alpha value--> | ||
[[Particles:getParticleDecay]] ''gets particle decay factor''<br/><!--GIDEROSMTD:Particles:getParticleDecay(i) gets particle decay factor--> | [[Particles:getParticleDecay]] ''gets particle decay factor''<br/><!--GIDEROSMTD:Particles:getParticleDecay(i) gets particle decay factor--> | ||
| + | [[Particles:getParticleExtra]] ''gets particle extra value''<br/><!--GIDEROSMTD:Particles:getParticleExtra(i) gets particle extra value--> | ||
[[Particles:getParticlePosition]] ''gets particle position''<br/><!--GIDEROSMTD:Particles:getParticlePosition(i) gets particle position--> | [[Particles:getParticlePosition]] ''gets particle position''<br/><!--GIDEROSMTD:Particles:getParticlePosition(i) gets particle position--> | ||
[[Particles:getParticleSize]] ''gets particle size in pixels''<br/><!--GIDEROSMTD:Particles:getParticleSize(i) gets particle size in pixels--> | [[Particles:getParticleSize]] ''gets particle size in pixels''<br/><!--GIDEROSMTD:Particles:getParticleSize(i) gets particle size in pixels--> | ||
| Line 61: | Line 123: | ||
[[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--> | [[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--> | ||
[[Particles:scaleParticles]] ''scales or resizes all particles in this sprite''<br/><!--GIDEROSMTD:Particles:scaleParticles(scale,absolute) scales or resizes all particles in this sprite--> | [[Particles:scaleParticles]] ''scales or resizes all particles in this sprite''<br/><!--GIDEROSMTD:Particles:scaleParticles(scale,absolute) scales or resizes all particles in this sprite--> | ||
| + | [[Particles:setMinimumSize]] ''sets particles minimum size''<br/><!--GIDEROSMTD:Particles:setMinimumSize(minSize) sets particles minimum size--> | ||
| + | [[Particles:setParticleAcceleration]] ''sets particle acceleration''<br/><!--GIDEROSMTD:Particles:setParticleAcceleration(i,acceleration,accelerationAlpha,accelerationGrowth,accelerationAngular) sets particle acceleration--> | ||
[[Particles:setParticleAngle]] ''sets particle angle''<br/><!--GIDEROSMTD:Particles:setParticleAngle(i,angle) sets particle angle--> | [[Particles:setParticleAngle]] ''sets particle angle''<br/><!--GIDEROSMTD:Particles:setParticleAngle(i,angle) sets particle angle--> | ||
[[Particles:setParticleColor]] ''sets particle color''<br/><!--GIDEROSMTD:Particles:setParticleColor(i,color,alpha) sets particle color--> | [[Particles:setParticleColor]] ''sets particle color''<br/><!--GIDEROSMTD:Particles:setParticleColor(i,color,alpha) sets particle color--> | ||
[[Particles:setParticleDecay]] ''sets particle decay factor''<br/><!--GIDEROSMTD:Particles:setParticleDecay(i,decay,decayAlpha,decayGrowth,decayAngular) sets particle decay factor--> | [[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: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:setParticleSize]] ''sets particle size''<br/><!--GIDEROSMTD:Particles:setParticleSize(i,size) sets particle size--> | ||
| Line 70: | Line 135: | ||
[[Particles:setParticleTtl]] ''sets particle time to live''<br/><!--GIDEROSMTD:Particles:setParticleTtl(i,ttl) sets particle time to live--> | [[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: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) sets texture to all particles--> | + | [[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;"| | ||
Latest revision as of 03:04, 17 November 2025
Supported platforms: ![]()
![]()
![]()
![]()
![]()
![]()
![]()
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
application:setBackgroundColor(0x5500ff)
local abs, cos, sin, random = math.abs, math.cos, math.sin, math.random
-- set up the 3D view and projection
local sw, sh = application:getContentWidth(), application:getContentHeight()
local projection = Matrix.new()
projection:perspectiveProjection(60, sw/sh, 0.01, 1000) -- fov, aspect, near, far
local view = Viewport.new()
view:setProjection(projection)
view:setPosition(sw/2, sh/2)
view:setScale(sw, -sh, 1)
-- a particle set showing a bubble, for a fountain
local fountain = Particles.new(true, true)
--fountain:setTexture(Texture.new("bubble.png"))
-- position
fountain:setX(0)
fountain:setY(0)
fountain:setZ(0)
-- order
view:setContent(fountain)
stage:addChild(view)
-- look at it
view:lookAt(0,-4,-2, fountain:getX(),fountain:getY(),fountain:getZ(), 0,1,0)
-- our emitter for fountain
function Particles:fountain()
local da = random()*6.28
local dh = cos(os:clock()/2)
local dr = random()*.001+.008*sin(os:clock()/2)
self:addParticles({
{
x=0, y=0, z=0,
size=.04, ttl=196,
speedY=.02+.02*abs(dh), speedX=dr*sin(da), speedZ=dr*cos(da),
decay=vector(1,1,1), acceleration=vector(0,-.0005,0),
color=random(0xff00ff),
}
})
end
-- game loop
stage:addEventListener(Event.ENTER_FRAME,function(e)
for i = 1, 8 do fountain:fountain() end
end)
MethodsParticles.new creates new particles group |
EventsConstants |