Fireflies

Hi, Caleb. I’m sorry to bother you again. I’m trying to make some realistic looking fireflies and so far so good, but I want them to change their angle a little bit during mid fly.

Here’s my code so far.
 

local fireflies = CBE.newVent { preset = "embers", positionType = "inRect", x = 0, y = 0, rectWidth = width, rectHeight = height, build = function() local size = random(5, 15) return display.newImageRect("CBEffects/textures/glow.png", size, size) end, emitDelay = 300, perEmit = 1, outTime = 1200, physics = { velocity = 0.5, angles = {{0, 360}}, scaleRateX = 0.997, scaleRateY = 0.997 } } fireflies:start()

Is this possible? Thanks.

Anything is possible with CBEffects :slight_smile:

Here’s a quick example (at least I think this is something like what you want).

The logic is really simple: every 5 updates on the particle, set the particle’s velocity to a new random one that doesn’t deviate too far from the previous velocity. This keeps the particle from doing weird jittering about.

If you want to make it more precise, you can calculate the angle between the previous velocity and the current and make sure the difference isn’t too big, but you might get a performance hit for that. For most uses, just checking the values is close enough.

Here’s the code:
[lua]
local fireflies = CBE.newVent {
preset = “embers”,
positionType = “inRect”,
x = 0, y = 0,
rectWidth = width,
rectHeight = height,

build = function()
local size = random(5, 15)
return display.newImageRect(“CBEffects/textures/glow.png”, size, size)
end,

onCreation = function§
local xVel, yVel = p:getVelocity()
p.xVel, p.yVel = xVel, yVel – These don’t actually affect the particle, they’re just here to hold a reference to the previous velocity
end,

onUpdate = function§
– You can get higher or lower jitter by modifying the wait value (here it’s 5; it looked pretty good to me)
if p._numUpdates % 5 == 0 then
local xVel, yVel = random(-10, 10) * 0.1, random(-10, 10) * 0.1
while abs(xVel - p.xVel) > 0.5 do – You can also adjust the > 0.5 to be as low or high as you want; this is how far the new velocity is allowed to deviate from the previous one. The lower (approaching zero) you go, the closer to the original.
xVel = random(-10, 10) * 0.1
end
while abs(yVel - p.yVel) > 0.5 do
yVel = random(-10, 10) * 0.1
end

p:setVelocity(xVel, yVel)
p.xVel, p.yVel = xVel, yVel
end
end,

emitDelay = 300,
perEmit = 1,
outTime = 1200,
physics = {
velocity = 0.5,
angles = {{0, 360}},
scaleRateX = 0.997,
scaleRateY = 0.997
}
}
fireflies:start()
[/lua]

_numUpdates is a pseudo-usable property (as in, never officially publicized but has been there forever) that points to the number of times the particle’s been “ticked”. So all we do is check for every fifth tick and re-set the particle’s velocity.

  • Caleb

Anything is possible with CBEffects :slight_smile:

Here’s a quick example (at least I think this is something like what you want).

The logic is really simple: every 5 updates on the particle, set the particle’s velocity to a new random one that doesn’t deviate too far from the previous velocity. This keeps the particle from doing weird jittering about.

If you want to make it more precise, you can calculate the angle between the previous velocity and the current and make sure the difference isn’t too big, but you might get a performance hit for that. For most uses, just checking the values is close enough.

Here’s the code:
[lua]
local fireflies = CBE.newVent {
preset = “embers”,
positionType = “inRect”,
x = 0, y = 0,
rectWidth = width,
rectHeight = height,

build = function()
local size = random(5, 15)
return display.newImageRect(“CBEffects/textures/glow.png”, size, size)
end,

onCreation = function§
local xVel, yVel = p:getVelocity()
p.xVel, p.yVel = xVel, yVel – These don’t actually affect the particle, they’re just here to hold a reference to the previous velocity
end,

onUpdate = function§
– You can get higher or lower jitter by modifying the wait value (here it’s 5; it looked pretty good to me)
if p._numUpdates % 5 == 0 then
local xVel, yVel = random(-10, 10) * 0.1, random(-10, 10) * 0.1
while abs(xVel - p.xVel) > 0.5 do – You can also adjust the > 0.5 to be as low or high as you want; this is how far the new velocity is allowed to deviate from the previous one. The lower (approaching zero) you go, the closer to the original.
xVel = random(-10, 10) * 0.1
end
while abs(yVel - p.yVel) > 0.5 do
yVel = random(-10, 10) * 0.1
end

p:setVelocity(xVel, yVel)
p.xVel, p.yVel = xVel, yVel
end
end,

emitDelay = 300,
perEmit = 1,
outTime = 1200,
physics = {
velocity = 0.5,
angles = {{0, 360}},
scaleRateX = 0.997,
scaleRateY = 0.997
}
}
fireflies:start()
[/lua]

_numUpdates is a pseudo-usable property (as in, never officially publicized but has been there forever) that points to the number of times the particle’s been “ticked”. So all we do is check for every fifth tick and re-set the particle’s velocity.

  • Caleb