Pointer sparks

The emitter follows the pointer. Particles stay where they were born, so the stream is left behind as a trail, and a tangential force curls it back.


    
main.lua
local natiny = require("natiny")

natiny.backend.init(arg and arg[1] or natiny.backend.AUTO)

local win = natiny.window.create("Natiny - Pointer sparks", 900, 600)
natiny.window.set_scale_mode(win, natiny.window.SCALE_MODE_STRETCH)

local BG = natiny.color.from_hex("#151E2C")
local sprite = natiny.texture.create(natiny.resource.load("common/textures/particle.png"))

local sparks = natiny.particles.create(1200)
natiny.particles.set_texture(sparks, sprite)
natiny.particles.set_shape(sparks, natiny.particles.SHAPE_CIRCLE, 10)
natiny.particles.set_rate(sparks, 420)
natiny.particles.set_speed(sparks, 40, 130)
natiny.particles.set_lifetime(sparks, 0.6, 1.1)
natiny.particles.set_size(sparks, 8, 24)
natiny.particles.set_sizes(sparks, { 1, 0 })
-- A little push outwards and a lot of swirl about wherever the emitter is
-- standing now, which is what curls the ribbon back towards the pointer.
natiny.particles.set_radial(sparks, 60, 340)
natiny.particles.set_colors(sparks, {
    natiny.color.from_hex("#E9FF7AFF"),
    natiny.color.from_hex("#A8C238AA"),
    natiny.color.from_hex("#3A6BFF00"),
})
natiny.particles.set_blend(sparks, natiny.particles.BLEND_ADD)

natiny.backend.loop(function(dt)
    -- Particles live in world space, so they stay where they were born and
    -- the moving emitter leaves a trail behind the pointer.
    natiny.entity.set_position(sparks, natiny.entity.SPACE_WORLD,
                               natiny.input.get_mouse_x(),
                               natiny.input.get_mouse_y(), 0)

    if natiny.input.get_mouse_button_pressed(natiny.input.MOUSE_BUTTON_LEFT) then
        natiny.particles.emit(sparks, 220)   -- a burst on top of the stream
    end

    natiny.window.bind(win, function()
        natiny.render.clear(BG)
        natiny.render.particles(sparks)
    end)
end)

natiny.backend.shutdown()